display commit at recipe
This commit is contained in:
parent
820557a268
commit
f2ec0ed5fa
6 changed files with 640 additions and 555 deletions
|
|
@ -1,194 +1,224 @@
|
|||
import { HttpClient } from '@angular/common/http';
|
||||
import { Injectable } from '@angular/core';
|
||||
import { Observable, tap } from 'rxjs';
|
||||
import {
|
||||
Recipe,
|
||||
Recipe01,
|
||||
RecipeDetail,
|
||||
RecipeDetailMat,
|
||||
RecipeOverview,
|
||||
RecipeOverviewList,
|
||||
RecipesDashboard,
|
||||
} from '../models/recipe.model';
|
||||
import { environment } from 'src/environments/environment';
|
||||
import { RecipeMetaData } from 'src/app/shared/types/recipe';
|
||||
|
||||
type RecipeOverviewParams = {
|
||||
filename: string;
|
||||
country: string;
|
||||
materialIds: number[];
|
||||
offset: number;
|
||||
take: number;
|
||||
search: string;
|
||||
};
|
||||
|
||||
type RecipeDashboardParams = {
|
||||
filename: string;
|
||||
country: string;
|
||||
};
|
||||
|
||||
interface RecipeFiles {
|
||||
[key: string]: string[];
|
||||
}
|
||||
|
||||
@Injectable({ providedIn: 'root' })
|
||||
export class RecipeService {
|
||||
private countries: string[] = [];
|
||||
private recipeFiles: RecipeFiles = {};
|
||||
|
||||
constructor(private _httpClient: HttpClient) {}
|
||||
|
||||
getRecipesDashboard(
|
||||
params: RecipeDashboardParams = {
|
||||
country: this.getCurrentCountry(),
|
||||
filename: this.getCurrentFile(),
|
||||
}
|
||||
): Observable<RecipesDashboard> {
|
||||
return this._httpClient.get<RecipesDashboard>(
|
||||
environment.api + '/recipes/dashboard',
|
||||
{
|
||||
params: {
|
||||
country: params.country,
|
||||
filename: params.filename,
|
||||
},
|
||||
withCredentials: true,
|
||||
responseType: 'json',
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
getRecipeOverview(
|
||||
params: RecipeOverviewParams = {
|
||||
country: this.getCurrentCountry(),
|
||||
filename: this.getCurrentFile(),
|
||||
materialIds: [],
|
||||
offset: 0,
|
||||
take: 20,
|
||||
search: '',
|
||||
}
|
||||
): Observable<RecipeOverviewList> {
|
||||
return this._httpClient.get<RecipeOverviewList>(
|
||||
environment.api + '/recipes/overview',
|
||||
{
|
||||
params: {
|
||||
country: params.country,
|
||||
filename: params.filename,
|
||||
materialIds: params.materialIds.join(','),
|
||||
offset: params.offset.toString(),
|
||||
take: params.take.toString(),
|
||||
search: params.search,
|
||||
},
|
||||
withCredentials: true,
|
||||
responseType: 'json',
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
getRecipeDetail(productCode: string): Observable<RecipeDetail> {
|
||||
return this._httpClient.get<RecipeDetail>(
|
||||
environment.api + '/recipes/' + productCode,
|
||||
{
|
||||
params: {
|
||||
filename: this.getCurrentFile(),
|
||||
country: this.getCurrentCountry(),
|
||||
},
|
||||
withCredentials: true,
|
||||
responseType: 'json',
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
getRecipeDetailMat(
|
||||
productCode: string
|
||||
): Observable<{ result: RecipeDetailMat[] }> {
|
||||
return this._httpClient.get<{ result: RecipeDetailMat[] }>(
|
||||
environment.api + '/recipes/' + productCode + '/mat',
|
||||
{
|
||||
params: {
|
||||
filename: this.getCurrentFile(),
|
||||
country: this.getCurrentCountry(),
|
||||
},
|
||||
withCredentials: true,
|
||||
responseType: 'json',
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
getCurrentFile(): string {
|
||||
const currentRecipeFile = localStorage.getItem('currentRecipeFile');
|
||||
if (currentRecipeFile) {
|
||||
return currentRecipeFile;
|
||||
}
|
||||
|
||||
return 'coffeethai02_580.json';
|
||||
}
|
||||
|
||||
getCurrentCountry(): string {
|
||||
const currentRecipeCountry = localStorage.getItem('currentRecipeCountry');
|
||||
if (currentRecipeCountry) {
|
||||
return currentRecipeCountry;
|
||||
}
|
||||
|
||||
return 'Thailand';
|
||||
}
|
||||
|
||||
getRecipesById(id: string): Observable<{
|
||||
recipe: Recipe01;
|
||||
recipeMetaData: RecipeMetaData;
|
||||
}> {
|
||||
return this._httpClient.get<{
|
||||
recipe: Recipe01;
|
||||
recipeMetaData: RecipeMetaData;
|
||||
}>(environment.api + '/recipes/' + id, {
|
||||
withCredentials: true,
|
||||
responseType: 'json',
|
||||
});
|
||||
}
|
||||
|
||||
getRecipeCountries(): Observable<string[]> {
|
||||
return this._httpClient
|
||||
.get<string[]>(environment.api + '/recipes/versions', {
|
||||
withCredentials: true,
|
||||
responseType: 'json',
|
||||
})
|
||||
.pipe(tap((countries) => (this.countries = countries)));
|
||||
}
|
||||
|
||||
getRecipeFiles(country: string): Observable<string[]> {
|
||||
return this._httpClient
|
||||
.get<string[]>(environment.api + '/recipes/versions/' + country, {
|
||||
withCredentials: true,
|
||||
responseType: 'json',
|
||||
})
|
||||
.pipe(tap((files) => (this.recipeFiles[country] = files)));
|
||||
}
|
||||
|
||||
getRecipeFileCountries(): string[] {
|
||||
return this.countries;
|
||||
}
|
||||
|
||||
getRecipeFileNames(country: string): string[] | null {
|
||||
return this.recipeFiles[country] ?? null;
|
||||
}
|
||||
|
||||
editChanges(country: string, filename: string, change: any) {
|
||||
console.log('target version = ', filename);
|
||||
console.log('change in edit: ', change.value);
|
||||
return this._httpClient
|
||||
.post<{
|
||||
status: string;
|
||||
}>(
|
||||
environment.api + ('/recipes/edit/' + country + '/' + filename),
|
||||
change.value,
|
||||
{
|
||||
withCredentials: true,
|
||||
responseType: 'json',
|
||||
}
|
||||
)
|
||||
.subscribe({
|
||||
next(value) {
|
||||
console.log(value, change.value);
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
import { HttpClient } from '@angular/common/http';
|
||||
import { Injectable } from '@angular/core';
|
||||
import { Observable, tap } from 'rxjs';
|
||||
import {
|
||||
Recipe,
|
||||
Recipe01,
|
||||
RecipeDetail,
|
||||
RecipeDetailMat,
|
||||
RecipeOverview,
|
||||
RecipeOverviewList,
|
||||
RecipesDashboard,
|
||||
} from '../models/recipe.model';
|
||||
import { environment } from 'src/environments/environment';
|
||||
import { RecipeMetaData } from 'src/app/shared/types/recipe';
|
||||
|
||||
type RecipeOverviewParams = {
|
||||
filename: string;
|
||||
country: string;
|
||||
materialIds: number[];
|
||||
offset: number;
|
||||
take: number;
|
||||
search: string;
|
||||
};
|
||||
|
||||
type RecipeDashboardParams = {
|
||||
filename: string;
|
||||
country: string;
|
||||
};
|
||||
|
||||
interface RecipeFiles {
|
||||
[key: string]: string[];
|
||||
}
|
||||
|
||||
@Injectable({ providedIn: 'root' })
|
||||
export class RecipeService {
|
||||
private countries: string[] = [];
|
||||
private recipeFiles: RecipeFiles = {};
|
||||
|
||||
private tmp_files: string[] = [];
|
||||
|
||||
private get tmpfiles(): string[] {
|
||||
return this.tmp_files;
|
||||
}
|
||||
|
||||
constructor(private _httpClient: HttpClient) {}
|
||||
|
||||
getRecipesDashboard(
|
||||
params: RecipeDashboardParams = {
|
||||
country: this.getCurrentCountry(),
|
||||
filename: this.getCurrentFile(),
|
||||
}
|
||||
): Observable<RecipesDashboard> {
|
||||
return this._httpClient.get<RecipesDashboard>(
|
||||
environment.api + '/recipes/dashboard',
|
||||
{
|
||||
params: {
|
||||
country: params.country,
|
||||
filename: params.filename,
|
||||
},
|
||||
withCredentials: true,
|
||||
responseType: 'json',
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
getRecipeOverview(
|
||||
params: RecipeOverviewParams = {
|
||||
country: this.getCurrentCountry(),
|
||||
filename: this.getCurrentFile(),
|
||||
materialIds: [],
|
||||
offset: 0,
|
||||
take: 20,
|
||||
search: '',
|
||||
}
|
||||
): Observable<RecipeOverviewList> {
|
||||
return this._httpClient.get<RecipeOverviewList>(
|
||||
environment.api + '/recipes/overview',
|
||||
{
|
||||
params: {
|
||||
country: params.country,
|
||||
filename: params.filename,
|
||||
materialIds: params.materialIds.join(','),
|
||||
offset: params.offset.toString(),
|
||||
take: params.take.toString(),
|
||||
search: params.search,
|
||||
},
|
||||
withCredentials: true,
|
||||
responseType: 'json',
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
getRecipeDetail(productCode: string): Observable<RecipeDetail> {
|
||||
return this._httpClient.get<RecipeDetail>(
|
||||
environment.api + '/recipes/' + productCode,
|
||||
{
|
||||
params: {
|
||||
filename: this.getCurrentFile(),
|
||||
country: this.getCurrentCountry(),
|
||||
},
|
||||
withCredentials: true,
|
||||
responseType: 'json',
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
getRecipeDetailMat(
|
||||
productCode: string
|
||||
): Observable<{ result: RecipeDetailMat[] }> {
|
||||
return this._httpClient.get<{ result: RecipeDetailMat[] }>(
|
||||
environment.api + '/recipes/' + productCode + '/mat',
|
||||
{
|
||||
params: {
|
||||
filename: this.getCurrentFile(),
|
||||
country: this.getCurrentCountry(),
|
||||
},
|
||||
withCredentials: true,
|
||||
responseType: 'json',
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
getCurrentFile(): string {
|
||||
const currentRecipeFile = localStorage.getItem('currentRecipeFile');
|
||||
if (currentRecipeFile) {
|
||||
return currentRecipeFile;
|
||||
}
|
||||
|
||||
return 'coffeethai02_580.json';
|
||||
}
|
||||
|
||||
getCurrentCountry(): string {
|
||||
const currentRecipeCountry = localStorage.getItem('currentRecipeCountry');
|
||||
if (currentRecipeCountry) {
|
||||
return currentRecipeCountry;
|
||||
}
|
||||
|
||||
return 'Thailand';
|
||||
}
|
||||
|
||||
getRecipesById(id: string): Observable<{
|
||||
recipe: Recipe01;
|
||||
recipeMetaData: RecipeMetaData;
|
||||
}> {
|
||||
return this._httpClient.get<{
|
||||
recipe: Recipe01;
|
||||
recipeMetaData: RecipeMetaData;
|
||||
}>(environment.api + '/recipes/' + id, {
|
||||
withCredentials: true,
|
||||
responseType: 'json',
|
||||
});
|
||||
}
|
||||
|
||||
getRecipeCountries(): Observable<string[]> {
|
||||
return this._httpClient
|
||||
.get<string[]>(environment.api + '/recipes/versions', {
|
||||
withCredentials: true,
|
||||
responseType: 'json',
|
||||
})
|
||||
.pipe(tap((countries) => (this.countries = countries)));
|
||||
}
|
||||
|
||||
getRecipeFiles(country: string): Observable<string[]> {
|
||||
return this._httpClient
|
||||
.get<string[]>(environment.api + '/recipes/versions/' + country, {
|
||||
withCredentials: true,
|
||||
responseType: 'json',
|
||||
})
|
||||
.pipe(tap((files) => (this.recipeFiles[country] = files)));
|
||||
}
|
||||
|
||||
getRecipeFileCountries(): string[] {
|
||||
return this.countries;
|
||||
}
|
||||
|
||||
getRecipeFileNames(country: string): string[] | null {
|
||||
return this.recipeFiles[country] ?? null;
|
||||
}
|
||||
|
||||
editChanges(country: string, filename: string, change: any) {
|
||||
console.log('target version = ', filename);
|
||||
console.log('change in edit: ', change);
|
||||
return this._httpClient
|
||||
.post<{
|
||||
status: string;
|
||||
}>(
|
||||
environment.api + ('/recipes/edit/' + country + '/' + filename),
|
||||
JSON.stringify(change),
|
||||
{
|
||||
withCredentials: true,
|
||||
responseType: 'json',
|
||||
}
|
||||
)
|
||||
.subscribe({
|
||||
next(value) {
|
||||
console.log( value);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
getSavedTmp(country: string, filename:string) {
|
||||
console.log("loading saved .tmp* file", country, filename)
|
||||
|
||||
// do split filename
|
||||
filename = filename.split('_')[1];
|
||||
|
||||
|
||||
// this._user.getCurrentUser().subscribe((user) => {
|
||||
// this.user = user.user;
|
||||
// })
|
||||
return this._httpClient
|
||||
.get<string[]>(
|
||||
environment.api + ("/recipes/saved/"+ country + "/" + filename),
|
||||
{
|
||||
withCredentials: true,
|
||||
}
|
||||
);
|
||||
// .subscribe({
|
||||
// next(value) {
|
||||
// console.log( value);
|
||||
// },
|
||||
// });
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -112,17 +112,20 @@
|
|||
</button>
|
||||
<!-- todo: add modal -->
|
||||
<dialog id="select_savefile_modal" class="modal">
|
||||
<div class="modal-box max-w-[600px] overflow-visible">
|
||||
<div class="modal-box max-w-[1000px] overflow-visible">
|
||||
<p class="font-bold text-lg m-2">Saved Files</p>
|
||||
<table class="table">
|
||||
<tr class="bg-primary ">
|
||||
<th>Name</th>
|
||||
<th>Commit ID</th>
|
||||
<th>Comment</th>
|
||||
<!-- <th></th> -->
|
||||
<th>Editor</th>
|
||||
<th>Date</th>
|
||||
</tr>
|
||||
<tr class="row hover:bg-secondary" *ngFor="let file of savedTmpfiles">
|
||||
<td>{{ file }}</td>
|
||||
<td>"-"</td>
|
||||
<td>{{ file.Id }}</td>
|
||||
<td>{{ file.Msg }}</td>
|
||||
<td>{{ file.Editor }}</td>
|
||||
<td>{{ file.Created_at }}</td>
|
||||
<button class="btn bg-blue-400">Select</button>
|
||||
</tr>
|
||||
</table>
|
||||
|
|
|
|||
|
|
@ -65,7 +65,7 @@ export class RecipesComponent implements OnInit, OnDestroy {
|
|||
private searchStr = '';
|
||||
private oldSearchStr = '';
|
||||
|
||||
savedTmpfiles: string[] = [];
|
||||
savedTmpfiles: any[] = [];
|
||||
|
||||
tableCtx?: ElementRef;
|
||||
|
||||
|
|
@ -146,7 +146,7 @@ export class RecipesComponent implements OnInit, OnDestroy {
|
|||
this._recipeService.getCurrentFile()
|
||||
).subscribe({
|
||||
next: (files:any) => {
|
||||
console.log("Obtain saves: ", typeof files);
|
||||
console.log("Obtain saves: ", typeof files, files);
|
||||
if(files != undefined && typeof files === 'object'){
|
||||
// console.log("Obtain saves object: ", files.files[0], typeof files);
|
||||
this.savedTmpfiles = files.files;
|
||||
|
|
|
|||
|
|
@ -1,6 +1,8 @@
|
|||
package data
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
"go.uber.org/zap"
|
||||
|
|
@ -60,9 +62,46 @@ func Insert(c *CommitLog) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// func GetCommitLogOfFilename(filename string) ([]CommitLog, error) {
|
||||
//}
|
||||
// cut .json, split then get pos 2, check `change_file` startwith "filename" then return all quries
|
||||
func GetCommitLogOfFilename(countryId string, filename string) ([]CommitLog, error) {
|
||||
|
||||
filename = strings.TrimSuffix(filename, ".json")
|
||||
|
||||
// try split and get pos 1 and 2
|
||||
filenameParts := strings.Split(filename, "_")
|
||||
if len(filenameParts) == 2 {
|
||||
filename = filenameParts[1]
|
||||
} else if len(filenameParts) > 2 {
|
||||
filename = filenameParts[1] + "_" + filenameParts[2]
|
||||
}
|
||||
|
||||
Log.Debug("CommitDB", zap.Any("lookup", filename))
|
||||
|
||||
commitDB, err := sqlx.Connect("sqlite3", "./data/database.db")
|
||||
if err != nil {
|
||||
Log.Fatal("Error when connecting to database", zap.Error(err))
|
||||
}
|
||||
|
||||
var commits []CommitLog
|
||||
// get contains
|
||||
// err = commitDB.Get(&commits, "SELECT * FROM commit_log WHERE change_file LIKE ?", "%"+filename+"%")
|
||||
|
||||
err = commitDB.Select(&commits, "SELECT * FROM commit_log WHERE change_file LIKE ?", "%"+filename+"%")
|
||||
|
||||
var commitsByCountryID []CommitLog
|
||||
|
||||
for _, v := range commits {
|
||||
if strings.Contains(v.Change_file, countryId) {
|
||||
commitsByCountryID = append(commitsByCountryID, v)
|
||||
}
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
Log.Error("Error when get commit log", zap.Error(err))
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return commitsByCountryID, nil
|
||||
}
|
||||
|
||||
func GetCommitLogs() ([]CommitLog, error) {
|
||||
commit_db, err := sqlx.Connect("sqlite3", "./data/database.db")
|
||||
|
|
|
|||
|
|
@ -1,338 +1,344 @@
|
|||
package data
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"recipe-manager/helpers"
|
||||
"recipe-manager/models"
|
||||
"recipe-manager/services/logger"
|
||||
"time"
|
||||
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
var (
|
||||
Log = logger.GetInstance()
|
||||
)
|
||||
|
||||
type RecipeWithTimeStamps struct {
|
||||
Recipe models.Recipe
|
||||
TimeStamps int64
|
||||
}
|
||||
|
||||
type Data struct {
|
||||
CurrentFile string
|
||||
CurrentCountryID string
|
||||
AllRecipeFiles map[string][]helpers.RecipePath
|
||||
currentRecipe *models.Recipe
|
||||
recipeMap map[string]RecipeWithTimeStamps
|
||||
Countries []helpers.CountryName
|
||||
}
|
||||
|
||||
func NewData() *Data {
|
||||
|
||||
countries := []helpers.CountryName{{
|
||||
CountryID: "tha",
|
||||
CountryName: "Thailand",
|
||||
}, {
|
||||
CountryID: "mys",
|
||||
CountryName: "Malaysia",
|
||||
}, {
|
||||
CountryID: "aus",
|
||||
CountryName: "Australia",
|
||||
},
|
||||
}
|
||||
|
||||
allRecipeFiles := helpers.ScanRecipeFiles(countries)
|
||||
|
||||
defaultFile := "coffeethai02_600.json"
|
||||
defaultCountry := "tha"
|
||||
defaultRecipe, err := helpers.ReadRecipeFile(defaultCountry, defaultFile)
|
||||
|
||||
if err != nil {
|
||||
log.Panic("Error when read default recipe file:", err)
|
||||
}
|
||||
|
||||
return &Data{
|
||||
CurrentFile: defaultFile,
|
||||
CurrentCountryID: defaultCountry,
|
||||
AllRecipeFiles: allRecipeFiles,
|
||||
currentRecipe: defaultRecipe,
|
||||
recipeMap: map[string]RecipeWithTimeStamps{
|
||||
defaultFile: {
|
||||
Recipe: *defaultRecipe,
|
||||
TimeStamps: time.Now().Unix(),
|
||||
},
|
||||
},
|
||||
Countries: countries,
|
||||
}
|
||||
}
|
||||
|
||||
func (d *Data) GetRecipe(countryID, filename string) *models.Recipe {
|
||||
|
||||
if countryID == "" {
|
||||
return d.currentRecipe
|
||||
}
|
||||
|
||||
if filename == "" || filename == d.CurrentFile {
|
||||
return d.currentRecipe
|
||||
}
|
||||
|
||||
if recipe, ok := d.recipeMap[filename]; ok {
|
||||
d.CurrentFile = filename
|
||||
d.CurrentCountryID = countryID
|
||||
return &recipe.Recipe
|
||||
}
|
||||
|
||||
// change current version and read new recipe
|
||||
d.CurrentFile = filename
|
||||
d.CurrentCountryID = countryID
|
||||
recipe, err := helpers.ReadRecipeFile(countryID, filename)
|
||||
|
||||
if err != nil {
|
||||
logger.GetInstance().Error("Error when read recipe file", zap.Error(err))
|
||||
return d.currentRecipe
|
||||
}
|
||||
|
||||
d.currentRecipe = recipe
|
||||
|
||||
// save to map
|
||||
if len(d.recipeMap) > 5 { // limit keep in memory 5 version
|
||||
// remove oldest version
|
||||
var oldestVersion string
|
||||
var oldestTime int64
|
||||
for k, v := range d.recipeMap {
|
||||
if oldestTime == 0 || v.TimeStamps < oldestTime {
|
||||
oldestTime = v.TimeStamps
|
||||
oldestVersion = k
|
||||
}
|
||||
}
|
||||
delete(d.recipeMap, oldestVersion)
|
||||
}
|
||||
|
||||
d.recipeMap[filename] = RecipeWithTimeStamps{
|
||||
Recipe: *d.currentRecipe,
|
||||
TimeStamps: time.Now().Unix(),
|
||||
}
|
||||
|
||||
return d.currentRecipe
|
||||
}
|
||||
|
||||
func (d *Data) GetRecipe01() []models.Recipe01 {
|
||||
return d.currentRecipe.Recipe01
|
||||
}
|
||||
|
||||
func (d *Data) GetRecipe01ByProductCode(filename, countryID, productCode string) (models.Recipe01, error) {
|
||||
|
||||
if filename == "" || filename == d.CurrentFile {
|
||||
for _, v := range d.currentRecipe.Recipe01 {
|
||||
if v.ProductCode == productCode {
|
||||
return v, nil
|
||||
}
|
||||
}
|
||||
} else if recipe, ok := d.recipeMap[filename]; ok {
|
||||
for _, v := range recipe.Recipe.Recipe01 {
|
||||
if v.ProductCode == productCode {
|
||||
return v, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
d.CurrentFile = filename
|
||||
d.CurrentCountryID = countryID
|
||||
recipe, err := helpers.ReadRecipeFile(countryID, filename)
|
||||
|
||||
if err != nil {
|
||||
logger.GetInstance().Error("Error when read recipe file", zap.Error(err))
|
||||
for _, v := range d.currentRecipe.Recipe01 {
|
||||
if v.ProductCode == productCode {
|
||||
return v, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
d.currentRecipe = recipe
|
||||
|
||||
// save to map
|
||||
if len(d.recipeMap) > 5 { // limit keep in memory 5 version
|
||||
// remove oldest version
|
||||
var oldestVersion string
|
||||
var oldestTime int64
|
||||
for k, v := range d.recipeMap {
|
||||
if oldestTime == 0 || v.TimeStamps < oldestTime {
|
||||
oldestTime = v.TimeStamps
|
||||
oldestVersion = k
|
||||
}
|
||||
}
|
||||
delete(d.recipeMap, oldestVersion)
|
||||
}
|
||||
|
||||
d.recipeMap[filename] = RecipeWithTimeStamps{
|
||||
Recipe: *d.currentRecipe,
|
||||
TimeStamps: time.Now().Unix(),
|
||||
}
|
||||
|
||||
for _, v := range d.currentRecipe.Recipe01 {
|
||||
if v.ProductCode == productCode {
|
||||
return v, nil
|
||||
}
|
||||
}
|
||||
|
||||
return models.Recipe01{}, fmt.Errorf("product code: %s not found", productCode)
|
||||
}
|
||||
|
||||
func (d *Data) SetValuesToRecipe(recipe models.Recipe01) {
|
||||
for _, v := range d.currentRecipe.Recipe01 {
|
||||
if v.ProductCode == recipe.ProductCode {
|
||||
v = recipe
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (d *Data) GetMaterialSetting(countryID, filename string) []models.MaterialSetting {
|
||||
result := make([]models.MaterialSetting, 0)
|
||||
|
||||
if countryID == "" {
|
||||
copy(result, d.currentRecipe.MaterialSetting)
|
||||
return result
|
||||
}
|
||||
|
||||
if filename == "" || filename == d.CurrentFile {
|
||||
copy(result, d.currentRecipe.MaterialSetting)
|
||||
return result
|
||||
}
|
||||
|
||||
if recipe, ok := d.recipeMap[filename]; ok {
|
||||
copy(result, recipe.Recipe.MaterialSetting)
|
||||
d.CurrentFile = filename
|
||||
d.CurrentCountryID = countryID
|
||||
return result
|
||||
}
|
||||
|
||||
d.CurrentFile = filename
|
||||
d.CurrentCountryID = countryID
|
||||
recipe, err := helpers.ReadRecipeFile(countryID, filename)
|
||||
|
||||
if err != nil {
|
||||
logger.GetInstance().Error("Error when read recipe file", zap.Error(err))
|
||||
copy(result, d.currentRecipe.MaterialSetting)
|
||||
return result
|
||||
}
|
||||
|
||||
d.currentRecipe = recipe
|
||||
|
||||
// save to map
|
||||
if len(d.recipeMap) > 5 { // limit keep in memory 5 version
|
||||
// remove oldest version
|
||||
var oldestVersion string
|
||||
var oldestTime int64
|
||||
for k, v := range d.recipeMap {
|
||||
if oldestTime == 0 || v.TimeStamps < oldestTime {
|
||||
oldestTime = v.TimeStamps
|
||||
oldestVersion = k
|
||||
}
|
||||
}
|
||||
delete(d.recipeMap, oldestVersion)
|
||||
}
|
||||
|
||||
d.recipeMap[filename] = RecipeWithTimeStamps{
|
||||
Recipe: *d.currentRecipe,
|
||||
TimeStamps: time.Now().Unix(),
|
||||
}
|
||||
|
||||
copy(result, d.currentRecipe.MaterialSetting)
|
||||
return result
|
||||
}
|
||||
|
||||
func (d *Data) GetMaterialCode(ids []uint64, countryID, filename string) []models.MaterialCode {
|
||||
var result []models.MaterialCode
|
||||
|
||||
if filename == "" || filename == d.CurrentFile {
|
||||
result = d.currentRecipe.MaterialCode
|
||||
} else if recipe, ok := d.recipeMap[filename]; ok {
|
||||
d.CurrentFile = filename
|
||||
return recipe.Recipe.MaterialCode
|
||||
} else {
|
||||
d.CurrentFile = filename
|
||||
d.CurrentCountryID = countryID
|
||||
recipe, err := helpers.ReadRecipeFile(countryID, filename)
|
||||
|
||||
if err != nil {
|
||||
logger.GetInstance().Error("Error when read recipe file", zap.Error(err))
|
||||
return d.currentRecipe.MaterialCode
|
||||
}
|
||||
|
||||
d.currentRecipe = recipe
|
||||
|
||||
// save to map
|
||||
if len(d.recipeMap) > 5 { // limit keep in memory 5 version
|
||||
// remove oldest version
|
||||
var oldestVersion string
|
||||
var oldestTime int64
|
||||
for k, v := range d.recipeMap {
|
||||
if oldestTime == 0 || v.TimeStamps < oldestTime {
|
||||
oldestTime = v.TimeStamps
|
||||
oldestVersion = k
|
||||
}
|
||||
}
|
||||
delete(d.recipeMap, oldestVersion)
|
||||
}
|
||||
|
||||
d.recipeMap[filename] = RecipeWithTimeStamps{
|
||||
Recipe: *d.currentRecipe,
|
||||
TimeStamps: time.Now().Unix(),
|
||||
}
|
||||
|
||||
result = d.currentRecipe.MaterialCode
|
||||
}
|
||||
|
||||
if len(ids) == 0 {
|
||||
return result
|
||||
}
|
||||
|
||||
resultFilter := make([]models.MaterialCode, len(ids))
|
||||
for _, id := range ids {
|
||||
if id == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
for _, m := range result {
|
||||
if m.MaterialID == id {
|
||||
resultFilter = append(resultFilter, m)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
func (d *Data) ExportToJSON() []byte {
|
||||
b_recipe, err := json.Marshal(d.currentRecipe)
|
||||
if err != nil {
|
||||
Log.Error("Error when marshal recipe", zap.Error(err))
|
||||
return nil
|
||||
}
|
||||
|
||||
return b_recipe
|
||||
}
|
||||
package data
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"recipe-manager/helpers"
|
||||
"recipe-manager/models"
|
||||
"recipe-manager/services/logger"
|
||||
"time"
|
||||
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
var (
|
||||
Log = logger.GetInstance()
|
||||
)
|
||||
|
||||
type RecipeWithTimeStamps struct {
|
||||
Recipe models.Recipe
|
||||
TimeStamps int64
|
||||
}
|
||||
|
||||
type Data struct {
|
||||
CurrentFile string
|
||||
CurrentCountryID string
|
||||
AllRecipeFiles map[string][]helpers.RecipePath
|
||||
currentRecipe *models.Recipe
|
||||
recipeMap map[string]RecipeWithTimeStamps
|
||||
Countries []helpers.CountryName
|
||||
}
|
||||
|
||||
func NewData() *Data {
|
||||
|
||||
countries := []helpers.CountryName{{
|
||||
CountryID: "tha",
|
||||
CountryName: "Thailand",
|
||||
}, {
|
||||
CountryID: "mys",
|
||||
CountryName: "Malaysia",
|
||||
}, {
|
||||
CountryID: "aus",
|
||||
CountryName: "Australia",
|
||||
},
|
||||
}
|
||||
|
||||
allRecipeFiles := helpers.ScanRecipeFiles(countries)
|
||||
|
||||
defaultFile := "coffeethai02_600.json"
|
||||
defaultCountry := "tha"
|
||||
defaultRecipe, err := helpers.ReadRecipeFile(defaultCountry, defaultFile)
|
||||
|
||||
if err != nil {
|
||||
log.Panic("Error when read default recipe file:", err)
|
||||
}
|
||||
|
||||
return &Data{
|
||||
CurrentFile: defaultFile,
|
||||
CurrentCountryID: defaultCountry,
|
||||
AllRecipeFiles: allRecipeFiles,
|
||||
currentRecipe: defaultRecipe,
|
||||
recipeMap: map[string]RecipeWithTimeStamps{
|
||||
defaultFile: {
|
||||
Recipe: *defaultRecipe,
|
||||
TimeStamps: time.Now().Unix(),
|
||||
},
|
||||
},
|
||||
Countries: countries,
|
||||
}
|
||||
}
|
||||
|
||||
func (d *Data) GetRecipe(countryID, filename string) *models.Recipe {
|
||||
|
||||
if countryID == "" {
|
||||
return d.currentRecipe
|
||||
}
|
||||
|
||||
if filename == "" || filename == d.CurrentFile {
|
||||
return d.currentRecipe
|
||||
}
|
||||
|
||||
if recipe, ok := d.recipeMap[filename]; ok {
|
||||
d.CurrentFile = filename
|
||||
d.CurrentCountryID = countryID
|
||||
return &recipe.Recipe
|
||||
}
|
||||
|
||||
// change current version and read new recipe
|
||||
d.CurrentFile = filename
|
||||
d.CurrentCountryID = countryID
|
||||
recipe, err := helpers.ReadRecipeFile(countryID, filename)
|
||||
|
||||
if err != nil {
|
||||
logger.GetInstance().Error("Error when read recipe file [GetRecipe]", zap.Error(err))
|
||||
return d.currentRecipe
|
||||
}
|
||||
|
||||
d.currentRecipe = recipe
|
||||
|
||||
// save to map
|
||||
if len(d.recipeMap) > 5 { // limit keep in memory 5 version
|
||||
// remove oldest version
|
||||
var oldestVersion string
|
||||
var oldestTime int64
|
||||
for k, v := range d.recipeMap {
|
||||
if oldestTime == 0 || v.TimeStamps < oldestTime {
|
||||
oldestTime = v.TimeStamps
|
||||
oldestVersion = k
|
||||
}
|
||||
}
|
||||
delete(d.recipeMap, oldestVersion)
|
||||
}
|
||||
|
||||
d.recipeMap[filename] = RecipeWithTimeStamps{
|
||||
Recipe: *d.currentRecipe,
|
||||
TimeStamps: time.Now().Unix(),
|
||||
}
|
||||
|
||||
return d.currentRecipe
|
||||
}
|
||||
|
||||
func (d *Data) GetRecipe01() []models.Recipe01 {
|
||||
return d.currentRecipe.Recipe01
|
||||
}
|
||||
|
||||
func (d *Data) GetCurrentRecipe() *models.Recipe {
|
||||
return d.currentRecipe
|
||||
}
|
||||
|
||||
func (d *Data) GetRecipe01ByProductCode(filename, countryID, productCode string) (models.Recipe01, error) {
|
||||
|
||||
if filename == "" || filename == d.CurrentFile {
|
||||
for _, v := range d.currentRecipe.Recipe01 {
|
||||
if v.ProductCode == productCode {
|
||||
return v, nil
|
||||
}
|
||||
}
|
||||
} else if recipe, ok := d.recipeMap[filename]; ok {
|
||||
for _, v := range recipe.Recipe.Recipe01 {
|
||||
if v.ProductCode == productCode {
|
||||
return v, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
d.CurrentFile = filename
|
||||
d.CurrentCountryID = countryID
|
||||
recipe, err := helpers.ReadRecipeFile(countryID, filename)
|
||||
|
||||
if err != nil {
|
||||
logger.GetInstance().Error("Error when read recipe file [GetRecipe01ByProductCode]", zap.Error(err))
|
||||
for _, v := range d.currentRecipe.Recipe01 {
|
||||
if v.ProductCode == productCode {
|
||||
return v, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
d.currentRecipe = recipe
|
||||
|
||||
// save to map
|
||||
if len(d.recipeMap) > 5 { // limit keep in memory 5 version
|
||||
// remove oldest version
|
||||
var oldestVersion string
|
||||
var oldestTime int64
|
||||
for k, v := range d.recipeMap {
|
||||
if oldestTime == 0 || v.TimeStamps < oldestTime {
|
||||
oldestTime = v.TimeStamps
|
||||
oldestVersion = k
|
||||
}
|
||||
}
|
||||
delete(d.recipeMap, oldestVersion)
|
||||
}
|
||||
|
||||
d.recipeMap[filename] = RecipeWithTimeStamps{
|
||||
Recipe: *d.currentRecipe,
|
||||
TimeStamps: time.Now().Unix(),
|
||||
}
|
||||
|
||||
for _, v := range d.currentRecipe.Recipe01 {
|
||||
if v.ProductCode == productCode {
|
||||
return v, nil
|
||||
}
|
||||
}
|
||||
|
||||
return models.Recipe01{}, fmt.Errorf("product code: %s not found", productCode)
|
||||
}
|
||||
|
||||
func (d *Data) SetValuesToRecipe(recipe models.Recipe01) {
|
||||
for index, v := range d.currentRecipe.Recipe01 {
|
||||
if v.ProductCode == recipe.ProductCode {
|
||||
// Log.Debug("SetValuesToRecipe", zap.Any("old", v), zap.Any("new", recipe))
|
||||
// v = recipe
|
||||
d.currentRecipe.Recipe01[index] = recipe
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (d *Data) GetMaterialSetting(countryID, filename string) []models.MaterialSetting {
|
||||
result := make([]models.MaterialSetting, 0)
|
||||
|
||||
if countryID == "" {
|
||||
copy(result, d.currentRecipe.MaterialSetting)
|
||||
return result
|
||||
}
|
||||
|
||||
if filename == "" || filename == d.CurrentFile {
|
||||
copy(result, d.currentRecipe.MaterialSetting)
|
||||
return result
|
||||
}
|
||||
|
||||
if recipe, ok := d.recipeMap[filename]; ok {
|
||||
copy(result, recipe.Recipe.MaterialSetting)
|
||||
d.CurrentFile = filename
|
||||
d.CurrentCountryID = countryID
|
||||
return result
|
||||
}
|
||||
|
||||
d.CurrentFile = filename
|
||||
d.CurrentCountryID = countryID
|
||||
recipe, err := helpers.ReadRecipeFile(countryID, filename)
|
||||
|
||||
if err != nil {
|
||||
logger.GetInstance().Error("Error when read recipe file [GetMaterialSetting]", zap.Error(err))
|
||||
copy(result, d.currentRecipe.MaterialSetting)
|
||||
return result
|
||||
}
|
||||
|
||||
d.currentRecipe = recipe
|
||||
|
||||
// save to map
|
||||
if len(d.recipeMap) > 5 { // limit keep in memory 5 version
|
||||
// remove oldest version
|
||||
var oldestVersion string
|
||||
var oldestTime int64
|
||||
for k, v := range d.recipeMap {
|
||||
if oldestTime == 0 || v.TimeStamps < oldestTime {
|
||||
oldestTime = v.TimeStamps
|
||||
oldestVersion = k
|
||||
}
|
||||
}
|
||||
delete(d.recipeMap, oldestVersion)
|
||||
}
|
||||
|
||||
d.recipeMap[filename] = RecipeWithTimeStamps{
|
||||
Recipe: *d.currentRecipe,
|
||||
TimeStamps: time.Now().Unix(),
|
||||
}
|
||||
|
||||
copy(result, d.currentRecipe.MaterialSetting)
|
||||
return result
|
||||
}
|
||||
|
||||
func (d *Data) GetMaterialCode(ids []uint64, countryID, filename string) []models.MaterialCode {
|
||||
var result []models.MaterialCode
|
||||
|
||||
if filename == "" || filename == d.CurrentFile {
|
||||
result = d.currentRecipe.MaterialCode
|
||||
} else if recipe, ok := d.recipeMap[filename]; ok {
|
||||
d.CurrentFile = filename
|
||||
return recipe.Recipe.MaterialCode
|
||||
} else {
|
||||
d.CurrentFile = filename
|
||||
d.CurrentCountryID = countryID
|
||||
recipe, err := helpers.ReadRecipeFile(countryID, filename)
|
||||
|
||||
if err != nil {
|
||||
logger.GetInstance().Error("Error when read recipe file [GetMaterialCode]", zap.Error(err))
|
||||
return d.currentRecipe.MaterialCode
|
||||
}
|
||||
|
||||
d.currentRecipe = recipe
|
||||
|
||||
// save to map
|
||||
if len(d.recipeMap) > 5 { // limit keep in memory 5 version
|
||||
// remove oldest version
|
||||
var oldestVersion string
|
||||
var oldestTime int64
|
||||
for k, v := range d.recipeMap {
|
||||
if oldestTime == 0 || v.TimeStamps < oldestTime {
|
||||
oldestTime = v.TimeStamps
|
||||
oldestVersion = k
|
||||
}
|
||||
}
|
||||
delete(d.recipeMap, oldestVersion)
|
||||
}
|
||||
|
||||
d.recipeMap[filename] = RecipeWithTimeStamps{
|
||||
Recipe: *d.currentRecipe,
|
||||
TimeStamps: time.Now().Unix(),
|
||||
}
|
||||
|
||||
result = d.currentRecipe.MaterialCode
|
||||
}
|
||||
|
||||
if len(ids) == 0 {
|
||||
return result
|
||||
}
|
||||
|
||||
resultFilter := make([]models.MaterialCode, len(ids))
|
||||
for _, id := range ids {
|
||||
if id == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
for _, m := range result {
|
||||
if m.MaterialID == id {
|
||||
resultFilter = append(resultFilter, m)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
func (d *Data) ExportToJSON() []byte {
|
||||
b_recipe, err := json.Marshal(d.currentRecipe)
|
||||
if err != nil {
|
||||
Log.Error("Error when marshal recipe", zap.Error(err))
|
||||
return nil
|
||||
}
|
||||
|
||||
return b_recipe
|
||||
}
|
||||
|
|
|
|||
|
|
@ -370,31 +370,38 @@ func (rr *RecipeRouter) Route(r chi.Router) {
|
|||
return
|
||||
}
|
||||
|
||||
recipe_root_path := "./cofffeemachineConfig/"
|
||||
// recipe_root_path := "./cofffeemachineConfig/"
|
||||
|
||||
// structure
|
||||
full_file_name_targets := []string{}
|
||||
// full_file_name_targets := []string{}
|
||||
|
||||
files, err := os.ReadDir(recipe_root_path + countryID)
|
||||
// files, err := os.ReadDir(recipe_root_path + countryID)
|
||||
|
||||
// if err != nil {
|
||||
// Log.Error("Error when read directory", zap.Error(err))
|
||||
// return
|
||||
// }
|
||||
|
||||
// for _, file := range files {
|
||||
// Log.Debug("File: ", zap.Any("file", file.Name()))
|
||||
// if strings.Contains(file.Name(), file_version) && strings.Contains(file.Name(), ".tmp") {
|
||||
// full_file_name_targets = append(full_file_name_targets, file.Name())
|
||||
// }
|
||||
// }
|
||||
|
||||
commits, err := data.GetCommitLogOfFilename(countryID, file_version)
|
||||
|
||||
if err != nil {
|
||||
Log.Error("Error when read directory", zap.Error(err))
|
||||
Log.Error("Error when get commit log", zap.Error(err))
|
||||
return
|
||||
}
|
||||
|
||||
for _, file := range files {
|
||||
Log.Debug("File: ", zap.Any("file", file.Name()))
|
||||
if strings.Contains(file.Name(), file_version) && strings.Contains(file.Name(), ".tmp") {
|
||||
full_file_name_targets = append(full_file_name_targets, file.Name())
|
||||
}
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(map[string]interface{}{"files": full_file_name_targets})
|
||||
json.NewEncoder(w).Encode(map[string]interface{}{"files": commits})
|
||||
|
||||
Log.Debug("Saved Files: ", zap.Any("files", full_file_name_targets))
|
||||
Log.Debug("Saved Files: ", zap.Any("files", commits))
|
||||
})
|
||||
})
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue