update getting file recipe
This commit is contained in:
parent
ea506b8128
commit
3bfbbd778a
10 changed files with 243 additions and 152 deletions
|
|
@ -10,13 +10,15 @@ export class MaterialService {
|
||||||
|
|
||||||
getMaterialCodes(
|
getMaterialCodes(
|
||||||
matIds?: number[],
|
matIds?: number[],
|
||||||
version?: string
|
country?: string,
|
||||||
|
filename?: string
|
||||||
): Observable<MaterialCode[]> {
|
): Observable<MaterialCode[]> {
|
||||||
return this._httpClient.get<MaterialCode[]>(
|
return this._httpClient.get<MaterialCode[]>(
|
||||||
`${environment.api}/materials/code`,
|
`${environment.api}/materials/code`,
|
||||||
{
|
{
|
||||||
params: {
|
params: {
|
||||||
version: version || '',
|
country: country || '',
|
||||||
|
filename: filename || '',
|
||||||
mat_ids: matIds?.join(',') || '',
|
mat_ids: matIds?.join(',') || '',
|
||||||
},
|
},
|
||||||
withCredentials: true,
|
withCredentials: true,
|
||||||
|
|
@ -26,13 +28,15 @@ export class MaterialService {
|
||||||
|
|
||||||
getMaterialSettingById(
|
getMaterialSettingById(
|
||||||
id: number,
|
id: number,
|
||||||
version?: string
|
country?: string,
|
||||||
|
filename?: string
|
||||||
): Observable<MaterialSetting> {
|
): Observable<MaterialSetting> {
|
||||||
return this._httpClient.get<MaterialSetting>(
|
return this._httpClient.get<MaterialSetting>(
|
||||||
`${environment.api}/materials/setting/${id}`,
|
`${environment.api}/materials/setting/${id}`,
|
||||||
{
|
{
|
||||||
params: {
|
params: {
|
||||||
version: version || '',
|
country: country || '',
|
||||||
|
filename: filename || '',
|
||||||
},
|
},
|
||||||
withCredentials: true,
|
withCredentials: true,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,8 @@ import { environment } from 'src/environments/environment';
|
||||||
import { RecipeMetaData } from 'src/app/shared/types/recipe';
|
import { RecipeMetaData } from 'src/app/shared/types/recipe';
|
||||||
|
|
||||||
interface RecipeParams {
|
interface RecipeParams {
|
||||||
version: string;
|
filename: string;
|
||||||
|
country: string;
|
||||||
offset: number;
|
offset: number;
|
||||||
take: number;
|
take: number;
|
||||||
search: string;
|
search: string;
|
||||||
|
|
@ -28,46 +29,49 @@ export class RecipeService {
|
||||||
take: 10,
|
take: 10,
|
||||||
offset: 0,
|
offset: 0,
|
||||||
search: '',
|
search: '',
|
||||||
version: this.getCurrentVersion(),
|
country: this.getCurrentCountry(),
|
||||||
|
filename: this.getCurrentFile(),
|
||||||
}
|
}
|
||||||
): Observable<{
|
): Observable<{
|
||||||
fileName: string;
|
fileName: string;
|
||||||
recipes: Recipe;
|
recipes: Recipe;
|
||||||
hasMore: boolean;
|
hasMore: boolean;
|
||||||
}> {
|
}> {
|
||||||
return this._httpClient
|
return this._httpClient.get<{
|
||||||
.get<{
|
fileName: string;
|
||||||
fileName: string;
|
recipes: Recipe;
|
||||||
recipes: Recipe;
|
hasMore: boolean;
|
||||||
hasMore: boolean;
|
}>(environment.api + '/recipes', {
|
||||||
}>(environment.api + '/recipes', {
|
params: {
|
||||||
params: {
|
offset: params.offset,
|
||||||
offset: params.offset,
|
take: params.take,
|
||||||
take: params.take,
|
search: params.search,
|
||||||
search: params.search,
|
country: params.country,
|
||||||
version: params.version,
|
filename: params.filename,
|
||||||
},
|
},
|
||||||
withCredentials: true,
|
withCredentials: true,
|
||||||
responseType: 'json',
|
responseType: 'json',
|
||||||
})
|
});
|
||||||
.pipe(
|
|
||||||
tap((data) => {
|
|
||||||
if (data.fileName !== this.getCurrentVersion()) {
|
|
||||||
localStorage.setItem('currentRecipeVersion', data.fileName);
|
|
||||||
}
|
|
||||||
})
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
getCurrentVersion(): string {
|
getCurrentFile(): string {
|
||||||
const currentRecipeVersion = localStorage.getItem('currentRecipeVersion');
|
const currentRecipeFile = localStorage.getItem('currentRecipeFile');
|
||||||
if (currentRecipeVersion) {
|
if (currentRecipeFile) {
|
||||||
return currentRecipeVersion;
|
return currentRecipeFile;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 'coffeethai02_580.json';
|
return 'coffeethai02_580.json';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getCurrentCountry(): string {
|
||||||
|
const currentRecipeCountry = localStorage.getItem('currentRecipeCountry');
|
||||||
|
if (currentRecipeCountry) {
|
||||||
|
return currentRecipeCountry;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 'Thailand';
|
||||||
|
}
|
||||||
|
|
||||||
getRecipesById(id: string): Observable<{
|
getRecipesById(id: string): Observable<{
|
||||||
recipe: Recipe01;
|
recipe: Recipe01;
|
||||||
recipeMetaData: RecipeMetaData;
|
recipeMetaData: RecipeMetaData;
|
||||||
|
|
@ -103,23 +107,28 @@ export class RecipeService {
|
||||||
return this.countries;
|
return this.countries;
|
||||||
}
|
}
|
||||||
|
|
||||||
getRecipeFileNames(country: string): string[] {
|
getRecipeFileNames(country: string): string[] | null {
|
||||||
return this.recipeFiles[country];
|
return this.recipeFiles[country] ?? null;
|
||||||
}
|
}
|
||||||
|
|
||||||
editChanges(version: string, change: any){
|
editChanges(country: string, filename: string, change: any) {
|
||||||
console.log("target version = ", version);
|
console.log('target version = ', filename);
|
||||||
console.log("change in edit: ",change.value)
|
console.log('change in edit: ', change.value);
|
||||||
return this._httpClient.post<{
|
return this._httpClient
|
||||||
status: string
|
.post<{
|
||||||
}>(
|
status: string;
|
||||||
environment.api + ('/recipes/edit/'+version),
|
}>(
|
||||||
change.value,
|
environment.api + ('/recipes/edit/' + country + '/' + filename),
|
||||||
{ withCredentials: true, responseType: 'json', }
|
change.value,
|
||||||
).subscribe({
|
{
|
||||||
next(value) {
|
withCredentials: true,
|
||||||
console.log(value, change.value)
|
responseType: 'json',
|
||||||
},
|
}
|
||||||
});
|
)
|
||||||
|
.subscribe({
|
||||||
|
next(value) {
|
||||||
|
console.log(value, change.value);
|
||||||
|
},
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -188,12 +188,13 @@ export class RecipeDetailsComponent implements OnInit {
|
||||||
console.log('confirm save');
|
console.log('confirm save');
|
||||||
// TODO: update value in targeted recipe
|
// TODO: update value in targeted recipe
|
||||||
this._recipeService.editChanges(
|
this._recipeService.editChanges(
|
||||||
this._recipeService.getCurrentVersion(),
|
this._recipeService.getCurrentCountry(),
|
||||||
|
this._recipeService.getCurrentFile(),
|
||||||
{
|
{
|
||||||
...this.recipeDetail
|
...this.recipeDetail,
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
console.log("Sending changes")
|
console.log('Sending changes');
|
||||||
this._router.navigate(['/recipes']);
|
this._router.navigate(['/recipes']);
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -27,11 +27,12 @@
|
||||||
<div class="flex flex-row gap-5">
|
<div class="flex flex-row gap-5">
|
||||||
<div class="dropdown dropdown-end">
|
<div class="dropdown dropdown-end">
|
||||||
<input
|
<input
|
||||||
type="text"
|
#countryInput
|
||||||
tabindex="0"
|
type="search"
|
||||||
|
tabindex="1"
|
||||||
placeholder="Select Country"
|
placeholder="Select Country"
|
||||||
class="input input-bordered input-sm w-full max-w-xs"
|
class="input input-bordered input-sm w-full max-w-xs"
|
||||||
[value]="currentCountryFilter.getValue()"
|
[value]="selectedCountry"
|
||||||
(input)="setCountryFilter($event)"
|
(input)="setCountryFilter($event)"
|
||||||
(focus)="getRecipeCountries()"
|
(focus)="getRecipeCountries()"
|
||||||
/>
|
/>
|
||||||
|
|
@ -39,7 +40,7 @@
|
||||||
class="dropdown-content z-[1000] min-w-[200px] max-h-[500px] overflow-y-auto"
|
class="dropdown-content z-[1000] min-w-[200px] max-h-[500px] overflow-y-auto"
|
||||||
>
|
>
|
||||||
<ul
|
<ul
|
||||||
tabindex="0"
|
tabindex="1"
|
||||||
class="menu p-2 shadow bg-base-100 rounded-box w-auto"
|
class="menu p-2 shadow bg-base-100 rounded-box w-auto"
|
||||||
>
|
>
|
||||||
<li *ngFor="let country of recipeCountryFiltered">
|
<li *ngFor="let country of recipeCountryFiltered">
|
||||||
|
|
@ -52,12 +53,13 @@
|
||||||
</div>
|
</div>
|
||||||
<div *ngIf="isCountrySelected" class="dropdown dropdown-end">
|
<div *ngIf="isCountrySelected" class="dropdown dropdown-end">
|
||||||
<input
|
<input
|
||||||
type="text"
|
#fileInput
|
||||||
|
type="search"
|
||||||
tabindex="1"
|
tabindex="1"
|
||||||
placeholder="เลือก Recipe File"
|
placeholder="เลือก Recipe File"
|
||||||
class="input input-bordered input-sm w-full max-w-xs"
|
class="input input-bordered input-sm w-full max-w-xs"
|
||||||
(input)="setFileFilter($event)"
|
(input)="setFileFilter($event)"
|
||||||
(focus)="getRecipeFileCountries()"
|
(focus)="getRecipeFiles()"
|
||||||
/>
|
/>
|
||||||
<div
|
<div
|
||||||
class="dropdown-content z-[1000] min-w-[200px] max-h-[500px] overflow-y-auto"
|
class="dropdown-content z-[1000] min-w-[200px] max-h-[500px] overflow-y-auto"
|
||||||
|
|
|
||||||
|
|
@ -66,7 +66,8 @@ export class DashboardComponent implements OnInit, OnDestroy {
|
||||||
offset: this.offset,
|
offset: this.offset,
|
||||||
take: this.take,
|
take: this.take,
|
||||||
search: this.oldSearchStr,
|
search: this.oldSearchStr,
|
||||||
version: this._recipeService.getCurrentVersion(),
|
filename: this._recipeService.getCurrentFile(),
|
||||||
|
country: this._recipeService.getCurrentCountry(),
|
||||||
})
|
})
|
||||||
.subscribe(({ recipes, hasMore, fileName }) => {
|
.subscribe(({ recipes, hasMore, fileName }) => {
|
||||||
const { Recipe01, ...recipesWithoutRecipe01 } = recipes;
|
const { Recipe01, ...recipesWithoutRecipe01 } = recipes;
|
||||||
|
|
@ -98,7 +99,8 @@ export class DashboardComponent implements OnInit, OnDestroy {
|
||||||
offset: this.offset,
|
offset: this.offset,
|
||||||
take: this.take,
|
take: this.take,
|
||||||
search: this.oldSearchStr,
|
search: this.oldSearchStr,
|
||||||
version: this._recipeService.getCurrentVersion(),
|
filename: this._recipeService.getCurrentFile(),
|
||||||
|
country: this._recipeService.getCurrentCountry(),
|
||||||
})
|
})
|
||||||
.subscribe(({ recipes, hasMore, fileName }) => {
|
.subscribe(({ recipes, hasMore, fileName }) => {
|
||||||
const { Recipe01, ...recipesWithoutRecipe01 } = recipes;
|
const { Recipe01, ...recipesWithoutRecipe01 } = recipes;
|
||||||
|
|
@ -132,7 +134,8 @@ export class DashboardComponent implements OnInit, OnDestroy {
|
||||||
offset: this.offset,
|
offset: this.offset,
|
||||||
take: this.take,
|
take: this.take,
|
||||||
search: this.searchStr,
|
search: this.searchStr,
|
||||||
version: this._recipeService.getCurrentVersion(),
|
filename: this._recipeService.getCurrentFile(),
|
||||||
|
country: this._recipeService.getCurrentCountry(),
|
||||||
})
|
})
|
||||||
.subscribe(({ recipes, hasMore, fileName }) => {
|
.subscribe(({ recipes, hasMore, fileName }) => {
|
||||||
const { Recipe01, ...recipesWithoutRecipe01 } = recipes;
|
const { Recipe01, ...recipesWithoutRecipe01 } = recipes;
|
||||||
|
|
@ -163,6 +166,23 @@ export class DashboardComponent implements OnInit, OnDestroy {
|
||||||
selectedCountry: string | null = null;
|
selectedCountry: string | null = null;
|
||||||
isCountrySelected: boolean = false;
|
isCountrySelected: boolean = false;
|
||||||
|
|
||||||
|
private countryInputEl?: ElementRef<HTMLInputElement>;
|
||||||
|
private fileInputEl?: ElementRef<HTMLInputElement>;
|
||||||
|
|
||||||
|
@ViewChild('countryInput', { static: false }) set countryInput(
|
||||||
|
countryInput: ElementRef<HTMLInputElement>
|
||||||
|
) {
|
||||||
|
this.countryInputEl = countryInput;
|
||||||
|
}
|
||||||
|
|
||||||
|
@ViewChild('fileInput', { static: false }) set fileInput(
|
||||||
|
fileInput: ElementRef<HTMLInputElement>
|
||||||
|
) {
|
||||||
|
this.fileInputEl = fileInput;
|
||||||
|
}
|
||||||
|
|
||||||
|
private firstTimeOpenModal = true;
|
||||||
|
|
||||||
initRecipeSelection() {
|
initRecipeSelection() {
|
||||||
if (this._recipeService.getRecipeFileCountries().length == 0) {
|
if (this._recipeService.getRecipeFileCountries().length == 0) {
|
||||||
this._recipeService.getRecipeCountries().subscribe((countries) => {
|
this._recipeService.getRecipeCountries().subscribe((countries) => {
|
||||||
|
|
@ -180,27 +200,46 @@ export class DashboardComponent implements OnInit, OnDestroy {
|
||||||
}
|
}
|
||||||
|
|
||||||
getRecipeCountries() {
|
getRecipeCountries() {
|
||||||
|
if (this.firstTimeOpenModal) {
|
||||||
|
this.countryInputEl!.nativeElement.blur();
|
||||||
|
this.firstTimeOpenModal = false;
|
||||||
|
}
|
||||||
this.currentCountryFilterSubScription = this.currentCountryFilter.subscribe(
|
this.currentCountryFilterSubScription = this.currentCountryFilter.subscribe(
|
||||||
(c) => {
|
(c) => {
|
||||||
const countries = this._recipeService.getRecipeFileCountries();
|
const countries = this._recipeService.getRecipeFileCountries();
|
||||||
if (countries.length > 0) {
|
if (countries.length > 0) {
|
||||||
this.recipeFileCountries = lodash.filter(countries, (country) =>
|
this.recipeCountryFiltered = lodash.filter(countries, (country) =>
|
||||||
country.includes(c)
|
country.toLowerCase().includes(c.toLowerCase())
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
getRecipeFileCountries() {
|
getRecipeFiles() {
|
||||||
this.currentFileFilterSubScription = this.currentFileFilter.subscribe(
|
this.currentFileFilterSubScription = this.currentFileFilter.subscribe(
|
||||||
(c) => {
|
(c) => {
|
||||||
const countries = this._recipeService.getRecipeFileCountries();
|
if (this.selectedCountry === null) {
|
||||||
if (countries.length > 0) {
|
return;
|
||||||
this.recipeCountryFiltered = lodash.filter(countries, (country) =>
|
|
||||||
country.includes(c)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const fileNames = this._recipeService.getRecipeFileNames(
|
||||||
|
this.selectedCountry
|
||||||
|
);
|
||||||
|
if (fileNames && fileNames.length > 0) {
|
||||||
|
this.recipeFileCountries = lodash.filter(fileNames, (file) =>
|
||||||
|
file.toLowerCase().includes(c.toLowerCase())
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
this._recipeService
|
||||||
|
.getRecipeFiles(this.selectedCountry)
|
||||||
|
.subscribe((files) => {
|
||||||
|
this.recipeFileCountries = lodash.filter(files, (file) =>
|
||||||
|
file.toLowerCase().includes(c.toLowerCase())
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
console.log(this.recipeFileCountries);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
@ -208,6 +247,7 @@ export class DashboardComponent implements OnInit, OnDestroy {
|
||||||
countrySelected(country: string) {
|
countrySelected(country: string) {
|
||||||
this.selectedCountry = country;
|
this.selectedCountry = country;
|
||||||
this.isCountrySelected = true;
|
this.isCountrySelected = true;
|
||||||
|
localStorage.setItem('currentRecipeCountry', country);
|
||||||
}
|
}
|
||||||
|
|
||||||
loadRecipe(recipeFileName: string) {
|
loadRecipe(recipeFileName: string) {
|
||||||
|
|
@ -219,13 +259,15 @@ export class DashboardComponent implements OnInit, OnDestroy {
|
||||||
this.isHasMore = true;
|
this.isHasMore = true;
|
||||||
this.isLoadMore = false;
|
this.isLoadMore = false;
|
||||||
this.oldSearchStr = '';
|
this.oldSearchStr = '';
|
||||||
|
localStorage.setItem('currentRecipeFile', recipeFileName);
|
||||||
|
|
||||||
this._recipeService
|
this._recipeService
|
||||||
.getRecipes({
|
.getRecipes({
|
||||||
offset: this.offset,
|
offset: this.offset,
|
||||||
take: this.take,
|
take: this.take,
|
||||||
search: this.oldSearchStr,
|
search: this.oldSearchStr,
|
||||||
version: recipeFileName,
|
filename: recipeFileName,
|
||||||
|
country: this.selectedCountry!,
|
||||||
})
|
})
|
||||||
.subscribe(({ recipes, hasMore, fileName }) => {
|
.subscribe(({ recipes, hasMore, fileName }) => {
|
||||||
const { Recipe01, ...recipesWithoutRecipe01 } = recipes;
|
const { Recipe01, ...recipesWithoutRecipe01 } = recipes;
|
||||||
|
|
@ -249,8 +291,7 @@ export class DashboardComponent implements OnInit, OnDestroy {
|
||||||
|
|
||||||
openJsonTab() {
|
openJsonTab() {
|
||||||
window.open(
|
window.open(
|
||||||
environment.api +
|
environment.api + `/recipes/${this._recipeService.getCurrentFile()}/json`,
|
||||||
`/recipes/${this._recipeService.getCurrentVersion()}/json`,
|
|
||||||
'_blank'
|
'_blank'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,9 +3,6 @@ package data
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
"recipe-manager/helpers"
|
"recipe-manager/helpers"
|
||||||
"recipe-manager/models"
|
"recipe-manager/models"
|
||||||
"recipe-manager/services/logger"
|
"recipe-manager/services/logger"
|
||||||
|
|
@ -18,40 +15,18 @@ var (
|
||||||
Log = logger.GetInstance()
|
Log = logger.GetInstance()
|
||||||
)
|
)
|
||||||
|
|
||||||
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)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
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 nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return data
|
|
||||||
}
|
|
||||||
|
|
||||||
type RecipeWithTimeStamps struct {
|
type RecipeWithTimeStamps struct {
|
||||||
Recipe models.Recipe
|
Recipe models.Recipe
|
||||||
TimeStamps int64
|
TimeStamps int64
|
||||||
}
|
}
|
||||||
|
|
||||||
type Data struct {
|
type Data struct {
|
||||||
CurrentVersion string
|
CurrentFile string
|
||||||
AllRecipeFiles map[string][]helpers.RecipePath
|
CurrentCountryID string
|
||||||
currentRecipe *models.Recipe
|
AllRecipeFiles map[string][]helpers.RecipePath
|
||||||
recipeMap map[string]RecipeWithTimeStamps
|
currentRecipe *models.Recipe
|
||||||
Countries []helpers.CountryName
|
recipeMap map[string]RecipeWithTimeStamps
|
||||||
|
Countries []helpers.CountryName
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewData() *Data {
|
func NewData() *Data {
|
||||||
|
|
@ -70,15 +45,17 @@ func NewData() *Data {
|
||||||
|
|
||||||
allRecipeFiles := helpers.ScanRecipeFiles(countries)
|
allRecipeFiles := helpers.ScanRecipeFiles(countries)
|
||||||
|
|
||||||
defaultVersion := "coffeethai02_580.json"
|
defaultFile := "coffeethai02_580.json"
|
||||||
defaultRecipe := readFile(defaultVersion)
|
defaultCountry := "thai"
|
||||||
|
defaultRecipe := helpers.ReadRecipeFile(defaultCountry, defaultFile)
|
||||||
|
|
||||||
return &Data{
|
return &Data{
|
||||||
CurrentVersion: defaultVersion,
|
CurrentFile: defaultFile,
|
||||||
AllRecipeFiles: allRecipeFiles,
|
CurrentCountryID: defaultCountry,
|
||||||
currentRecipe: defaultRecipe,
|
AllRecipeFiles: allRecipeFiles,
|
||||||
|
currentRecipe: defaultRecipe,
|
||||||
recipeMap: map[string]RecipeWithTimeStamps{
|
recipeMap: map[string]RecipeWithTimeStamps{
|
||||||
defaultVersion: {
|
defaultFile: {
|
||||||
Recipe: *defaultRecipe,
|
Recipe: *defaultRecipe,
|
||||||
TimeStamps: time.Now().Unix(),
|
TimeStamps: time.Now().Unix(),
|
||||||
},
|
},
|
||||||
|
|
@ -87,22 +64,26 @@ func NewData() *Data {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Data) GetRecipe(version string) models.Recipe {
|
func (d *Data) GetRecipe(countryID, filename string) models.Recipe {
|
||||||
|
|
||||||
if version == "" || version == d.CurrentVersion {
|
if countryID == "" {
|
||||||
return *d.currentRecipe
|
return *d.currentRecipe
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Println("Change recipe to version:", version)
|
if filename == "" || filename == d.CurrentFile {
|
||||||
|
return *d.currentRecipe
|
||||||
|
}
|
||||||
|
|
||||||
if recipe, ok := d.recipeMap[version]; ok {
|
if recipe, ok := d.recipeMap[filename]; ok {
|
||||||
d.CurrentVersion = version
|
d.CurrentFile = filename
|
||||||
|
d.CurrentCountryID = countryID
|
||||||
return recipe.Recipe
|
return recipe.Recipe
|
||||||
}
|
}
|
||||||
|
|
||||||
// change current version and read new recipe
|
// change current version and read new recipe
|
||||||
d.CurrentVersion = version
|
d.CurrentFile = filename
|
||||||
d.currentRecipe = readFile(version)
|
d.CurrentCountryID = countryID
|
||||||
|
d.currentRecipe = helpers.ReadRecipeFile(countryID, filename)
|
||||||
|
|
||||||
// save to map
|
// save to map
|
||||||
if len(d.recipeMap) > 5 { // limit keep in memory 5 version
|
if len(d.recipeMap) > 5 { // limit keep in memory 5 version
|
||||||
|
|
@ -118,7 +99,7 @@ func (d *Data) GetRecipe(version string) models.Recipe {
|
||||||
delete(d.recipeMap, oldestVersion)
|
delete(d.recipeMap, oldestVersion)
|
||||||
}
|
}
|
||||||
|
|
||||||
d.recipeMap[version] = RecipeWithTimeStamps{
|
d.recipeMap[filename] = RecipeWithTimeStamps{
|
||||||
Recipe: *d.currentRecipe,
|
Recipe: *d.currentRecipe,
|
||||||
TimeStamps: time.Now().Unix(),
|
TimeStamps: time.Now().Unix(),
|
||||||
}
|
}
|
||||||
|
|
@ -151,22 +132,29 @@ func (d *Data) SetValuesToRecipe(recipe models.Recipe01) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Data) GetMaterialSetting(version string) []models.MaterialSetting {
|
func (d *Data) GetMaterialSetting(countryID, filename string) []models.MaterialSetting {
|
||||||
result := make([]models.MaterialSetting, 0)
|
result := make([]models.MaterialSetting, 0)
|
||||||
|
|
||||||
if version == "" || version == d.CurrentVersion {
|
if countryID == "" {
|
||||||
copy(result, d.currentRecipe.MaterialSetting)
|
copy(result, d.currentRecipe.MaterialSetting)
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
if recipe, ok := d.recipeMap[version]; ok {
|
if filename == "" || filename == d.CurrentFile {
|
||||||
copy(result, recipe.Recipe.MaterialSetting)
|
copy(result, d.currentRecipe.MaterialSetting)
|
||||||
d.CurrentVersion = version
|
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
d.CurrentVersion = version
|
if recipe, ok := d.recipeMap[filename]; ok {
|
||||||
d.currentRecipe = readFile(version)
|
copy(result, recipe.Recipe.MaterialSetting)
|
||||||
|
d.CurrentFile = filename
|
||||||
|
d.CurrentCountryID = countryID
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
d.CurrentFile = filename
|
||||||
|
d.CurrentCountryID = countryID
|
||||||
|
d.currentRecipe = helpers.ReadRecipeFile(countryID, filename)
|
||||||
|
|
||||||
// save to map
|
// save to map
|
||||||
if len(d.recipeMap) > 5 { // limit keep in memory 5 version
|
if len(d.recipeMap) > 5 { // limit keep in memory 5 version
|
||||||
|
|
@ -182,7 +170,7 @@ func (d *Data) GetMaterialSetting(version string) []models.MaterialSetting {
|
||||||
delete(d.recipeMap, oldestVersion)
|
delete(d.recipeMap, oldestVersion)
|
||||||
}
|
}
|
||||||
|
|
||||||
d.recipeMap[version] = RecipeWithTimeStamps{
|
d.recipeMap[filename] = RecipeWithTimeStamps{
|
||||||
Recipe: *d.currentRecipe,
|
Recipe: *d.currentRecipe,
|
||||||
TimeStamps: time.Now().Unix(),
|
TimeStamps: time.Now().Unix(),
|
||||||
}
|
}
|
||||||
|
|
@ -191,17 +179,18 @@ func (d *Data) GetMaterialSetting(version string) []models.MaterialSetting {
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Data) GetMaterialCode(ids []uint64, version string) []models.MaterialCode {
|
func (d *Data) GetMaterialCode(ids []uint64, countryID, filename string) []models.MaterialCode {
|
||||||
var result []models.MaterialCode
|
var result []models.MaterialCode
|
||||||
|
|
||||||
if version == "" || version == d.CurrentVersion {
|
if filename == "" || filename == d.CurrentFile {
|
||||||
result = d.currentRecipe.MaterialCode
|
result = d.currentRecipe.MaterialCode
|
||||||
} else if recipe, ok := d.recipeMap[version]; ok {
|
} else if recipe, ok := d.recipeMap[filename]; ok {
|
||||||
d.CurrentVersion = version
|
d.CurrentFile = filename
|
||||||
return recipe.Recipe.MaterialCode
|
return recipe.Recipe.MaterialCode
|
||||||
} else {
|
} else {
|
||||||
d.CurrentVersion = version
|
d.CurrentFile = filename
|
||||||
d.currentRecipe = readFile(version)
|
d.CurrentCountryID = countryID
|
||||||
|
d.currentRecipe = helpers.ReadRecipeFile(countryID, filename)
|
||||||
|
|
||||||
// save to map
|
// save to map
|
||||||
if len(d.recipeMap) > 5 { // limit keep in memory 5 version
|
if len(d.recipeMap) > 5 { // limit keep in memory 5 version
|
||||||
|
|
@ -217,7 +206,7 @@ func (d *Data) GetMaterialCode(ids []uint64, version string) []models.MaterialCo
|
||||||
delete(d.recipeMap, oldestVersion)
|
delete(d.recipeMap, oldestVersion)
|
||||||
}
|
}
|
||||||
|
|
||||||
d.recipeMap[version] = RecipeWithTimeStamps{
|
d.recipeMap[filename] = RecipeWithTimeStamps{
|
||||||
Recipe: *d.currentRecipe,
|
Recipe: *d.currentRecipe,
|
||||||
TimeStamps: time.Now().Unix(),
|
TimeStamps: time.Now().Unix(),
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"recipe-manager/models"
|
"recipe-manager/models"
|
||||||
"sort"
|
"sort"
|
||||||
|
|
@ -28,23 +29,28 @@ func ReadFile(filePath string) (string, error) {
|
||||||
return string(file), nil
|
return string(file), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func ReadRecipeFile(filePath string) models.Recipe {
|
func ReadRecipeFile(countryID, filePath string) *models.Recipe {
|
||||||
file, err := os.Open(filePath)
|
|
||||||
|
if countryID == "thai" {
|
||||||
|
countryID = ""
|
||||||
|
}
|
||||||
|
|
||||||
|
file, err := os.Open(path.Join("cofffeemachineConfig", countryID, filePath))
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Error when open file: %s", err)
|
log.Fatalf("Error when open file: %s", err)
|
||||||
return models.Recipe{}
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
|
|
||||||
var data models.Recipe
|
var data *models.Recipe
|
||||||
|
|
||||||
err = json.NewDecoder(file).Decode(&data)
|
err = json.NewDecoder(file).Decode(&data)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Error when decode file: %s", err)
|
log.Fatalf("Error when decode file: %s", err)
|
||||||
return models.Recipe{}
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return data
|
return data
|
||||||
|
|
|
||||||
|
|
@ -57,7 +57,7 @@ type Recipe01 struct {
|
||||||
OnTOP bool `json:"OnTOP"`
|
OnTOP bool `json:"OnTOP"`
|
||||||
LastChange string `json:"LastChange"`
|
LastChange string `json:"LastChange"`
|
||||||
MenuStatus int `json:"MenuStatus"`
|
MenuStatus int `json:"MenuStatus"`
|
||||||
RemainingCups string `json:"RemainingCups"`
|
RemainingCups int `json:"RemainingCups"`
|
||||||
StringParam string `json:"StringParam"`
|
StringParam string `json:"StringParam"`
|
||||||
TextForWarningBeforePay []string `json:"TextForWarningBeforePay"`
|
TextForWarningBeforePay []string `json:"TextForWarningBeforePay"`
|
||||||
CashPrice int `json:"cashPrice"`
|
CashPrice int `json:"cashPrice"`
|
||||||
|
|
|
||||||
|
|
@ -26,7 +26,8 @@ func (mr *MaterialRouter) Route(r chi.Router) {
|
||||||
r.Get("/code", func(w http.ResponseWriter, r *http.Request) {
|
r.Get("/code", func(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Header().Add("Content-Type", "application/json")
|
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")
|
matIDs := r.URL.Query().Get("mat_ids")
|
||||||
|
|
||||||
|
|
@ -41,7 +42,14 @@ func (mr *MaterialRouter) Route(r chi.Router) {
|
||||||
matIDsUint = append(matIDsUint, matIDUint)
|
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)
|
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) {
|
r.Get("/setting/{mat_id}", func(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Header().Add("Content-Type", "application/json")
|
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")
|
matID := chi.URLParam(r, "mat_id")
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"path"
|
||||||
"recipe-manager/data"
|
"recipe-manager/data"
|
||||||
"recipe-manager/models"
|
"recipe-manager/models"
|
||||||
"recipe-manager/services/logger"
|
"recipe-manager/services/logger"
|
||||||
|
|
@ -47,9 +48,17 @@ func (rr *RecipeRouter) Route(r chi.Router) {
|
||||||
take = newTake
|
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")
|
searchQuery := r.URL.Query().Get("search")
|
||||||
|
|
||||||
if searchQuery != "" {
|
if searchQuery != "" {
|
||||||
|
|
@ -76,7 +85,7 @@ func (rr *RecipeRouter) Route(r chi.Router) {
|
||||||
}
|
}
|
||||||
|
|
||||||
json.NewEncoder(w).Encode(map[string]interface{}{
|
json.NewEncoder(w).Encode(map[string]interface{}{
|
||||||
"fileName": rr.data.CurrentVersion,
|
"fileName": rr.data.CurrentFile,
|
||||||
"recipes": recipe,
|
"recipes": recipe,
|
||||||
"hasMore": isHasMore,
|
"hasMore": isHasMore,
|
||||||
})
|
})
|
||||||
|
|
@ -124,11 +133,17 @@ func (rr *RecipeRouter) Route(r chi.Router) {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
r.Get("/{version}/json", func(w http.ResponseWriter, r *http.Request) {
|
r.Get("/{country}/{filename}/json", func(w http.ResponseWriter, r *http.Request) {
|
||||||
version := chi.URLParam(r, "version")
|
country := chi.URLParam(r, "country")
|
||||||
|
filename := chi.URLParam(r, "filename")
|
||||||
|
|
||||||
w.Header().Add("Content-Type", "application/json")
|
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) {
|
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)
|
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))
|
Log.Debug("Edit: ", zap.String("path", r.RequestURI))
|
||||||
version := chi.URLParam(r, "version")
|
filename := chi.URLParam(r, "filename")
|
||||||
target_recipe := rr.data.GetRecipe(version)
|
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))
|
Log.Debug("Target => ", zap.Any("target", target_recipe.MachineSetting.ConfigNumber))
|
||||||
|
|
||||||
// Body
|
// Body
|
||||||
var changes models.Recipe01
|
var changes models.Recipe01
|
||||||
err := json.NewDecoder(r.Body).Decode(&changes)
|
err = json.NewDecoder(r.Body).Decode(&changes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Log.Error("Decode in request failed: ", zap.Error(err))
|
Log.Error("Decode in request failed: ", zap.Error(err))
|
||||||
}
|
}
|
||||||
|
|
@ -219,7 +242,7 @@ func (rr *RecipeRouter) Route(r chi.Router) {
|
||||||
return
|
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")
|
w.Header().Add("Content-Type", "application/json")
|
||||||
json.NewEncoder(w).Encode(map[string]interface{}{
|
json.NewEncoder(w).Encode(map[string]interface{}{
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue