2023-10-20 17:05:24 +07:00
|
|
|
package helpers
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
2023-10-31 10:30:06 +07:00
|
|
|
"fmt"
|
2023-10-20 17:05:24 +07:00
|
|
|
"log"
|
|
|
|
|
"os"
|
2023-10-24 18:01:52 +07:00
|
|
|
"path"
|
2023-10-20 17:05:24 +07:00
|
|
|
"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
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-31 10:30:06 +07:00
|
|
|
func ReadRecipeFile(countryID, filePath string) (*models.Recipe, error) {
|
2023-10-24 18:01:52 +07:00
|
|
|
|
|
|
|
|
file, err := os.Open(path.Join("cofffeemachineConfig", countryID, filePath))
|
2023-10-20 17:05:24 +07:00
|
|
|
|
|
|
|
|
if err != nil {
|
2023-10-31 10:30:06 +07:00
|
|
|
return nil, fmt.Errorf("error when open file: %s", err)
|
2023-10-20 17:05:24 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
defer file.Close()
|
|
|
|
|
|
2023-10-24 18:01:52 +07:00
|
|
|
var data *models.Recipe
|
2023-10-20 17:05:24 +07:00
|
|
|
|
|
|
|
|
err = json.NewDecoder(file).Decode(&data)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
2023-10-31 10:30:06 +07:00
|
|
|
return nil, fmt.Errorf("error when decode file: %s", err)
|
2023-10-20 17:05:24 +07:00
|
|
|
}
|
|
|
|
|
|
2023-10-31 10:30:06 +07:00
|
|
|
return data, nil
|
2023-10-20 17:05:24 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 {
|
2023-11-27 19:58:07 +07:00
|
|
|
files := ListFile("cofffeemachineConfig/" + country.CountryID + "/coffeethai02_*.json")
|
2023-10-20 17:05:24 +07:00
|
|
|
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
|
|
|
|
|
}
|
2024-03-18 14:11:24 +07:00
|
|
|
|
|
|
|
|
func LoadCountrySettings() []CountryName {
|
|
|
|
|
res := make([]CountryName, 0)
|
|
|
|
|
|
|
|
|
|
// read file country.settings.json
|
|
|
|
|
content, err := os.Open("country.settings.json")
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Errorf("country.settings.json not found")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// read content to json
|
|
|
|
|
var countrySettings []map[string]interface{}
|
|
|
|
|
err = json.NewDecoder(content).Decode(&countrySettings)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Errorf("error in country.settings")
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, v := range countrySettings {
|
|
|
|
|
if ignore, ok := v["ignore"].(bool); ok {
|
|
|
|
|
if ignore {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
res = append(res, CountryName{
|
|
|
|
|
CountryID: v["short"].(string),
|
|
|
|
|
CountryName: v["name"].(string),
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return res
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// integrate with permissions
|
|
|
|
|
|
|
|
|
|
type CountryNamePerms struct {
|
|
|
|
|
CountryID string
|
|
|
|
|
CountryName string
|
|
|
|
|
CountryPermission int
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func LoadCountrySettingsWithPermissions() []CountryNamePerms {
|
|
|
|
|
res := make([]CountryNamePerms, 0)
|
|
|
|
|
|
|
|
|
|
// read file country.settings.json
|
|
|
|
|
content, err := os.Open("country.settings.json")
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Errorf("country.settings.json not found")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// read content to json
|
|
|
|
|
var countrySettings []map[string]interface{}
|
|
|
|
|
err = json.NewDecoder(content).Decode(&countrySettings)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Errorf("error in country.settings")
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, v := range countrySettings {
|
|
|
|
|
if ignore, ok := v["ignore"].(bool); ok {
|
|
|
|
|
if ignore {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
res = append(res, CountryNamePerms{
|
|
|
|
|
CountryID: v["short"].(string),
|
|
|
|
|
CountryName: v["name"].(string),
|
|
|
|
|
CountryPermission: int(v["permissions"].(float64)),
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return res
|
|
|
|
|
}
|