update recipe now can select recipe file
This commit is contained in:
parent
b25c836f0c
commit
b5cb469a30
11 changed files with 209 additions and 236173 deletions
2
server/.gitignore
vendored
2
server/.gitignore
vendored
|
|
@ -1,4 +1,4 @@
|
|||
/cofffeemachineConfig
|
||||
/cofffeemachineConfig/*
|
||||
**/*.log
|
||||
token.json
|
||||
client_secret.json
|
||||
|
|
|
|||
1
server/cofffeemachineConfig
Submodule
1
server/cofffeemachineConfig
Submodule
|
|
@ -0,0 +1 @@
|
|||
Subproject commit 9d6f2eb9c961e256c97b81ca9829eb57e5d890a9
|
||||
|
|
@ -4,11 +4,14 @@ import (
|
|||
"encoding/json"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"recipe-manager/models"
|
||||
"sort"
|
||||
)
|
||||
|
||||
func readFile() *models.Recipe {
|
||||
file, err := os.Open("data/data.json")
|
||||
func readFile(version string) *models.Recipe {
|
||||
path := filepath.Join("cofffeemachineConfig", version)
|
||||
file, err := os.Open(path)
|
||||
|
||||
if err != nil {
|
||||
log.Fatalf("Error when open file: %s", err)
|
||||
|
|
@ -30,16 +33,56 @@ func readFile() *models.Recipe {
|
|||
}
|
||||
|
||||
type Data struct {
|
||||
recipe *models.Recipe
|
||||
CurrentVersion string
|
||||
AllVersions []string
|
||||
recipe *models.Recipe
|
||||
}
|
||||
|
||||
func NewData() *Data {
|
||||
|
||||
files, err := filepath.Glob("cofffeemachineConfig/coffeethai02_*.json")
|
||||
if err != nil {
|
||||
log.Panic("Error when scan recipe files:", err)
|
||||
}
|
||||
|
||||
sort.Slice(files, func(i, j int) bool {
|
||||
file1, err := os.Stat(files[i])
|
||||
|
||||
if err != nil {
|
||||
log.Panic("Error when get file info:", err)
|
||||
}
|
||||
|
||||
file2, err := os.Stat(files[j])
|
||||
|
||||
if err != nil {
|
||||
log.Panic("Error when get file info:", err)
|
||||
}
|
||||
|
||||
return file1.ModTime().After(file2.ModTime())
|
||||
})
|
||||
|
||||
for i := 0; i < len(files); i++ {
|
||||
files[i] = filepath.Base(files[i])
|
||||
}
|
||||
|
||||
return &Data{
|
||||
recipe: readFile(),
|
||||
CurrentVersion: "coffeethai02_580.json",
|
||||
AllVersions: files,
|
||||
recipe: readFile("coffeethai02_580.json"),
|
||||
}
|
||||
}
|
||||
|
||||
func (d *Data) GetRecipe() models.Recipe {
|
||||
func (d *Data) GetRecipe(version string) models.Recipe {
|
||||
|
||||
if version == "" || version == d.CurrentVersion {
|
||||
return *d.recipe
|
||||
}
|
||||
|
||||
log.Println("Change recipe to version:", version)
|
||||
|
||||
// change current version and read new recipe
|
||||
d.CurrentVersion = version
|
||||
d.recipe = readFile(version)
|
||||
return *d.recipe
|
||||
}
|
||||
|
||||
|
|
|
|||
236126
server/data/data.json
236126
server/data/data.json
File diff suppressed because one or more lines are too long
|
|
@ -35,7 +35,9 @@ func (rr *RecipeRouter) Route(r chi.Router) {
|
|||
take = newTake
|
||||
}
|
||||
|
||||
recipe := rr.data.GetRecipe()
|
||||
version := r.URL.Query().Get("version")
|
||||
|
||||
recipe := rr.data.GetRecipe(version)
|
||||
searchQuery := r.URL.Query().Get("search")
|
||||
|
||||
if searchQuery != "" {
|
||||
|
|
@ -62,8 +64,9 @@ func (rr *RecipeRouter) Route(r chi.Router) {
|
|||
}
|
||||
|
||||
json.NewEncoder(w).Encode(map[string]interface{}{
|
||||
"recipes": recipe,
|
||||
"hasMore": isHasMore,
|
||||
"fileName": rr.data.CurrentVersion,
|
||||
"recipes": recipe,
|
||||
"hasMore": isHasMore,
|
||||
})
|
||||
})
|
||||
|
||||
|
|
@ -86,9 +89,15 @@ func (rr *RecipeRouter) Route(r chi.Router) {
|
|||
http.Error(w, "Recipe not found", http.StatusNotFound)
|
||||
})
|
||||
|
||||
r.Get("/json", func(w http.ResponseWriter, r *http.Request) {
|
||||
r.Get("/{version}/json", func(w http.ResponseWriter, r *http.Request) {
|
||||
version := chi.URLParam(r, "version")
|
||||
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(rr.data.GetRecipe())
|
||||
json.NewEncoder(w).Encode(rr.data.GetRecipe(version))
|
||||
})
|
||||
|
||||
r.Get("/versions", func(w http.ResponseWriter, r *http.Request) {
|
||||
json.NewEncoder(w).Encode(rr.data.AllVersions)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue