Skip to content

Commit

Permalink
{feat} - 음식 정보 없는 음식 리포트 Api 구현 \n #228
Browse files Browse the repository at this point in the history
  • Loading branch information
JokerTrickster committed Nov 24, 2024
1 parent 154b493 commit c57850e
Show file tree
Hide file tree
Showing 15 changed files with 396 additions and 1 deletion.
28 changes: 28 additions & 0 deletions src/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,34 @@ const docTemplate = `{
}
}
},
"/v0.1/system/foods/report": {
"post": {
"description": "■ errCode with 400\nPARAM_BAD : 파라미터 오류\nUSER_NOT_EXIST : 유저가 존재하지 않음\nUSER_ALREADY_EXISTED : 유저가 이미 존재\n\n■ errCode with 500\nINTERNAL_SERVER : 내부 로직 처리 실패\nINTERNAL_DB : DB 처리 실패\nPLAYER_STATE_CHANGE_FAILED : 플레이어 상태 변경 실패",
"produces": [
"application/json"
],
"tags": [
"system"
],
"summary": "음식 이미지, 영양 성분 없는 음식 리포트",
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "boolean"
}
},
"400": {
"description": "Bad Request",
"schema": {}
},
"500": {
"description": "Internal Server Error",
"schema": {}
}
}
}
},
"/v0.1/system/report": {
"post": {
"description": "■ errCode with 400\nPARAM_BAD : 파라미터 오류\nUSER_NOT_EXIST : 유저가 존재하지 않음\nUSER_ALREADY_EXISTED : 유저가 이미 존재\n\n■ errCode with 500\nINTERNAL_SERVER : 내부 로직 처리 실패\nINTERNAL_DB : DB 처리 실패\nPLAYER_STATE_CHANGE_FAILED : 플레이어 상태 변경 실패",
Expand Down
28 changes: 28 additions & 0 deletions src/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -875,6 +875,34 @@
}
}
},
"/v0.1/system/foods/report": {
"post": {
"description": "■ errCode with 400\nPARAM_BAD : 파라미터 오류\nUSER_NOT_EXIST : 유저가 존재하지 않음\nUSER_ALREADY_EXISTED : 유저가 이미 존재\n\n■ errCode with 500\nINTERNAL_SERVER : 내부 로직 처리 실패\nINTERNAL_DB : DB 처리 실패\nPLAYER_STATE_CHANGE_FAILED : 플레이어 상태 변경 실패",
"produces": [
"application/json"
],
"tags": [
"system"
],
"summary": "음식 이미지, 영양 성분 없는 음식 리포트",
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "boolean"
}
},
"400": {
"description": "Bad Request",
"schema": {}
},
"500": {
"description": "Internal Server Error",
"schema": {}
}
}
}
},
"/v0.1/system/report": {
"post": {
"description": "■ errCode with 400\nPARAM_BAD : 파라미터 오류\nUSER_NOT_EXIST : 유저가 존재하지 않음\nUSER_ALREADY_EXISTED : 유저가 이미 존재\n\n■ errCode with 500\nINTERNAL_SERVER : 내부 로직 처리 실패\nINTERNAL_DB : DB 처리 실패\nPLAYER_STATE_CHANGE_FAILED : 플레이어 상태 변경 실패",
Expand Down
28 changes: 28 additions & 0 deletions src/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1378,6 +1378,34 @@ paths:
summary: 음식 선택하기
tags:
- food
/v0.1/system/foods/report:
post:
description: |-
■ errCode with 400
PARAM_BAD : 파라미터 오류
USER_NOT_EXIST : 유저가 존재하지 않음
USER_ALREADY_EXISTED : 유저가 이미 존재
■ errCode with 500
INTERNAL_SERVER : 내부 로직 처리 실패
INTERNAL_DB : DB 처리 실패
PLAYER_STATE_CHANGE_FAILED : 플레이어 상태 변경 실패
produces:
- application/json
responses:
"200":
description: OK
schema:
type: boolean
"400":
description: Bad Request
schema: {}
"500":
description: Internal Server Error
schema: {}
summary: 음식 이미지, 영양 성분 없는 음식 리포트
tags:
- system
/v0.1/system/report:
post:
description: |-
Expand Down
49 changes: 49 additions & 0 deletions src/features/system/handler/foodReportSystemHandler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package handler

import (
"context"
_interface "main/features/system/model/interface"
"net/http"

"github.com/labstack/echo/v4"
)

type FoodReportSystemHandler struct {
UseCase _interface.IFoodReportSystemUseCase
}

func NewFoodReportSystemHandler(c *echo.Echo, useCase _interface.IFoodReportSystemUseCase) _interface.IFoodReportSystemHandler {
handler := &FoodReportSystemHandler{
UseCase: useCase,
}
c.POST("/v0.1/system/foods/report", handler.FoodReport)
return handler
}

// 음식 이미지, 영양 성분 없는 음식 리포트
// @Router /v0.1/system/foods/report [post]
// @Summary 음식 이미지, 영양 성분 없는 음식 리포트
// @Description
// @Description ■ errCode with 400
// @Description PARAM_BAD : 파라미터 오류
// @Description USER_NOT_EXIST : 유저가 존재하지 않음
// @Description USER_ALREADY_EXISTED : 유저가 이미 존재
// @Description
// @Description ■ errCode with 500
// @Description INTERNAL_SERVER : 내부 로직 처리 실패
// @Description INTERNAL_DB : DB 처리 실패
// @Description PLAYER_STATE_CHANGE_FAILED : 플레이어 상태 변경 실패
// @Produce json
// @Success 200 {object} bool
// @Failure 400 {object} error
// @Failure 500 {object} error
// @Tags system
func (d *FoodReportSystemHandler) FoodReport(c echo.Context) error {
ctx := context.Background()
err := d.UseCase.FoodReport(ctx)
if err != nil {
return err
}

return c.JSON(http.StatusOK, true)
}
1 change: 1 addition & 0 deletions src/features/system/handler/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ import (

func NewSystemHandler(c *echo.Echo) {
NewReportSystemHandler(c, usecase.NewReportSystemUseCase(repository.NewReportSystemRepository(mysql.GormMysqlDB), mysql.DBTimeOut))
NewFoodReportSystemHandler(c, usecase.NewFoodReportSystemUseCase(repository.NewFoodReportSystemRepository(mysql.GormMysqlDB), mysql.DBTimeOut))
}
4 changes: 4 additions & 0 deletions src/features/system/model/interface/ISystemHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ import "github.com/labstack/echo/v4"
type IReportSystemHandler interface {
Report(c echo.Context) error
}

type IFoodReportSystemHandler interface {
FoodReport(c echo.Context) error
}
5 changes: 5 additions & 0 deletions src/features/system/model/interface/ISystemRepository.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,8 @@ import (
type IReportSystemRepository interface {
SaveReport(ctx context.Context, reportDTO *mysql.Reports) error
}

type IFoodReportSystemRepository interface {
FindFoodWithoutImage(ctx context.Context) ([]*mysql.FoodImages, error)
FindFoodWithoutNutrient(ctx context.Context) ([]*mysql.Foods, error)
}
4 changes: 4 additions & 0 deletions src/features/system/model/interface/ISystemUseCase.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ import (
type IReportSystemUseCase interface {
Report(c context.Context, uID uint, req *request.ReqReport) error
}

type IFoodReportSystemUseCase interface {
FoodReport(c context.Context) error
}
42 changes: 42 additions & 0 deletions src/features/system/repository/foodReportSystemRepository.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package repository

import (
"context"
_interface "main/features/system/model/interface"
"main/utils"
"main/utils/db/mysql"

"gorm.io/gorm"
)

func NewFoodReportSystemRepository(gormDB *gorm.DB) _interface.IFoodReportSystemRepository {
return &FoodReportSystemRepository{GormDB: gormDB}
}

func (d *FoodReportSystemRepository) FindFoodWithoutImage(ctx context.Context) ([]*mysql.FoodImages, error) {
var foodImages []*mysql.FoodImages
result := d.GormDB.Where("image = ?", "food_default.png").Find(&foodImages)
if result.Error != nil {
return nil, utils.ErrorMsg(ctx, utils.ErrInternalDB, utils.Trace(), utils.HandleError(result.Error.Error(), foodImages), utils.ErrFromMysqlDB)
}
return foodImages, nil
}

func (d *FoodReportSystemRepository) FindFoodWithoutNutrient(ctx context.Context) ([]*mysql.Foods, error) {

var foodsWithoutNutrients []*mysql.Foods

query := `
SELECT f.*
FROM foods f
LEFT JOIN nutrients n ON f.name = n.food_name
WHERE n.food_name IS NULL AND f.deleted_at IS NULL
`

if err := d.GormDB.WithContext(ctx).Raw(query).Scan(&foodsWithoutNutrients).Error; err != nil {
return nil, utils.ErrorMsg(ctx, utils.ErrInternalDB, utils.Trace(), utils.HandleError(err.Error(), foodsWithoutNutrients), utils.ErrFromMysqlDB)
}

return foodsWithoutNutrients, nil

}
4 changes: 4 additions & 0 deletions src/features/system/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ import "gorm.io/gorm"
type ReportSystemRepository struct {
GormDB *gorm.DB
}

type FoodReportSystemRepository struct {
GormDB *gorm.DB
}
46 changes: 46 additions & 0 deletions src/features/system/usecase/foodReportSystemUseCase.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package usecase

import (
"context"
_interface "main/features/system/model/interface"
"main/utils/aws"
"time"
)

type FoodReportSystemUseCase struct {
Repository _interface.IFoodReportSystemRepository
ContextTimeout time.Duration
}

func NewFoodReportSystemUseCase(repo _interface.IFoodReportSystemRepository, timeout time.Duration) _interface.IFoodReportSystemUseCase {
return &FoodReportSystemUseCase{Repository: repo, ContextTimeout: timeout}
}

func (d *FoodReportSystemUseCase) FoodReport(c context.Context) error {
ctx, cancel := context.WithTimeout(c, d.ContextTimeout)
defer cancel()

//음식 이미지가 없는 음식 리스트 조회
images, err := d.Repository.FindFoodWithoutImage(ctx)
if err != nil {
return err
}

//음식 영양성분이 없는 음식 리스트 조회
nutrients, err := d.Repository.FindFoodWithoutNutrient(ctx)
if err != nil {
return err
}
imageList := make([]string, 0)
nutrientList := make([]string, 0)
for _, image := range images {
imageList = append(imageList, image.Name)
}
for _, nutrient := range nutrients {
nutrientList = append(nutrientList, nutrient.Name)
}

go aws.EmailSendFoodInfoEmptyReport(imageList, nutrientList)

return nil
}
2 changes: 1 addition & 1 deletion src/features/system/usecase/reportSystemUseCase.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (d *ReportSystemUseCase) Report(c context.Context, uID uint, req *request.R
UserID: strconv.Itoa(int(uID)),
Reason: string(req.Reason),
}
go aws.EmailSendReport([]string{"[email protected]", "[email protected]"}, reqReport)
go aws.EmailSendReport([]string{"[email protected]", "[email protected]", "[email protected]"}, reqReport)

return nil
}
14 changes: 14 additions & 0 deletions src/utils/aws/ses.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,20 @@ type ReqReportSES struct {
Reason string
}


func EmailSendFoodInfoEmptyReport(imageEmpty, nutrientEmpty []string) {
templateDataMap := map[string][]string{
"imageMissingFoods": imageEmpty,
"nutrientMissingFoods": nutrientEmpty,
}
templateDataJson, err := json.Marshal(templateDataMap)
if err != nil {
fmt.Println("Error marshaling template data:", err)
return
}

emailSend([]string{"[email protected]", "[email protected]", "[email protected]"}, emailTypeFoodNameReport, string(templateDataJson), "foodInfoEmptyReport")
}
func EmailSendFoodUploadReport(successFoodList, failedFoodList []string) {
templateDataMap := map[string]string{
"successFoodList": strings.Join(successFoodList, ", "),
Expand Down
Loading

0 comments on commit c57850e

Please sign in to comment.