Skip to content

Commit

Permalink
Merge pull request #240 from JokerTrickster/improvement/237/category
Browse files Browse the repository at this point in the history
Improvement/237/category
  • Loading branch information
JokerTrickster authored Dec 1, 2024
2 parents c17669d + 0d36b46 commit 9ea6459
Show file tree
Hide file tree
Showing 21 changed files with 314 additions and 338 deletions.
32 changes: 16 additions & 16 deletions src/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -1730,16 +1730,28 @@ const docTemplate = `{
"type": "number"
},
"scenarios": {
"type": "string"
"type": "array",
"items": {
"type": "string"
}
},
"themes": {
"type": "string"
"type": "array",
"items": {
"type": "string"
}
},
"times": {
"type": "string"
"type": "array",
"items": {
"type": "string"
}
},
"types": {
"type": "string"
"type": "array",
"items": {
"type": "string"
}
}
}
},
Expand Down Expand Up @@ -1795,18 +1807,6 @@ const docTemplate = `{
},
"name": {
"type": "string"
},
"scenario": {
"type": "string"
},
"theme": {
"type": "string"
},
"time": {
"type": "string"
},
"type": {
"type": "string"
}
}
},
Expand Down
32 changes: 16 additions & 16 deletions src/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -1719,16 +1719,28 @@
"type": "number"
},
"scenarios": {
"type": "string"
"type": "array",
"items": {
"type": "string"
}
},
"themes": {
"type": "string"
"type": "array",
"items": {
"type": "string"
}
},
"times": {
"type": "string"
"type": "array",
"items": {
"type": "string"
}
},
"types": {
"type": "string"
"type": "array",
"items": {
"type": "string"
}
}
}
},
Expand Down Expand Up @@ -1784,18 +1796,6 @@
},
"name": {
"type": "string"
},
"scenario": {
"type": "string"
},
"theme": {
"type": "string"
},
"time": {
"type": "string"
},
"type": {
"type": "string"
}
}
},
Expand Down
34 changes: 17 additions & 17 deletions src/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,11 @@ definitions:
sex:
type: string
type: object
request.ReqV02GoogleOauth:
properties:
token:
type: string
type: object
request.ReqV12RecommendFood:
properties:
previousAnswer:
Expand Down Expand Up @@ -208,11 +213,6 @@ definitions:
example: 한식
type: string
type: object
request.ReqV02GoogleOauth:
properties:
token:
type: string
type: object
request.ReqValidatePassword:
properties:
code:
Expand All @@ -237,13 +237,21 @@ definitions:
protein:
type: number
scenarios:
type: string
items:
type: string
type: array
themes:
type: string
items:
type: string
type: array
times:
type: string
items:
type: string
type: array
types:
type: string
items:
type: string
type: array
required:
- name
type: object
Expand Down Expand Up @@ -281,14 +289,6 @@ definitions:
type: string
name:
type: string
scenario:
type: string
theme:
type: string
time:
type: string
type:
type: string
type: object
response.MetaData:
properties:
Expand Down
6 changes: 3 additions & 3 deletions src/features/auth/repository/guestAuthRepository.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (g *GuestAuthRepository) SaveToken(ctx context.Context, uID uint, accessTok
}
result := g.GormDB.Model(&token).Create(&token)
if result.Error != nil {
return utils.ErrorMsg(ctx, utils.ErrInternalServer, utils.Trace(), utils.HandleError(result.Error.Error(),uID,accessToken,refreshToken), utils.ErrFromInternal)
return utils.ErrorMsg(ctx, utils.ErrInternalServer, utils.Trace(), utils.HandleError(result.Error.Error(), uID, accessToken, refreshToken), utils.ErrFromInternal)
}
return nil
}
Expand All @@ -32,9 +32,9 @@ func (g *GuestAuthRepository) FindOneAndUpdateUser(ctx context.Context, email, p
Email: email,
}

err := g.GormDB.WithContext(ctx).Where("email = ? and password = ? and provider = ?", email, password, "test").First(&user).Error
err := g.GormDB.WithContext(ctx).Where("email = ? and password = ? and provider = ?", email, password, "email").First(&user).Error
if err != nil {
return mysql.Users{}, utils.ErrorMsg(ctx, utils.ErrInternalServer, utils.Trace(), utils.HandleError(err.Error(),email,password), utils.ErrFromInternal)
return mysql.Users{}, utils.ErrorMsg(ctx, utils.ErrInternalServer, utils.Trace(), utils.HandleError(err.Error(), email, password), utils.ErrFromInternal)
}
return user, nil
}
8 changes: 5 additions & 3 deletions src/features/food/model/interface/IFoodRepository.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ type IRecommendFoodRepository interface {

type ISelectFoodRepository interface {
FindOneFood(ctx context.Context, foodDTO *mysql.Foods) (uint, error)
InsertOneFoodHistory(ctx context.Context, foodHistoryDTO *mysql.FoodHistory) error
InsertOneFoodHistory(ctx context.Context, foodHistoryDTO *mysql.FoodHistories) error
IncrementFoodRanking(ctx context.Context, foodName string, score float64) error
}
type IHistoryFoodRepository interface {
FindAllFoodHistory(ctx context.Context, userID uint) ([]mysql.FoodHistory, error)
FindAllFoodHistory(ctx context.Context, userID uint) ([]mysql.FoodHistories, error)
FindOneFood(ctx context.Context, foodID uint) (*mysql.Foods, error)
}

Expand Down Expand Up @@ -49,9 +49,11 @@ type IDailyRecommendFoodRepository interface {
}

type ISaveFoodRepository interface {
SaveFood(ctx context.Context, foodDTO *mysql.Foods) error
SaveFood(ctx context.Context, foodDTO *mysql.Foods) (uint, error)
FindOneOrCreateFoodImage(ctx context.Context, foodImageDTO *mysql.FoodImages) (*mysql.FoodImages, error)
SaveNutrient(ctx context.Context, nutrientDTO *mysql.Nutrients) error
FindCategoryIDs(ctx context.Context, categories []string) ([]uint, error)
SaveFoodCategory(ctx context.Context, foodID uint, categoryIDs []uint) error
}

type ICheckImageUploadFoodRepository interface {
Expand Down
20 changes: 10 additions & 10 deletions src/features/food/model/request/saveFood.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ type ReqSaveFood struct {
}

type SaveFood struct {
Name string `json:"name" validate:"required"`
Times string `json:"times"`
Scenarios string `json:"scenarios"`
Themes string `json:"themes"`
Types string `json:"types"`
Amount string `json:"amount"`
Kcal float64 `json:"kcal"`
Fat float64 `json:"fat"`
Carbohydrate float64 `json:"carbohydrate"`
Protein float64 `json:"protein"`
Name string `json:"name" validate:"required"`
Times []string `json:"times"`
Scenarios []string `json:"scenarios"`
Themes []string `json:"themes"`
Types []string `json:"types"`
Amount string `json:"amount"`
Kcal float64 `json:"kcal"`
Fat float64 `json:"fat"`
Carbohydrate float64 `json:"carbohydrate"`
Protein float64 `json:"protein"`
}
8 changes: 2 additions & 6 deletions src/features/food/model/response/historyFood.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ type ResHistoryFood struct {
}

type HistoryFood struct {
Name string `json:"name"`
Type string `json:"type"`
Time string `json:"time"`
Scenario string `json:"scenario"`
Theme string `json:"theme"`
Created string `json:"created"`
Name string `json:"name"`
Created string `json:"created"`
}
4 changes: 2 additions & 2 deletions src/features/food/repository/historyFoodRepository.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ func (g *HistoryFoodRepository) FindOneFood(ctx context.Context, foodID uint) (*
return &food, nil
}

func (g *HistoryFoodRepository) FindAllFoodHistory(ctx context.Context, userID uint) ([]mysql.FoodHistory, error) {
foodHistoryList := []mysql.FoodHistory{}
func (g *HistoryFoodRepository) FindAllFoodHistory(ctx context.Context, userID uint) ([]mysql.FoodHistories, error) {
foodHistoryList := []mysql.FoodHistories{}
if err := g.GormDB.WithContext(ctx).Where("user_id = ?", userID).Find(&foodHistoryList).Error; err != nil {
return nil, utils.ErrorMsg(ctx, utils.ErrInternalDB, utils.Trace(), utils.HandleError(_errors.ErrServerError.Error()+err.Error(), userID), utils.ErrFromMysqlDB)
}
Expand Down
2 changes: 1 addition & 1 deletion src/features/food/repository/recommendFoodRepository.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (d *RecommendFoodRepository) FindOneUser(ctx context.Context, uID uint) (*m
func (d *RecommendFoodRepository) SaveRecommendFood(ctx context.Context, foodDTO *mysql.Foods) (*mysql.Foods, error) {
foods := mysql.Foods{}
// 존재 여부 확인
err := d.GormDB.WithContext(ctx).Model(&foods).Where("name = ? AND time_id = ? AND type_id = ? AND scenario_id = ? and theme_id = ? ", foodDTO.Name, foodDTO.TimeID, foodDTO.TypeID, foodDTO.ScenarioID, foodDTO.ThemeID).First(&foods).Error
err := d.GormDB.WithContext(ctx).Model(&foods).Where("name = ? ", foodDTO.Name).First(&foods).Error

if err == nil {
// 데이터가 이미 존재함
Expand Down
33 changes: 27 additions & 6 deletions src/features/food/repository/saveFoodRepository.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,26 @@ func NewSaveFoodRepository(gormDB *gorm.DB) _interface.ISaveFoodRepository {
return &SaveFoodRepository{GormDB: gormDB}
}

func (d *SaveFoodRepository) SaveFood(ctx context.Context, foodDTO *mysql.Foods) error {
func (d *SaveFoodRepository) SaveFood(ctx context.Context, foodDTO *mysql.Foods) (uint, error) {
foods := mysql.Foods{}
// 존재 여부 확인
err := d.GormDB.WithContext(ctx).Model(&foods).Where("name = ? AND time_id = ? AND type_id = ? AND scenario_id = ? and theme_id = ? ", foodDTO.Name, foodDTO.TimeID, foodDTO.TypeID, foodDTO.ScenarioID, foodDTO.ThemeID).First(&foods).Error
err := d.GormDB.WithContext(ctx).Model(&foods).Where("name = ? ", foodDTO.Name).First(&foods).Error

if err == nil {
// 데이터가 이미 존재함
return nil
return foods.ID, nil
}

if !errors.Is(err, gorm.ErrRecordNotFound) {
// 데이터베이스 오류
return utils.ErrorMsg(ctx, utils.ErrInternalDB, utils.Trace(), utils.HandleError(_errors.ErrServerError.Error()+err.Error(), foodDTO), utils.ErrFromMysqlDB)
return 0, utils.ErrorMsg(ctx, utils.ErrInternalDB, utils.Trace(), utils.HandleError(_errors.ErrServerError.Error()+err.Error(), foodDTO), utils.ErrFromMysqlDB)
}

// 데이터가 존재하지 않으므로 저장
if err := d.GormDB.WithContext(ctx).Create(&foodDTO).Error; err != nil {
return utils.ErrorMsg(ctx, utils.ErrInternalDB, utils.Trace(), utils.HandleError(_errors.ErrServerError.Error()+err.Error(), foodDTO), utils.ErrFromMysqlDB)
return 0, utils.ErrorMsg(ctx, utils.ErrInternalDB, utils.Trace(), utils.HandleError(_errors.ErrServerError.Error()+err.Error(), foodDTO), utils.ErrFromMysqlDB)
}
return nil
return foodDTO.ID, nil
}

func (d *SaveFoodRepository) FindOneOrCreateFoodImage(ctx context.Context, foodImageDTO *mysql.FoodImages) (*mysql.FoodImages, error) {
Expand Down Expand Up @@ -66,3 +66,24 @@ func (d *SaveFoodRepository) SaveNutrient(ctx context.Context, nutrientDTO *mysq
}
return nil
}

func (d *SaveFoodRepository) FindCategoryIDs(ctx context.Context, categories []string) ([]uint, error) {
categoryIDs := make([]uint, 0)
// 카테고리 조회
result := d.GormDB.WithContext(ctx).Model(&mysql.Categories{}).Where("name IN ?", categories).Pluck("id", &categoryIDs)
if result.Error != nil {
return nil, utils.ErrorMsg(ctx, utils.ErrInternalDB, utils.Trace(), utils.HandleError(_errors.ErrServerError.Error()+result.Error.Error(), categories), utils.ErrFromMysqlDB)
}
return categoryIDs, nil
}

func (d *SaveFoodRepository) SaveFoodCategory(ctx context.Context, foodID uint, categoryIDs []uint) error {
// 음식 카테고리 저장
for _, categoryID := range categoryIDs {
foodCategoryDTO := mysql.FoodCategories{FoodID: int(foodID), CategoryID: int(categoryID)}
if err := d.GormDB.WithContext(ctx).Create(&foodCategoryDTO).Error; err != nil {
return utils.ErrorMsg(ctx, utils.ErrInternalDB, utils.Trace(), utils.HandleError(_errors.ErrServerError.Error()+err.Error(), foodCategoryDTO), utils.ErrFromMysqlDB)
}
}
return nil
}
2 changes: 1 addition & 1 deletion src/features/food/repository/selectFoodRepository.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (g *SelectFoodRepository) FindOneFood(ctx context.Context, foodDTO *mysql.F
}
return food.ID, nil
}
func (g *SelectFoodRepository) InsertOneFoodHistory(ctx context.Context, foodHistoryDTO *mysql.FoodHistory) error {
func (g *SelectFoodRepository) InsertOneFoodHistory(ctx context.Context, foodHistoryDTO *mysql.FoodHistories) error {
if err := g.GormDB.WithContext(ctx).Create(&foodHistoryDTO).Error; err != nil {
return utils.ErrorMsg(ctx, utils.ErrInternalDB, utils.Trace(), utils.HandleError(_errors.ErrServerError.Error()+err.Error(), foodHistoryDTO), utils.ErrFromMysqlDB)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ func (d *V12RecommendFoodRepository) FindOneV12RecommendFood(ctx context.Context
if result.RowsAffected == 0 {
return nil, utils.ErrorMsg(ctx, utils.ErrFoodNotFound, utils.Trace(), "no matching record found", utils.ErrFromClient)
}

return &food, nil // 반환할 때 포인터로 반환
}

Expand Down
2 changes: 1 addition & 1 deletion src/features/food/repository/v1RecommendFoodRepository.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (d *V1RecommendFoodRepository) FindOneV1RecommendFood(ctx context.Context,
func (d *V1RecommendFoodRepository) SaveRecommendFood(ctx context.Context, foodDTO *mysql.Foods) (*mysql.Foods, error) {
foods := mysql.Foods{}
// 존재 여부 확인
err := d.GormDB.WithContext(ctx).Model(&foods).Where("name = ? AND time_id = ? AND type_id = ? AND scenario_id = ? and theme_id = ? ", foodDTO.Name, foodDTO.TimeID, foodDTO.TypeID, foodDTO.ScenarioID, foodDTO.ThemeID).First(&foods).Error
err := d.GormDB.WithContext(ctx).Model(&foods).Where("name = ? ", foodDTO.Name).First(&foods).Error

if err == nil {
// 데이터가 이미 존재함
Expand Down
2 changes: 1 addition & 1 deletion src/features/food/usecase/dailyRecommendFoodUseCase.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (d *DailyRecommendFoodUseCase) DailyRecommend(c context.Context) (response.
res := response.ResDailyRecommendFood{}
for _, food := range foods {
// 음식 이미지를 가져온다.
foodImage, err := d.Repository.FindOneFoodImage(ctx, food.FoodImageID)
foodImage, err := d.Repository.FindOneFoodImage(ctx, food.ImageID)
if err != nil {
return response.ResDailyRecommendFood{}, err
}
Expand Down
Loading

0 comments on commit 9ea6459

Please sign in to comment.