add file select for multiple country
This commit is contained in:
parent
e5eee656d5
commit
652ecbbf1f
9 changed files with 294 additions and 47 deletions
|
|
@ -1 +1 @@
|
|||
Subproject commit bb7e2e1d3ce239ac877ed0ecdad934eef4f2513d
|
||||
Subproject commit 5adec80644b88c732a06331bfa002427bf0a84da
|
||||
|
|
@ -2,11 +2,12 @@ package data
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"recipe-manager/helpers"
|
||||
"recipe-manager/models"
|
||||
"sort"
|
||||
"time"
|
||||
)
|
||||
|
||||
|
|
@ -40,44 +41,34 @@ type RecipeWithTimeStamps struct {
|
|||
|
||||
type Data struct {
|
||||
CurrentVersion string
|
||||
AllVersions []string
|
||||
AllRecipeFiles map[string][]helpers.RecipePath
|
||||
currentRecipe *models.Recipe
|
||||
recipeMap map[string]RecipeWithTimeStamps
|
||||
Countries []helpers.CountryName
|
||||
}
|
||||
|
||||
func NewData() *Data {
|
||||
|
||||
files, err := filepath.Glob("cofffeemachineConfig/coffeethai02_*.json")
|
||||
if err != nil {
|
||||
log.Panic("Error when scan recipe files:", err)
|
||||
countries := []helpers.CountryName{{
|
||||
CountryID: "thai",
|
||||
CountryName: "Thailand",
|
||||
}, {
|
||||
CountryID: "mys",
|
||||
CountryName: "Malaysia",
|
||||
}, {
|
||||
CountryID: "aus",
|
||||
CountryName: "Australia",
|
||||
},
|
||||
}
|
||||
|
||||
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])
|
||||
}
|
||||
allRecipeFiles := helpers.ScanRecipeFiles(countries)
|
||||
|
||||
defaultVersion := "coffeethai02_580.json"
|
||||
defaultRecipe := readFile(defaultVersion)
|
||||
|
||||
return &Data{
|
||||
CurrentVersion: defaultVersion,
|
||||
AllVersions: files,
|
||||
AllRecipeFiles: allRecipeFiles,
|
||||
currentRecipe: defaultRecipe,
|
||||
recipeMap: map[string]RecipeWithTimeStamps{
|
||||
defaultVersion: {
|
||||
|
|
@ -85,6 +76,7 @@ func NewData() *Data {
|
|||
TimeStamps: time.Now().Unix(),
|
||||
},
|
||||
},
|
||||
Countries: countries,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -225,3 +217,21 @@ func (d *Data) GetMaterialCode(ids []uint64, version string) []models.MaterialCo
|
|||
|
||||
return resultFilter
|
||||
}
|
||||
|
||||
func (d *Data) GetCountryNameByID(countryID string) (string, error) {
|
||||
for _, country := range d.Countries {
|
||||
if country.CountryID == countryID {
|
||||
return country.CountryName, nil
|
||||
}
|
||||
}
|
||||
return "", fmt.Errorf("country ID: %s not found", countryID)
|
||||
}
|
||||
|
||||
func (d *Data) GetCountryIDByName(countryName string) (string, error) {
|
||||
for _, country := range d.Countries {
|
||||
if country.CountryName == countryName {
|
||||
return country.CountryID, nil
|
||||
}
|
||||
}
|
||||
return "", fmt.Errorf("country name: %s not found", countryName)
|
||||
}
|
||||
|
|
|
|||
101
server/helpers/filereader.go
Normal file
101
server/helpers/filereader.go
Normal file
|
|
@ -0,0 +1,101 @@
|
|||
package helpers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"recipe-manager/models"
|
||||
"sort"
|
||||
)
|
||||
|
||||
func ListFile(rootPath string) []string {
|
||||
files, err := filepath.Glob(rootPath)
|
||||
if err != nil {
|
||||
log.Panic("Error when scan recipe files:", err)
|
||||
}
|
||||
return files
|
||||
}
|
||||
|
||||
func ReadFile(filePath string) (string, error) {
|
||||
file, err := os.ReadFile(filePath)
|
||||
|
||||
if err != nil {
|
||||
log.Fatalf("Error when open file: %s", err)
|
||||
return "", err
|
||||
}
|
||||
|
||||
return string(file), nil
|
||||
}
|
||||
|
||||
func ReadRecipeFile(filePath string) models.Recipe {
|
||||
file, err := os.Open(filePath)
|
||||
|
||||
if err != nil {
|
||||
log.Fatalf("Error when open file: %s", err)
|
||||
return models.Recipe{}
|
||||
}
|
||||
|
||||
defer file.Close()
|
||||
|
||||
var data models.Recipe
|
||||
|
||||
err = json.NewDecoder(file).Decode(&data)
|
||||
|
||||
if err != nil {
|
||||
log.Fatalf("Error when decode file: %s", err)
|
||||
return models.Recipe{}
|
||||
}
|
||||
|
||||
return data
|
||||
}
|
||||
|
||||
type RecipePath struct {
|
||||
Name string `json:"name"`
|
||||
Path string `json:"path"`
|
||||
}
|
||||
|
||||
type CountryName struct {
|
||||
CountryID string `json:"countryId"`
|
||||
CountryName string `json:"countryName"`
|
||||
}
|
||||
|
||||
func ScanRecipeFiles(countries []CountryName) map[string][]RecipePath {
|
||||
recipeFiles := map[string][]RecipePath{}
|
||||
|
||||
for _, country := range countries {
|
||||
var files []string
|
||||
if country.CountryID == "thai" {
|
||||
files = ListFile("cofffeemachineConfig/coffeethai02_*.json")
|
||||
} else {
|
||||
files = ListFile("cofffeemachineConfig/" + country.CountryID + "/coffeethai02_*.json")
|
||||
}
|
||||
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++ {
|
||||
if _, ok := recipeFiles[country.CountryID]; !ok {
|
||||
recipeFiles[country.CountryID] = []RecipePath{}
|
||||
}
|
||||
|
||||
recipeFiles[country.CountryID] = append(recipeFiles[country.CountryID], RecipePath{
|
||||
Name: filepath.Base(files[i]),
|
||||
Path: files[i],
|
||||
})
|
||||
}
|
||||
}
|
||||
return recipeFiles
|
||||
}
|
||||
|
|
@ -2,6 +2,7 @@ package routers
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"recipe-manager/data"
|
||||
"recipe-manager/models"
|
||||
|
|
@ -123,7 +124,33 @@ func (rr *RecipeRouter) Route(r chi.Router) {
|
|||
})
|
||||
|
||||
r.Get("/versions", func(w http.ResponseWriter, r *http.Request) {
|
||||
json.NewEncoder(w).Encode(rr.data.AllVersions)
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
// get key from map
|
||||
keys := []string{}
|
||||
for k := range rr.data.AllRecipeFiles {
|
||||
countryName, err := rr.data.GetCountryNameByID(k)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
keys = append(keys, countryName)
|
||||
}
|
||||
json.NewEncoder(w).Encode(keys)
|
||||
})
|
||||
|
||||
r.Get("/versions/{country}", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
|
||||
countryName := chi.URLParam(r, "country")
|
||||
countryID, err := rr.data.GetCountryIDByName(countryName)
|
||||
if err != nil {
|
||||
http.Error(w, fmt.Sprintf("Country Name: %s not found!!!", countryName), http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
files := []string{}
|
||||
for _, v := range rr.data.AllRecipeFiles[countryID] {
|
||||
files = append(files, v.Name)
|
||||
}
|
||||
json.NewEncoder(w).Encode(files)
|
||||
})
|
||||
|
||||
r.Get("/test/sheet", func(w http.ResponseWriter, r *http.Request) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue