update test get recipe metadatas

This commit is contained in:
Kenta420 2023-10-05 09:39:25 +07:00
parent 41812591b7
commit 7450796a6b
4 changed files with 95 additions and 37 deletions

View file

@ -60,8 +60,28 @@ export class RecipeService {
return 'coffeethai02_580.json'; return 'coffeethai02_580.json';
} }
getRecipesById(id: string): Observable<Recipe01> { getRecipesById(id: string): Observable<{
return this._httpClient.get<Recipe01>(environment.api + '/recipes/' + id, { recipe: Recipe01;
recipeMetaData: {
productCode: string;
name: string;
otherName: string;
description: string;
otherDescription: string;
picture: string;
};
}> {
return this._httpClient.get<{
recipe: Recipe01;
recipeMetaData: {
productCode: string;
name: string;
otherName: string;
description: string;
otherDescription: string;
picture: string;
};
}>(environment.api + '/recipes/' + id, {
withCredentials: true, withCredentials: true,
responseType: 'json', responseType: 'json',
}); });

View file

@ -24,7 +24,11 @@
<div class="modal-scroll"> <div class="modal-scroll">
<form class="flex flex-col gap-2" [formGroup]="recipeDetail"> <form class="flex flex-col gap-2" [formGroup]="recipeDetail">
<div class="flex gap-5"> <div class="flex gap-5">
<div class="bg-gray-500 w-[300px] h-[300px]">Picture</div> <img
src="{{ recipeMetaData?.picture }}.png"
width="300"
height="400"
/>
<div class="grid grid-cols-2 gap-4 w-full"> <div class="grid grid-cols-2 gap-4 w-full">
<div class="flex items-center"> <div class="flex items-center">
<label class="label" <label class="label"

View file

@ -22,6 +22,15 @@ interface RecipeDetail {
disable: boolean; disable: boolean;
} }
interface RecipeMetaData {
productCode: string;
name: string;
otherName: string;
description: string;
otherDescription: string;
picture: string;
}
@Component({ @Component({
selector: 'recipe-modal', selector: 'recipe-modal',
templateUrl: './recipe-modal.component.html', templateUrl: './recipe-modal.component.html',
@ -45,6 +54,8 @@ export class RecipeModalComponent {
disable: new FormControl<boolean>(false), disable: new FormControl<boolean>(false),
}); });
recipeMetaData: RecipeMetaData | null = null;
originalRecipeDetail: RecipeDetail | null = null; originalRecipeDetail: RecipeDetail | null = null;
private detailModal: ElementRef<HTMLDialogElement> | null = null; private detailModal: ElementRef<HTMLDialogElement> | null = null;
@ -61,10 +72,25 @@ export class RecipeModalComponent {
this.detailModal?.nativeElement.showModal(); this.detailModal?.nativeElement.showModal();
if (this.detailModal?.nativeElement.open) { if (this.detailModal?.nativeElement.open) {
this.recipeService.getRecipesById(this.id).subscribe((recipe) => { this.recipeService
this.title = recipe.name + ' | ' + recipe.productCode; .getRecipesById(this.id)
this.recipeDetail.patchValue( .subscribe(({ recipe, recipeMetaData }) => {
{ this.title = recipe.name + ' | ' + recipe.productCode;
this.recipeDetail.patchValue(
{
productCode: recipe.productCode,
name: recipe.name,
otherName: recipe.otherName,
description: recipe.Description,
otherDescription: recipe.otherDescription,
price: recipe.cashPrice,
isUse: recipe.isUse,
isShow: recipe.isShow,
disable: recipe.disable,
},
{ emitEvent: false }
);
this.originalRecipeDetail = {
productCode: recipe.productCode, productCode: recipe.productCode,
name: recipe.name, name: recipe.name,
otherName: recipe.otherName, otherName: recipe.otherName,
@ -74,21 +100,9 @@ export class RecipeModalComponent {
isUse: recipe.isUse, isUse: recipe.isUse,
isShow: recipe.isShow, isShow: recipe.isShow,
disable: recipe.disable, disable: recipe.disable,
}, };
{ emitEvent: false } this.recipeMetaData = recipeMetaData;
); });
this.originalRecipeDetail = {
productCode: recipe.productCode,
name: recipe.name,
otherName: recipe.otherName,
description: recipe.Description,
otherDescription: recipe.otherDescription,
price: recipe.cashPrice,
isUse: recipe.isUse,
isShow: recipe.isShow,
disable: recipe.disable,
};
});
} }
} }

View file

@ -73,22 +73,42 @@ func (rr *RecipeRouter) Route(r chi.Router) {
}) })
}) })
r.Get("/{id}", func(w http.ResponseWriter, r *http.Request) { r.Get("/{product_code}", func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json") w.Header().Add("Content-Type", "application/json")
id, err := strconv.ParseUint(chi.URLParam(r, "id"), 10, 64) productCode := chi.URLParam(r, "product_code")
if err != nil {
http.Error(w, "Invalid ID", http.StatusBadRequest)
return
}
recipe := rr.data.GetRecipe01() recipe := rr.data.GetRecipe01()
recipeMetaData := rr.sheetService.GetSheet(r.Context(), "1rSUKcc5POR1KeZFGoeAZIoVoI7LPGztBhPw5Z_ConDE")
recipeResult := models.Recipe01{}
recipeMetaDataResult := map[string]string{}
for _, v := range recipe { for _, v := range recipe {
if uint64(v.ID) == id { if v.ProductCode == productCode {
json.NewEncoder(w).Encode(v) recipeResult = v
return break
} }
} }
for _, v := range recipeMetaData {
if v[0].(string) == productCode {
recipeMetaDataResult = map[string]string{
"productCode": v[0].(string),
"name": v[1].(string),
"otherName": v[2].(string),
"description": v[3].(string),
"otherDescription": v[4].(string),
"picture": v[5].(string),
}
break
}
}
json.NewEncoder(w).Encode(map[string]interface{}{
"recipe": recipeResult,
"recipeMetaData": recipeMetaDataResult,
})
http.Error(w, "Recipe not found", http.StatusNotFound) http.Error(w, "Recipe not found", http.StatusNotFound)
}) })
@ -110,12 +130,12 @@ func (rr *RecipeRouter) Route(r chi.Router) {
for _, v := range result { for _, v := range result {
mapResult = append(mapResult, map[string]string{ mapResult = append(mapResult, map[string]string{
"product_code": v[0].(string), "productCode": v[0].(string),
"name": v[1].(string), "name": v[1].(string),
"other_name": v[2].(string), "otherName": v[2].(string),
"description": v[3].(string), "description": v[3].(string),
"other_description": v[4].(string), "otherDescription": v[4].(string),
"image": v[5].(string), "picture": v[5].(string),
}) })
} }
json.NewEncoder(w).Encode(mapResult) json.NewEncoder(w).Encode(mapResult)