Add User route and Refactor code
This commit is contained in:
parent
519749fd3a
commit
b311a41dc7
24 changed files with 902 additions and 489 deletions
|
|
@ -1,26 +1,27 @@
|
|||
package routers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"recipe-manager/config"
|
||||
"recipe-manager/services/oauth"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
"go.uber.org/zap"
|
||||
"golang.org/x/oauth2"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"recipe-manager/config"
|
||||
"recipe-manager/services/logger"
|
||||
"recipe-manager/services/oauth"
|
||||
"recipe-manager/services/user"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
type AuthRouter struct {
|
||||
cfg *config.ServerConfig
|
||||
oauth oauth.OAuthService
|
||||
}
|
||||
|
||||
func NewAuthRouter(cfg *config.ServerConfig, oauth oauth.OAuthService) *AuthRouter {
|
||||
return &AuthRouter{cfg, oauth}
|
||||
cfg *config.ServerConfig
|
||||
oauth oauth.OAuthService
|
||||
userService user.UserService
|
||||
taoLogger *logger.TaoLogger
|
||||
}
|
||||
|
||||
func (ar *AuthRouter) Route(r chi.Router) {
|
||||
|
|
@ -29,7 +30,10 @@ func (ar *AuthRouter) Route(r chi.Router) {
|
|||
|
||||
// generate state and nonce
|
||||
bytes := make([]byte, 32)
|
||||
rand.Read(bytes)
|
||||
_, err := rand.Read(bytes)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
state := base64.URLEncoding.EncodeToString(bytes)
|
||||
|
||||
stateMap := map[string]string{}
|
||||
|
|
@ -38,14 +42,16 @@ func (ar *AuthRouter) Route(r chi.Router) {
|
|||
stateMap["redirect_to"] = r.URL.Query().Get("redirect_to")
|
||||
}
|
||||
|
||||
url := ar.oauth.AuthURL(state, stateMap)
|
||||
http.Redirect(w, r, url, http.StatusTemporaryRedirect)
|
||||
authURL := ar.oauth.AuthURL(state, stateMap)
|
||||
http.Redirect(w, r, authURL, http.StatusTemporaryRedirect)
|
||||
})
|
||||
|
||||
r.Get("/google/callback", func(w http.ResponseWriter, r *http.Request) {
|
||||
// check state
|
||||
ctx, cancel := context.WithTimeout(r.Context(), time.Second*5)
|
||||
defer cancel()
|
||||
|
||||
var redirect_to string
|
||||
var redirectTo string
|
||||
state := r.URL.Query().Get("state")
|
||||
if state == "" {
|
||||
http.Error(w, "State not found", http.StatusBadRequest)
|
||||
|
|
@ -58,7 +64,7 @@ func (ar *AuthRouter) Route(r chi.Router) {
|
|||
return
|
||||
}
|
||||
|
||||
redirect_to = val["redirect_to"]
|
||||
redirectTo = val["redirect_to"]
|
||||
|
||||
ar.oauth.RemoveState(state)
|
||||
}
|
||||
|
|
@ -70,25 +76,44 @@ func (ar *AuthRouter) Route(r chi.Router) {
|
|||
return
|
||||
}
|
||||
|
||||
// get user info
|
||||
user, err := ar.oauth.GetUserInfo(r.Context(), token)
|
||||
// get userInfo info
|
||||
userInfo, err := ar.oauth.GetUserInfo(r.Context(), token)
|
||||
|
||||
if err != nil {
|
||||
http.Error(w, "Error getting user info", http.StatusBadRequest)
|
||||
http.Error(w, "Error getting userInfo info", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// map with database
|
||||
userFromDb, err := ar.userService.GetUserByEmail(ctx, userInfo.Email)
|
||||
if err != nil {
|
||||
http.Error(w, "Error while getting user data from database.", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
if userFromDb == nil {
|
||||
http.Error(w, "Unauthorized, We not found your email, Please contact admin.", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
picture := userInfo.Picture
|
||||
if userFromDb.Picture != "" {
|
||||
picture = userFromDb.Picture
|
||||
}
|
||||
|
||||
value := url.Values{
|
||||
"name": {user.Name},
|
||||
"email": {user.Email},
|
||||
"picture": {user.Picture},
|
||||
"id": {userFromDb.ID},
|
||||
"name": {userFromDb.Name},
|
||||
"email": {userInfo.Email},
|
||||
"picture": {picture},
|
||||
"permissions": {strconv.Itoa(int(userFromDb.Permissions))},
|
||||
}
|
||||
|
||||
if redirect_to != "" {
|
||||
value.Add("redirect_to", redirect_to)
|
||||
if redirectTo != "" {
|
||||
value.Add("redirect_to", redirectTo)
|
||||
}
|
||||
|
||||
Log.Info("User Log-In Success", zap.String("user", user.Name), zap.String("email", user.Email))
|
||||
ar.taoLogger.Log.Info("User Log-In Success", zap.String("userInfo", userInfo.Name), zap.String("email", userInfo.Email))
|
||||
|
||||
// redirect to frontend with token and refresh token
|
||||
w.Header().Add("set-cookie", "access_token="+token.AccessToken+"; Path=/; HttpOnly; SameSite=None; Secure; Max-Age=3600")
|
||||
|
|
@ -132,38 +157,9 @@ func (ar *AuthRouter) Route(r chi.Router) {
|
|||
w.Header().Add("set-cookie", "refresh_token=; Path=/; HttpOnly; SameSite=None; Secure; Max-Age=0")
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
})
|
||||
|
||||
r.Get("/user", func(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
token := &oauth2.Token{}
|
||||
if cookie, err := r.Cookie("access_token"); err == nil {
|
||||
token.AccessToken = cookie.Value
|
||||
}
|
||||
|
||||
// if have refresh token, set refresh token to token
|
||||
if cookie, err := r.Cookie("refresh_token"); err == nil {
|
||||
token.RefreshToken = cookie.Value
|
||||
}
|
||||
|
||||
if token.AccessToken == "" && token.RefreshToken == "" {
|
||||
http.Error(w, "Unauthorized", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
// get user info
|
||||
user, err := ar.oauth.GetUserInfo(r.Context(), token)
|
||||
|
||||
if err != nil {
|
||||
http.Error(w, "Error getting user info", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// return user info
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(map[string]interface{}{
|
||||
"user": user,
|
||||
})
|
||||
})
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
func NewAuthRouter(cfg *config.ServerConfig, oauth oauth.OAuthService, userService user.UserService, taoLogger *logger.TaoLogger) *AuthRouter {
|
||||
return &AuthRouter{cfg, oauth, userService, taoLogger}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ package routers
|
|||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/pkg/errors"
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
|
|
@ -24,172 +25,36 @@ type RecipeRouter struct {
|
|||
data *data.Data
|
||||
sheetService sheet.SheetService
|
||||
recipeService recipe.RecipeService
|
||||
taoLogger *logger.TaoLogger
|
||||
}
|
||||
|
||||
var (
|
||||
Log = logger.GetInstance()
|
||||
)
|
||||
|
||||
func NewRecipeRouter(data *data.Data, recipeService recipe.RecipeService, sheetService sheet.SheetService) *RecipeRouter {
|
||||
func NewRecipeRouter(data *data.Data, recipeService recipe.RecipeService, sheetService sheet.SheetService, taoLogger *logger.TaoLogger) *RecipeRouter {
|
||||
return &RecipeRouter{
|
||||
data: data,
|
||||
recipeService: recipeService,
|
||||
sheetService: sheetService,
|
||||
data,
|
||||
sheetService,
|
||||
recipeService,
|
||||
taoLogger,
|
||||
}
|
||||
}
|
||||
|
||||
func (rr *RecipeRouter) Route(r chi.Router) {
|
||||
r.Route("/recipes", func(r chi.Router) {
|
||||
r.Get("/dashboard", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
r.Get("/dashboard", rr.dashBoard)
|
||||
|
||||
country := r.URL.Query().Get("country")
|
||||
filename := r.URL.Query().Get("filename")
|
||||
r.Get("/overview", rr.overview)
|
||||
|
||||
result, err := rr.recipeService.GetRecipeDashboard(&contracts.RecipeDashboardRequest{
|
||||
Country: country,
|
||||
Filename: filename,
|
||||
})
|
||||
r.Get("/{product_code}", rr.getRecipeByProductCode)
|
||||
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
r.Get("/{product_code}/mat", rr.getRecipeMatByProductCode)
|
||||
|
||||
json.NewEncoder(w).Encode(result)
|
||||
})
|
||||
r.Get("/{country}/{filename}/json", rr.getRecipeJson)
|
||||
|
||||
r.Get("/overview", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
var take, offset uint64 = 10, 0
|
||||
if newOffset, err := strconv.ParseUint(r.URL.Query().Get("offset"), 10, 64); err == nil {
|
||||
offset = newOffset
|
||||
}
|
||||
r.Post("/edit/{country}/{filename}", rr.updateRecipe)
|
||||
|
||||
if newTake, err := strconv.ParseUint(r.URL.Query().Get("take"), 10, 64); err == nil {
|
||||
take = newTake
|
||||
}
|
||||
|
||||
country := r.URL.Query().Get("country")
|
||||
filename := r.URL.Query().Get("filename")
|
||||
materialIds := r.URL.Query().Get("materialIds")
|
||||
|
||||
var materialIdsUint []int
|
||||
for _, v := range strings.Split(materialIds, ",") {
|
||||
materialIdUint, err := strconv.ParseUint(v, 10, 64)
|
||||
if err != nil || materialIdUint == 0 {
|
||||
continue
|
||||
}
|
||||
materialIdsUint = append(materialIdsUint, int(materialIdUint))
|
||||
}
|
||||
|
||||
result, err := rr.recipeService.GetRecipeOverview(&contracts.RecipeOverviewRequest{
|
||||
Take: int(take),
|
||||
Skip: int(offset),
|
||||
Search: r.URL.Query().Get("search"),
|
||||
Country: country,
|
||||
Filename: filename,
|
||||
MatIds: materialIdsUint,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
json.NewEncoder(w).Encode(result)
|
||||
})
|
||||
|
||||
r.Get("/{product_code}", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
productCode := chi.URLParam(r, "product_code")
|
||||
|
||||
// recipe := rr.data.GetRecipe01()
|
||||
// recipeMetaData := rr.sheetService.GetSheet(r.Context(), "1rSUKcc5POR1KeZFGoeAZIoVoI7LPGztBhPw5Z_ConDE")
|
||||
|
||||
// var recipeResult *models.Recipe01
|
||||
// recipeMetaDataResult := map[string]string{}
|
||||
|
||||
// for _, v := range recipe {
|
||||
// if v.ProductCode == productCode {
|
||||
// recipeResult = &v
|
||||
// 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
|
||||
// }
|
||||
// }
|
||||
|
||||
// if recipeResult == nil {
|
||||
// http.Error(w, "Not Found", http.StatusNotFound)
|
||||
// return
|
||||
// }
|
||||
|
||||
// json.NewEncoder(w).Encode(map[string]interface{}{
|
||||
// "recipe": recipeResult,
|
||||
// "recipeMetaData": recipeMetaDataResult,
|
||||
// })
|
||||
|
||||
result, err := rr.recipeService.GetRecipeDetail(&contracts.RecipeDetailRequest{
|
||||
Filename: r.URL.Query().Get("filename"),
|
||||
Country: r.URL.Query().Get("country"),
|
||||
ProductCode: productCode,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
json.NewEncoder(w).Encode(result)
|
||||
})
|
||||
|
||||
r.Get("/{product_code}/mat", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
productCode := chi.URLParam(r, "product_code")
|
||||
|
||||
result, err := rr.recipeService.GetRecipeDetailMat(&contracts.RecipeDetailRequest{
|
||||
Filename: r.URL.Query().Get("filename"),
|
||||
Country: r.URL.Query().Get("country"),
|
||||
ProductCode: productCode,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
json.NewEncoder(w).Encode(result)
|
||||
})
|
||||
|
||||
r.Get("/{country}/{filename}/json", func(w http.ResponseWriter, r *http.Request) {
|
||||
country := chi.URLParam(r, "country")
|
||||
filename := chi.URLParam(r, "filename")
|
||||
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
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("/countries", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
// get key from map
|
||||
keys := []string{}
|
||||
var keys []string
|
||||
for k := range rr.data.AllRecipeFiles {
|
||||
countryName, err := rr.data.GetCountryNameByID(k)
|
||||
if err != nil {
|
||||
|
|
@ -197,10 +62,14 @@ func (rr *RecipeRouter) Route(r chi.Router) {
|
|||
}
|
||||
keys = append(keys, countryName)
|
||||
}
|
||||
json.NewEncoder(w).Encode(keys)
|
||||
if err := json.NewEncoder(w).Encode(keys); err != nil {
|
||||
rr.taoLogger.Log.Error("RecipeRouter.GetCountryRecipe", zap.Error(err))
|
||||
http.Error(w, "Internal Error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
})
|
||||
|
||||
r.Get("/versions/{country}", func(w http.ResponseWriter, r *http.Request) {
|
||||
r.Get("/{country}/versions", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
|
||||
countryName := chi.URLParam(r, "country")
|
||||
|
|
@ -209,17 +78,22 @@ func (rr *RecipeRouter) Route(r chi.Router) {
|
|||
http.Error(w, fmt.Sprintf("Country Name: %s not found!!!", countryName), http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
files := []string{}
|
||||
var files []string
|
||||
for _, v := range rr.data.AllRecipeFiles[countryID] {
|
||||
files = append(files, v.Name)
|
||||
}
|
||||
json.NewEncoder(w).Encode(files)
|
||||
|
||||
if err := json.NewEncoder(w).Encode(files); err != nil {
|
||||
rr.taoLogger.Log.Error("RecipeRouter.GetVersionsCountryRecipe", zap.Error(err))
|
||||
http.Error(w, "Internal Error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
})
|
||||
|
||||
r.Get("/test/sheet", func(w http.ResponseWriter, r *http.Request) {
|
||||
result := rr.sheetService.GetSheet(r.Context(), "1rSUKcc5POR1KeZFGoeAZIoVoI7LPGztBhPw5Z_ConDE")
|
||||
|
||||
mapResult := []map[string]string{}
|
||||
var mapResult []map[string]string
|
||||
|
||||
for _, v := range result {
|
||||
mapResult = append(mapResult, map[string]string{
|
||||
|
|
@ -231,96 +105,289 @@ func (rr *RecipeRouter) Route(r chi.Router) {
|
|||
"picture": v[5].(string),
|
||||
})
|
||||
}
|
||||
json.NewEncoder(w).Encode(mapResult)
|
||||
})
|
||||
|
||||
r.Post("/edit/{country}/{filename}", func(w http.ResponseWriter, r *http.Request) {
|
||||
Log.Debug("Edit: ", zap.String("path", r.RequestURI))
|
||||
filename := chi.URLParam(r, "filename")
|
||||
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)
|
||||
if err := json.NewEncoder(w).Encode(mapResult); err != nil {
|
||||
rr.taoLogger.Log.Error("RecipeRouter.TestSheet", zap.Error(err))
|
||||
http.Error(w, "Internal Error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
target_recipe := rr.data.GetRecipe(countryID, filename)
|
||||
|
||||
Log.Debug("Target => ", zap.Any("target", target_recipe.MachineSetting.ConfigNumber))
|
||||
|
||||
// check request structure
|
||||
|
||||
// FIXME: Request structure bug. Case-sensitive, likely bug at client
|
||||
// uncomment the below code to view the bug
|
||||
|
||||
// var change_request map[string]interface{}
|
||||
// err = json.NewDecoder(r.Body).Decode(&change_request)
|
||||
// if err != nil {
|
||||
// Log.Error("Decode in request failed: ", zap.Error(err))
|
||||
// }
|
||||
// Log.Debug("Request => ", zap.Any("request", change_request))
|
||||
|
||||
// Body
|
||||
var changes models.Recipe01
|
||||
err = json.NewDecoder(r.Body).Decode(&changes)
|
||||
if err != nil {
|
||||
Log.Error("Decode in request failed: ", zap.Error(err))
|
||||
}
|
||||
|
||||
Log.Debug("Changes: ", zap.Any("changes", changes))
|
||||
// TODO: find the matched pd
|
||||
target_menu, err := rr.data.GetRecipe01ByProductCode(filename, countryID, changes.ProductCode)
|
||||
|
||||
if err != nil {
|
||||
Log.Error("Error when get recipe by product code", zap.Error(err))
|
||||
return
|
||||
}
|
||||
|
||||
menu_map := target_menu.ToMap()
|
||||
change_map := changes.ToMap()
|
||||
|
||||
// Find changes
|
||||
for key, val := range menu_map {
|
||||
|
||||
test_bool, err := helpers.DynamicCompare(val, change_map[key])
|
||||
|
||||
if err != nil {
|
||||
Log.Error("DynamicCompare in request failed: ", zap.Error(err))
|
||||
}
|
||||
|
||||
if !test_bool {
|
||||
menu_map[key] = change_map[key]
|
||||
}
|
||||
}
|
||||
|
||||
// Apply changes
|
||||
tempRecipe := models.Recipe01{}
|
||||
tempRecipe = tempRecipe.FromMap(menu_map)
|
||||
rr.data.SetValuesToRecipe(tempRecipe)
|
||||
Log.Debug("ApplyChange", zap.Any("status", "passed"))
|
||||
|
||||
// check if changed
|
||||
// Log.Debug("Check if changed", zap.Any("result", rr.data.GetRecipe01ByProductCode(changes.ProductCode)))
|
||||
|
||||
file, _ := os.Create(path.Join("./cofffeemachineConfig", countryID, filename))
|
||||
if err != nil {
|
||||
Log.Error("Error when tried to create file", zap.Error(err))
|
||||
return
|
||||
}
|
||||
|
||||
encoder := json.NewEncoder(file)
|
||||
encoder.SetIndent("", " ")
|
||||
err = encoder.Encode(rr.data.GetRecipe(countryID, filename))
|
||||
|
||||
if err != nil {
|
||||
Log.Error("Error when write file", zap.Error(err))
|
||||
}
|
||||
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(map[string]interface{}{
|
||||
"status": "OK",
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
// ====================== Handler =================================
|
||||
|
||||
func (rr *RecipeRouter) dashBoard(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")
|
||||
|
||||
result, err := rr.recipeService.GetRecipeDashboard(&contracts.RecipeDashboardRequest{
|
||||
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.Dashboard", zap.Error(err))
|
||||
http.Error(w, "Internal Error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (rr *RecipeRouter) overview(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
var take, offset uint64 = 10, 0
|
||||
if newOffset, err := strconv.ParseUint(r.URL.Query().Get("offset"), 10, 64); err == nil {
|
||||
offset = newOffset
|
||||
}
|
||||
|
||||
if newTake, err := strconv.ParseUint(r.URL.Query().Get("take"), 10, 64); err == nil {
|
||||
take = newTake
|
||||
}
|
||||
|
||||
country := r.URL.Query().Get("country")
|
||||
filename := r.URL.Query().Get("filename")
|
||||
materialIds := r.URL.Query().Get("materialIds")
|
||||
|
||||
var materialIdsUint []int
|
||||
for _, v := range strings.Split(materialIds, ",") {
|
||||
materialIdUint, err := strconv.ParseUint(v, 10, 64)
|
||||
if err != nil || materialIdUint == 0 {
|
||||
continue
|
||||
}
|
||||
materialIdsUint = append(materialIdsUint, int(materialIdUint))
|
||||
}
|
||||
|
||||
result, err := rr.recipeService.GetRecipeOverview(&contracts.RecipeOverviewRequest{
|
||||
Take: int(take),
|
||||
Skip: int(offset),
|
||||
Search: r.URL.Query().Get("search"),
|
||||
Country: country,
|
||||
Filename: filename,
|
||||
MatIds: materialIdsUint,
|
||||
})
|
||||
|
||||
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.Overview", zap.Error(err))
|
||||
http.Error(w, "Internal Error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (rr *RecipeRouter) getRecipeByProductCode(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
productCode := chi.URLParam(r, "product_code")
|
||||
fileName := r.URL.Query().Get("filename")
|
||||
country := r.URL.Query().Get("country")
|
||||
|
||||
// recipe := rr.data.GetRecipe01()
|
||||
// recipeMetaData := rr.sheetService.GetSheet(r.Context(), "1rSUKcc5POR1KeZFGoeAZIoVoI7LPGztBhPw5Z_ConDE")
|
||||
|
||||
// var recipeResult *models.Recipe01
|
||||
// recipeMetaDataResult := map[string]string{}
|
||||
|
||||
// for _, v := range recipe {
|
||||
// if v.ProductCode == productCode {
|
||||
// recipeResult = &v
|
||||
// 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
|
||||
// }
|
||||
// }
|
||||
|
||||
// if recipeResult == nil {
|
||||
// http.Error(w, "Not Found", http.StatusNotFound)
|
||||
// return
|
||||
// }
|
||||
|
||||
// json.NewEncoder(w).Encode(map[string]interface{}{
|
||||
// "recipe": recipeResult,
|
||||
// "recipeMetaData": recipeMetaDataResult,
|
||||
// })
|
||||
|
||||
result, err := rr.recipeService.GetRecipeDetail(&contracts.RecipeDetailRequest{
|
||||
Filename: fileName,
|
||||
Country: country,
|
||||
ProductCode: productCode,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
rr.taoLogger.Log.Error("RecipeRouter.GetRecipeByProductCode", zap.Error(err))
|
||||
http.Error(w, fmt.Sprintf("Recipe file: %s with productCode: %s not found", fileName, productCode), http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
if err := json.NewEncoder(w).Encode(result); err != nil {
|
||||
rr.taoLogger.Log.Error("RecipeRouter.GetRecipeByProductCode", zap.Error(err))
|
||||
http.Error(w, "Internal Error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (rr *RecipeRouter) getRecipeMatByProductCode(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
productCode := chi.URLParam(r, "product_code")
|
||||
fileName := r.URL.Query().Get("filename")
|
||||
country := r.URL.Query().Get("country")
|
||||
|
||||
result, err := rr.recipeService.GetRecipeDetailMat(&contracts.RecipeDetailRequest{
|
||||
Filename: fileName,
|
||||
Country: country,
|
||||
ProductCode: productCode,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
rr.taoLogger.Log.Error("RecipeRouter.GetRecipeMatByProductCode", zap.Error(err))
|
||||
http.Error(w, fmt.Sprintf("Material Recipe file: %s with productCode: %s not found", fileName, productCode), http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
if err := json.NewEncoder(w).Encode(result); err != nil {
|
||||
rr.taoLogger.Log.Error("RecipeRouter.GetRecipeMatByProductCode", zap.Error(err))
|
||||
http.Error(w, "Internal Error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (rr *RecipeRouter) getRecipeJson(w http.ResponseWriter, r *http.Request) {
|
||||
country := chi.URLParam(r, "country")
|
||||
filename := chi.URLParam(r, "filename")
|
||||
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
countryID, err := rr.data.GetCountryIDByName(country)
|
||||
if err != nil {
|
||||
rr.taoLogger.Log.Error("RecipeRouter.GetRecipeJson", zap.Error(err))
|
||||
http.Error(w, fmt.Sprintf("Country Name: %s not found!!!", country), http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
if err := json.NewEncoder(w).Encode(rr.data.GetRecipe(countryID, filename)); err != nil {
|
||||
rr.taoLogger.Log.Error("RecipeRouter.GetRecipeJson", zap.Error(err))
|
||||
http.Error(w, "Internal Error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (rr *RecipeRouter) updateRecipe(w http.ResponseWriter, r *http.Request) {
|
||||
rr.taoLogger.Log.Debug("Edit: ", zap.String("path", r.RequestURI))
|
||||
filename := chi.URLParam(r, "filename")
|
||||
country := chi.URLParam(r, "country")
|
||||
|
||||
countryID, err := rr.data.GetCountryIDByName(country)
|
||||
if err != nil {
|
||||
rr.taoLogger.Log.Error("RecipeRouter.UpdateRecipe", zap.Error(err))
|
||||
http.Error(w, fmt.Sprintf("Country Name: %s not found!!!", country), http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
targetRecipe := rr.data.GetRecipe(countryID, filename)
|
||||
|
||||
rr.taoLogger.Log.Debug("Target => ", zap.Any("target", targetRecipe.MachineSetting.ConfigNumber))
|
||||
|
||||
// check request structure
|
||||
|
||||
// FIXME: Request structure bug. Case-sensitive, likely bug at client
|
||||
// uncomment the below code to view the bug
|
||||
|
||||
// var change_request map[string]interface{}
|
||||
// err = json.NewDecoder(r.Body).Decode(&change_request)
|
||||
// if err != nil {
|
||||
// rr.taoLogger.Log.Error("Decode in request failed: ", zap.Error(err))
|
||||
// }
|
||||
// Log.Debug("Request => ", zap.Any("request", change_request))
|
||||
|
||||
// Body
|
||||
var changes models.Recipe01
|
||||
err = json.NewDecoder(r.Body).Decode(&changes)
|
||||
if err != nil {
|
||||
rr.taoLogger.Log.Error("RecipeRouter.UpdateRecipe", zap.Error(errors.WithMessage(err, "Decode in request failed")))
|
||||
http.Error(w, "Can't Decode Recipe request body.", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
rr.taoLogger.Log.Debug("Changes: ", zap.Any("changes", changes))
|
||||
// TODO: find the matched pd
|
||||
targetMenu, err := rr.data.GetRecipe01ByProductCode(filename, countryID, changes.ProductCode)
|
||||
|
||||
if err != nil {
|
||||
rr.taoLogger.Log.Error("RecipeRouter.UpdateRecipe", zap.Error(errors.WithMessage(err, "Error when get recipe by product code")))
|
||||
http.Error(w, fmt.Sprintf("Recipe country: %s file: %s productCode: %s not found.", country, filename, changes.ProductCode), http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
menuMap := targetMenu.ToMap()
|
||||
changeMap := changes.ToMap()
|
||||
|
||||
// Find changes
|
||||
for key, val := range menuMap {
|
||||
|
||||
testBool, err := helpers.DynamicCompare(val, changeMap[key])
|
||||
|
||||
if err != nil {
|
||||
rr.taoLogger.Log.Error("RecipeRouter.UpdateRecipe", zap.Error(errors.WithMessage(err, "DynamicCompare in request failed")))
|
||||
http.Error(w, "Internal Error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
if !testBool {
|
||||
menuMap[key] = changeMap[key]
|
||||
}
|
||||
}
|
||||
|
||||
// Apply changes
|
||||
tempRecipe := models.Recipe01{}
|
||||
tempRecipe = tempRecipe.FromMap(menuMap)
|
||||
rr.data.SetValuesToRecipe(tempRecipe)
|
||||
rr.taoLogger.Log.Debug("ApplyChange", zap.Any("status", "passed"))
|
||||
|
||||
// check if changed
|
||||
// Log.Debug("Check if changed", zap.Any("result", rr.data.GetRecipe01ByProductCode(changes.ProductCode)))
|
||||
|
||||
file, _ := os.Create(path.Join("./cofffeemachineConfig", countryID, filename))
|
||||
if err != nil {
|
||||
rr.taoLogger.Log.Error("RecipeRouter.UpdateRecipe", zap.Error(errors.WithMessage(err, "Error when tried to create file")))
|
||||
http.Error(w, "Internal Error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
encoder := json.NewEncoder(file)
|
||||
encoder.SetIndent("", " ")
|
||||
err = encoder.Encode(rr.data.GetRecipe(countryID, filename))
|
||||
|
||||
if err != nil {
|
||||
rr.taoLogger.Log.Error("RecipeRouter.UpdateRecipe", zap.Error(errors.WithMessage(err, "Error when write file")))
|
||||
http.Error(w, "Internal Error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
if err := json.NewEncoder(w).Encode(&contracts.ResponseDefault{
|
||||
Status: http.StatusText(http.StatusOK),
|
||||
Message: "Recipe Updated",
|
||||
}); err != nil {
|
||||
rr.taoLogger.Log.Error("RecipeRouter.UpdateRecipe", zap.Error(err))
|
||||
http.Error(w, "Internal Error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
|
|
|||
98
server/routers/user.go
Normal file
98
server/routers/user.go
Normal file
|
|
@ -0,0 +1,98 @@
|
|||
package routers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"go.uber.org/zap"
|
||||
"net/http"
|
||||
"recipe-manager/contracts"
|
||||
"recipe-manager/enums/permissions"
|
||||
"recipe-manager/middlewares"
|
||||
"recipe-manager/services/logger"
|
||||
"recipe-manager/services/user"
|
||||
"time"
|
||||
)
|
||||
|
||||
type UserRouter struct {
|
||||
taoLogger *logger.TaoLogger
|
||||
userService user.UserService
|
||||
}
|
||||
|
||||
func NewUserRouter(taoLogger *logger.TaoLogger, userService user.UserService) *UserRouter {
|
||||
return &UserRouter{taoLogger, userService}
|
||||
}
|
||||
|
||||
func (ur *UserRouter) Route(r chi.Router) {
|
||||
|
||||
// Users
|
||||
r.Route("/users", func(r chi.Router) {
|
||||
r.Get("/", middlewares.Authorize([]permissions.Permission{permissions.SuperAdmin}, ur.getUsers))
|
||||
|
||||
r.Post("/", middlewares.Authorize([]permissions.Permission{permissions.SuperAdmin}, ur.createUser))
|
||||
})
|
||||
|
||||
// User
|
||||
r.Route("/user", func(r chi.Router) {
|
||||
r.Get("/{id}", middlewares.OwnOrAuthorize([]permissions.Permission{permissions.SuperAdmin}, ur.getUser))
|
||||
})
|
||||
}
|
||||
|
||||
// ================== Users Handler ================================
|
||||
|
||||
func (ur *UserRouter) getUsers(w http.ResponseWriter, r *http.Request) {
|
||||
// TODO: get all User, This route only SuperAdmin permission can access
|
||||
json.NewEncoder(w).Encode(map[string]interface{}{
|
||||
"status": "OK",
|
||||
})
|
||||
}
|
||||
|
||||
func (ur *UserRouter) createUser(w http.ResponseWriter, r *http.Request) {
|
||||
ctx, cancel := context.WithTimeout(r.Context(), time.Second*5)
|
||||
defer cancel()
|
||||
|
||||
u := &contracts.CreateUserReq{}
|
||||
if err := json.NewDecoder(r.Body).Decode(u); err != nil {
|
||||
ur.taoLogger.Log.Error("UserRouter.CreateUser", zap.Error(err))
|
||||
}
|
||||
|
||||
ur.taoLogger.Log.Info("UserRouter.CreateUser", zap.Reflect("u", u))
|
||||
|
||||
if err := ur.userService.CreateNewUser(ctx, u.Name, u.Email, u.Picture, u.Permissions); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
if err := json.NewEncoder(w).Encode(&contracts.ResponseDefault{
|
||||
Status: http.StatusText(http.StatusNoContent),
|
||||
Message: "Created",
|
||||
}); err != nil {
|
||||
ur.taoLogger.Log.Error("UserRouter.CreateUser", zap.Error(err))
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
// ==================== User Handler ===============================
|
||||
|
||||
func (ur *UserRouter) getUser(w http.ResponseWriter, r *http.Request) {
|
||||
ctx, cancel := context.WithTimeout(r.Context(), time.Second*5)
|
||||
defer cancel()
|
||||
|
||||
userID := chi.URLParam(r, "id")
|
||||
|
||||
getUser, err := ur.userService.GetUserByID(ctx, userID)
|
||||
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
if err := json.NewEncoder(w).Encode(&contracts.UserRes{
|
||||
ID: getUser.ID,
|
||||
Name: getUser.Name,
|
||||
Email: getUser.Email,
|
||||
Picture: getUser.Picture,
|
||||
Permissions: getUser.Permissions,
|
||||
}); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue