Skip to content

Commit

Permalink
setmeal 抽象接口
Browse files Browse the repository at this point in the history
  • Loading branch information
luminescenceJ committed Jan 7, 2025
1 parent 12b056d commit 912ff3a
Show file tree
Hide file tree
Showing 14 changed files with 365 additions and 0 deletions.
14 changes: 14 additions & 0 deletions global/tx/transactionManager.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package tx

// Transaction 定义事务管理器接口
// 因为不同数据库的事务管理方式不同,所以这里使用接口来定义事务管理器的功能
// 具体的实现可以由不同的数据库驱动来实现
// 例如,我们当前项目的Dao层为了引入事务破坏了 `依赖倒置` 原则,所以我们的目标是面向抽象
type Transaction interface {
// Begin 开始一个事务
Begin() error
// Commit 提交事务
Commit() error
// Rollback 回滚事务
Rollback()
}
1 change: 1 addition & 0 deletions initialize/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func routerInit() *gin.Engine {
allRouter.CategoryRouter.InitApiRouter(admin)
allRouter.DishRouter.InitApiRouter(admin)
allRouter.CommonRouter.InitApiRouter(admin)
allRouter.SetMealRouter.InitApiRouter(admin)
}
return r
}
56 changes: 56 additions & 0 deletions internal/api/controller/setmeal_controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package controller

import (
"github.com/gin-gonic/gin"
"net/http"
"takeout/common"
"takeout/common/e"
"takeout/internal/service"
)

type SetMealController struct {
service service.ISetMealService
}

func NewSetMealController(service service.ISetMealService) *SetMealController {
return &SetMealController{service: service}
}

// SaveWithDish 保存套餐和菜品信息
func (sc *SetMealController) SaveWithDish(ctx *gin.Context) {
code := e.SUCCESS
ctx.JSON(http.StatusOK, common.Result{
Code: code,
Msg: e.GetMsg(code),
})
}

// PageQuery 套餐分页查询
func (sc *SetMealController) PageQuery(ctx *gin.Context) {
code := e.SUCCESS

ctx.JSON(http.StatusOK, common.Result{
Code: code,
Msg: e.GetMsg(code),
})
}

// OnOrClose 套餐启用禁用
func (sc *SetMealController) OnOrClose(ctx *gin.Context) {
code := e.SUCCESS

ctx.JSON(http.StatusOK, common.Result{
Code: code,
Msg: e.GetMsg(code),
})
}

// GetByIdWithDish 根据套餐id获取套餐和关联菜品信息
func (sc *SetMealController) GetByIdWithDish(ctx *gin.Context) {
code := e.SUCCESS

ctx.JSON(http.StatusOK, common.Result{
Code: code,
Msg: e.GetMsg(code),
})
}
22 changes: 22 additions & 0 deletions internal/api/request/setmeal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package request

import "takeout/internal/model"

type SetMealDTO struct {
Id uint64 `json:"id"` // 主键id
CategoryId uint64 `json:"categoryId"` // 分类id
Name string `json:"name"` // 套餐名称
Price string `json:"price"` // 套餐单价
Status int `json:"status"` // 套餐状态
Description string `json:"description"` // 套餐描述
Image string `json:"image"` // 套餐图片
SetMealDishs []model.SetMealDish `json:"setmealDishes"` // 套餐菜品关系
}

type SetMealPageQueryDTO struct {
Page int `form:"page"` // 分页查询的页数
PageSize int `form:"pageSize"` // 分页查询的页容量
Name string `form:"name"` // 分页查询的name
CategoryId uint64 `form:"categoryId"` // 分类ID:
Status int `form:"status"` // 套餐起售状态
}
34 changes: 34 additions & 0 deletions internal/api/response/setmeal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package response

import (
"takeout/internal/model"
"time"
)

type SetMealPageQueryVo struct {
Id uint64 `json:"id" ` // 主键id
CategoryId uint64 `json:"categoryId"` // 分类id
Name string `json:"name"` // 套餐名称
Price float64 `json:"price"` // 套餐单价
Status int `json:"status"` // 套餐状态
Description string `json:"description"` // 套餐描述
Image string `json:"image"` // 套餐图片
CreateTime time.Time `json:"createTime"` // 创建时间
UpdateTime time.Time `json:"updateTime"` // 更新时间
CreateUser uint64 `json:"createUser"` // 创建用户
UpdateUser uint64 `json:"updateUser"` // 更新用户
CategoryName string `json:"categoryName"` // 分类名称
}

type SetMealWithDishByIdVo struct {
Id uint64 `json:"id"`
CategoryId uint64 `json:"categoryId"`
CategoryName string `json:"categoryName"`
Description string `json:"description"`
Image string `json:"image"`
Name string `json:"name"`
Price float64 `json:"price"`
SetmealDishes []model.SetMealDish `json:"setmealDishes"`
Status int `json:"status"`
UpdateTime time.Time `json:"updateTime"`
}
49 changes: 49 additions & 0 deletions internal/model/setmeal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package model

import (
"gorm.io/gorm"
"takeout/common/enum"
"time"
)

type SetMeal struct {
Id uint64 `json:"id" gorm:"primaryKey;AUTO_INCREMENT"` // 主键id
CategoryId uint64 `json:"category_id"` // 分类id
Name string `json:"name"` // 套餐名称
Price float64 `json:"price"` // 套餐单价
Status int `json:"status"` // 套餐状态
Description string `json:"description"` // 套餐描述
Image string `json:"image"` // 套餐图片
CreateTime time.Time `json:"create_time"` // 创建时间
UpdateTime time.Time `json:"update_time"` // 更新时间
CreateUser uint64 `json:"create_user"` // 创建用户
UpdateUser uint64 `json:"update_user"` // 更新用户
}

func (e *SetMeal) BeforeCreate(tx *gorm.DB) error {
// 自动填充 创建时间、创建人、更新时间、更新用户
e.CreateTime = time.Now()
e.UpdateTime = time.Now()
// 从上下文获取用户信息
value := tx.Statement.Context.Value(enum.CurrentId)
if uid, ok := value.(uint64); ok {
e.CreateUser = uid
e.UpdateUser = uid
}
return nil
}

func (e *SetMeal) BeforeUpdate(tx *gorm.DB) error {
// 在更新记录千自动填充更新时间
e.UpdateTime = time.Now()
// 从上下文获取用户信息
value := tx.Statement.Context.Value(enum.CurrentId)
if uid, ok := value.(uint64); ok {
e.UpdateUser = uid
}
return nil
}

func (e *SetMeal) TableName() string {
return "setmeal"
}
14 changes: 14 additions & 0 deletions internal/model/setmeal_dish.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package model

type SetMealDish struct {
Id uint64 `json:"id"` // 中间表id
SetmealId uint64 `json:"setmealId"` // 套餐id
DishId uint64 `json:"dishId"` // 菜品id
Name string `json:"name"` // 菜品名称冗余字段
Price float64 `json:"price"` // 菜品单价冗余字段
Copies int `json:"copies"` // 菜品数量冗余字段
}

func (e *SetMealDish) TableName() string {
return "setmeal_dish"
}
30 changes: 30 additions & 0 deletions internal/router/admin/setmeal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package admin

import (
"github.com/gin-gonic/gin"
"takeout/global"
"takeout/internal/api/controller"
"takeout/internal/service"
"takeout/middle"
"takeout/repository/dao"
)

type SetMealRouter struct {
service service.ISetMealService
}

func (er *SetMealRouter) InitApiRouter(router *gin.RouterGroup) {
// admin/employee
privateRouter := router.Group("setmeal")
// 私有路由使用jwt验证
privateRouter.Use(middle.VerifiyJWTAdmin())
// 依赖注入
er.service = service.NewSetMealService(dao.NewSetMealDao(global.DB), dao.NewSetMealDishDao())
setmealCtrl := controller.NewSetMealController(er.service)
{
privateRouter.POST("", setmealCtrl.SaveWithDish)
privateRouter.GET("/page", setmealCtrl.PageQuery)
privateRouter.GET("/:id", setmealCtrl.GetByIdWithDish)
privateRouter.POST("/status/:status", setmealCtrl.OnOrClose)
}
}
1 change: 1 addition & 0 deletions internal/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ type RouterGroup struct {
admin.CategoryRouter
admin.DishRouter
admin.CommonRouter
admin.SetMealRouter
}

var AllRouter = new(RouterGroup)
48 changes: 48 additions & 0 deletions internal/service/setmeal_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package service

import (
"context"
"takeout/common"
"takeout/internal/api/request"
"takeout/internal/api/response"
"takeout/repository"
)

type ISetMealService interface {
SaveWithDish(ctx context.Context, dto request.SetMealDTO) error
PageQuery(ctx context.Context, dto request.SetMealPageQueryDTO) (*common.PageResult, error)
OnOrClose(ctx context.Context, id uint64, status int) error
GetByIdWithDish(ctx context.Context, dishId uint64) (response.SetMealWithDishByIdVo, error)
}

type SetMealServiceImpl struct {
repo repository.SetMealRepo
setMealDishRepo repository.SetMealDishRepo
}

func (s SetMealServiceImpl) SaveWithDish(ctx context.Context, dto request.SetMealDTO) error {
//TODO implement me
panic("implement me")
}

func (s SetMealServiceImpl) PageQuery(ctx context.Context, dto request.SetMealPageQueryDTO) (*common.PageResult, error) {
//TODO implement me
panic("implement me")
}

func (s SetMealServiceImpl) OnOrClose(ctx context.Context, id uint64, status int) error {
//TODO implement me
panic("implement me")
}

func (s SetMealServiceImpl) GetByIdWithDish(ctx context.Context, dishId uint64) (response.SetMealWithDishByIdVo, error) {
//TODO implement me
panic("implement me")
}

func NewSetMealService(repo repository.SetMealRepo, setMealDishRepo repository.SetMealDishRepo) ISetMealService {
return &SetMealServiceImpl{
repo: repo,
setMealDishRepo: setMealDishRepo,
}
}
44 changes: 44 additions & 0 deletions repository/dao/setmeal_dao.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package dao

import (
"context"
"gorm.io/gorm"
"takeout/common"
"takeout/global/tx"
"takeout/internal/api/request"
"takeout/internal/model"
"takeout/repository"
)

type SetMealDao struct {
db *gorm.DB
}

func (s SetMealDao) Transaction(ctx context.Context) tx.Transaction {
//TODO implement me
panic("implement me")
}

func (s SetMealDao) Insert(db tx.Transaction, meal *model.SetMeal) error {
//TODO implement me
panic("implement me")
}

func (s SetMealDao) PageQuery(ctx context.Context, dto request.SetMealPageQueryDTO) (*common.PageResult, error) {
//TODO implement me
panic("implement me")
}

func (s SetMealDao) SetStatus(ctx context.Context, id uint64, status int) error {
//TODO implement me
panic("implement me")
}

func (s SetMealDao) GetByIdWithDish(db tx.Transaction, dishId uint64) (model.SetMeal, error) {
//TODO implement me
panic("implement me")
}

func NewSetMealDao(db *gorm.DB) repository.SetMealRepo {
return &SetMealDao{db: db}
}
24 changes: 24 additions & 0 deletions repository/dao/setmeal_dish_dao.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package dao

import (
"takeout/global/tx"
"takeout/internal/model"
"takeout/repository"
)

type SetMealDishDao struct {
}

func (s SetMealDishDao) InsertBatch(db tx.Transaction, setmealDishs []model.SetMealDish) error {
//TODO implement me
panic("implement me")
}

func (s SetMealDishDao) GetBySetMealId(db tx.Transaction, SetMealId uint64) ([]model.SetMealDish, error) {
//TODO implement me
panic("implement me")
}

func NewSetMealDishDao() repository.SetMealDishRepo {
return &SetMealDishDao{}
}
11 changes: 11 additions & 0 deletions repository/setmeal_dish_repo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package repository

import (
"takeout/global/tx"
"takeout/internal/model"
)

type SetMealDishRepo interface {
InsertBatch(db tx.Transaction, setmealDishs []model.SetMealDish) error
GetBySetMealId(db tx.Transaction, SetMealId uint64) ([]model.SetMealDish, error)
}
17 changes: 17 additions & 0 deletions repository/setmeal_repo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package repository

import (
"context"
"takeout/common"
"takeout/global/tx"
"takeout/internal/api/request"
"takeout/internal/model"
)

type SetMealRepo interface {
Transaction(ctx context.Context) tx.Transaction
Insert(db tx.Transaction, meal *model.SetMeal) error
PageQuery(ctx context.Context, dto request.SetMealPageQueryDTO) (*common.PageResult, error)
SetStatus(ctx context.Context, id uint64, status int) error
GetByIdWithDish(db tx.Transaction, dishId uint64) (model.SetMeal, error)
}

0 comments on commit 912ff3a

Please sign in to comment.