From 28cdbbc4f9db1c9db0b4a70fc9de04c2ef765d01 Mon Sep 17 00:00:00 2001 From: Kenta420 Date: Thu, 25 Jan 2024 16:54:57 +0700 Subject: [PATCH] update --- server/contracts/recipe.go | 9 +++++++ server/data/database.db | Bin 36864 -> 36864 bytes server/routers/recipe.go | 25 ++++++++++++++++++ server/services/oauth/oauth.go | 8 +++++- server/services/recipe/recipe.go | 43 +++++++++++++++++++++++++++++++ 5 files changed, 84 insertions(+), 1 deletion(-) diff --git a/server/contracts/recipe.go b/server/contracts/recipe.go index b970137..044983e 100644 --- a/server/contracts/recipe.go +++ b/server/contracts/recipe.go @@ -82,3 +82,12 @@ type RecipeDetailMatListRequest struct { type RecipeDetailMatListResponse struct { Result []RecipeDetailMat `json:"result"` } + +type GetAllRecipeRequest struct { + Filename string `json:"filename"` + Country string `json:"country"` +} + +type GetAllRecipeResponse struct { + Result []RecipeOverview `json:"result"` +} diff --git a/server/data/database.db b/server/data/database.db index 8d4513b6903ddf9d2074c05c27297a53de099046..e2bf5bef9f2ad89547344e8e929c8895c44ef688 100644 GIT binary patch delta 265 zcmZozz|^pSX@WGP^+XwGM(d3UOYC`A_?#H{d-xUjHt;!Z78Eez%gbTm2o*PGGB)N- zNj5SvFgH)tO-nX3*ELBrG|;s$v^3RCHa1DMurxJGOEgSMNi5GS0TX)p4r%#CB^i3j z`FbT84D*c)j7)S540Vl66bucljEt;|%&-V;-ryI)$iu=P%fNqwe?5OJe;d#hzWkaR t%%P0L*v~S#Exrz@e=Y<6T>f+X&-k|j_4o2ih%*~AVsRGB=B@eO3IOVtM}z-pP&f))Ie+v4khVp|#b&+$Lw-wG64&cAtUzPADZ>jfEy diff --git a/server/routers/recipe.go b/server/routers/recipe.go index 3f5bee8..0903aca 100644 --- a/server/routers/recipe.go +++ b/server/routers/recipe.go @@ -50,6 +50,31 @@ func (rr *RecipeRouter) Route(r chi.Router) { r.Route("/recipes", func(r chi.Router) { r.Get("/dashboard", rr.dashBoard) + r.Get("/{country}/{filename}/all", func(w http.ResponseWriter, r *http.Request) { + w.Header().Add("Content-Type", "application/json") + + country := r.URL.Query().Get("country") + filename := r.URL.Query().Get("filename") + + rr.taoLogger.Log.Debug("RecipeRouter.GetAll", zap.Any("country", country), zap.Any("filename", filename)) + + result, err := rr.recipeService.GetAllRecipe(&contracts.GetAllRecipeRequest{ + Country: country, + Filename: filename, + }) + + if err != nil { + http.Error(w, err.Error(), http.StatusNotFound) + return + } + + if err := json.NewEncoder(w).Encode(result); err != nil { + rr.taoLogger.Log.Error("RecipeRouter.GetAll", zap.Error(err)) + http.Error(w, "Internal Error", http.StatusInternalServerError) + return + } + }) + r.Get("/overview", rr.overview) r.Get("/{product_code}", rr.getRecipeByProductCode) diff --git a/server/services/oauth/oauth.go b/server/services/oauth/oauth.go index e8c8c05..ee3a6c1 100644 --- a/server/services/oauth/oauth.go +++ b/server/services/oauth/oauth.go @@ -52,7 +52,13 @@ func (o *oauthService) AuthURL(state string, stateMap map[string]string) string o.nonce[state] = make(map[string]string) } - return o.gConfig.AuthCodeURL(state, oauth2.SetAuthURLParam("hd", "forth.co.th"), oauth2.SetAuthURLParam("include_granted_scopes", "true"), oauth2.AccessTypeOffline) + authCodeOptions := []oauth2.AuthCodeOption{ + // oauth2.SetAuthURLParam("hd", "forth.co.th"), + oauth2.SetAuthURLParam("include_granted_scopes", "true"), + oauth2.AccessTypeOffline, + } + + return o.gConfig.AuthCodeURL(state, authCodeOptions...) } func (o *oauthService) GetState(state string) (map[string]string, bool) { diff --git a/server/services/recipe/recipe.go b/server/services/recipe/recipe.go index 9b202d1..eb63eac 100644 --- a/server/services/recipe/recipe.go +++ b/server/services/recipe/recipe.go @@ -16,6 +16,7 @@ import ( type RecipeService interface { GetRecipeDashboard(request *contracts.RecipeDashboardRequest) (contracts.RecipeDashboardResponse, error) GetRecipeOverview(request *contracts.RecipeOverviewRequest) (contracts.RecipeOverviewResponse, error) + GetAllRecipe(request *contracts.GetAllRecipeRequest) (contracts.GetAllRecipeResponse, error) GetRecipeDetail(request *contracts.RecipeDetailRequest) (contracts.RecipeDetailResponse, error) GetRecipeDetailMat(request *contracts.RecipeDetailRequest) (contracts.RecipeDetailMatListResponse, error) @@ -299,6 +300,48 @@ func (rs *recipeService) GetRecipeOverview(request *contracts.RecipeOverviewRequ return result, nil } +func (rs *recipeService) GetAllRecipe(request *contracts.GetAllRecipeRequest) (contracts.GetAllRecipeResponse, error) { + countryID, err := rs.db.GetCountryIDByName(request.Country) + + if err != nil { + return contracts.GetAllRecipeResponse{}, fmt.Errorf("country name: %s not found", request.Country) + } + recipe := rs.db.GetRecipe(countryID, request.Filename) + + result := contracts.GetAllRecipeResponse{} + + // Map to contracts.RecipeOverview + for _, v := range recipe.Recipe01 { + result.Result = append(result.Result, contracts.RecipeOverview{ + ID: v.ID, + ProductCode: v.ProductCode, + Name: v.Name, + OtherName: v.OtherName, + Description: v.Description, + OtherDescription: v.OtherDescription, + LastUpdated: v.LastChange, + }) + + // submenu + if len(v.SubMenu) > 0 { + for _, sub := range v.SubMenu { + result.Result = append(result.Result, contracts.RecipeOverview{ + ID: sub.ID, + ProductCode: sub.ProductCode, + Name: sub.Name, + OtherName: sub.OtherName, + Description: sub.Description, + OtherDescription: sub.OtherDescription, + LastUpdated: sub.LastChange, + }) + } + } + + } + + return result, nil +} + func NewRecipeService(db *data.Data, taoLogger *logger.TaoLogger) RecipeService { return &recipeService{ db: db,