update getting file recipe

This commit is contained in:
Kenta420 2023-10-24 18:01:52 +07:00
parent ea506b8128
commit 3bfbbd778a
10 changed files with 243 additions and 152 deletions

View file

@ -26,7 +26,8 @@ func (mr *MaterialRouter) Route(r chi.Router) {
r.Get("/code", func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
version := r.URL.Query().Get("version")
filename := r.URL.Query().Get("filename")
country := r.URL.Query().Get("country")
matIDs := r.URL.Query().Get("mat_ids")
@ -41,7 +42,14 @@ func (mr *MaterialRouter) Route(r chi.Router) {
matIDsUint = append(matIDsUint, matIDUint)
}
material := mr.data.GetMaterialCode(matIDsUint, version)
countryID, err := mr.data.GetCountryIDByName(country)
if err != nil {
http.Error(w, "Country not found", http.StatusNotFound)
return
}
material := mr.data.GetMaterialCode(matIDsUint, countryID, filename)
json.NewEncoder(w).Encode(material)
})
@ -49,9 +57,17 @@ func (mr *MaterialRouter) Route(r chi.Router) {
r.Get("/setting/{mat_id}", func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
version := r.URL.Query().Get("version")
filename := r.URL.Query().Get("filename")
country := r.URL.Query().Get("country")
material := mr.data.GetMaterialSetting(version)
countryID, err := mr.data.GetCountryIDByName(country)
if err != nil {
http.Error(w, "Country not found", http.StatusNotFound)
return
}
material := mr.data.GetMaterialSetting(countryID, filename)
matID := chi.URLParam(r, "mat_id")

View file

@ -6,6 +6,7 @@ import (
"io/fs"
"net/http"
"os"
"path"
"recipe-manager/data"
"recipe-manager/models"
"recipe-manager/services/logger"
@ -47,9 +48,17 @@ func (rr *RecipeRouter) Route(r chi.Router) {
take = newTake
}
version := r.URL.Query().Get("version")
country := r.URL.Query().Get("country")
filename := r.URL.Query().Get("filename")
recipe := rr.data.GetRecipe(version)
countryID, err := rr.data.GetCountryIDByName(country)
if err != nil {
http.Error(w, fmt.Sprintf("Country Name: %s not found!!!", country), http.StatusNotFound)
return
}
recipe := rr.data.GetRecipe(countryID, filename)
searchQuery := r.URL.Query().Get("search")
if searchQuery != "" {
@ -76,7 +85,7 @@ func (rr *RecipeRouter) Route(r chi.Router) {
}
json.NewEncoder(w).Encode(map[string]interface{}{
"fileName": rr.data.CurrentVersion,
"fileName": rr.data.CurrentFile,
"recipes": recipe,
"hasMore": isHasMore,
})
@ -124,11 +133,17 @@ func (rr *RecipeRouter) Route(r chi.Router) {
})
})
r.Get("/{version}/json", func(w http.ResponseWriter, r *http.Request) {
version := chi.URLParam(r, "version")
r.Get("/{country}/{filename}/json", func(w http.ResponseWriter, r *http.Request) {
country := chi.URLParam(r, "country")
filename := chi.URLParam(r, "filename")
w.Header().Add("Content-Type", "application/json")
json.NewEncoder(w).Encode(rr.data.GetRecipe(version))
countryID, err := rr.data.GetCountryIDByName(country)
if err != nil {
http.Error(w, fmt.Sprintf("Country Name: %s not found!!!", country), http.StatusNotFound)
return
}
json.NewEncoder(w).Encode(rr.data.GetRecipe(countryID, filename))
})
r.Get("/versions", func(w http.ResponseWriter, r *http.Request) {
@ -179,16 +194,24 @@ func (rr *RecipeRouter) Route(r chi.Router) {
json.NewEncoder(w).Encode(mapResult)
})
r.Post("/edit/{version}", func(w http.ResponseWriter, r *http.Request) {
r.Post("/edit/{country}/{filename}", func(w http.ResponseWriter, r *http.Request) {
Log.Debug("Edit: ", zap.String("path", r.RequestURI))
version := chi.URLParam(r, "version")
target_recipe := rr.data.GetRecipe(version)
filename := chi.URLParam(r, "filename")
country := chi.URLParam(r, "country")
countryID, err := rr.data.GetCountryIDByName(country)
if err != nil {
http.Error(w, fmt.Sprintf("Country Name: %s not found!!!", country), http.StatusNotFound)
return
}
target_recipe := rr.data.GetRecipe(countryID, filename)
Log.Debug("Target => ", zap.Any("target", target_recipe.MachineSetting.ConfigNumber))
// Body
var changes models.Recipe01
err := json.NewDecoder(r.Body).Decode(&changes)
err = json.NewDecoder(r.Body).Decode(&changes)
if err != nil {
Log.Error("Decode in request failed: ", zap.Error(err))
}
@ -219,7 +242,7 @@ func (rr *RecipeRouter) Route(r chi.Router) {
return
}
os.WriteFile("./cofffeemachineConfig/coffeethai02_"+version+".json", temp_finalData, fs.FileMode(0666))
os.WriteFile(path.Join("cofffeemachineConfig", countryID, filename), temp_finalData, fs.FileMode(0666))
w.Header().Add("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]interface{}{