diff --git a/api/app/user.go b/api/app/user.go index b762e78..94627ed 100644 --- a/api/app/user.go +++ b/api/app/user.go @@ -104,3 +104,40 @@ func (u userApi) UpdateUser(ctx *gin.Context) { // 操作成功,更新头像和昵称 core.R(ctx).Ok() } + +// GetLikeWineList 获取我喜欢的酒单列表 +func (u userApi) GetLikeWineList(ctx *gin.Context) { + var p param.GetLikeWineList + if err := ctx.ShouldBind(&p); err != nil { + core.R(ctx).FailWithMessage("参数错误: " + err.Error()) + return + } + + // 获取我喜欢的酒单列表 + wines, err := repository.User().GetLikeWineList(&p) + if err != nil { + log.Errorf("获取我喜欢的酒单列表失败:%s", err.Error()) + core.R(ctx).FailWithMessage("系统错误,请稍后再试") + return + } + + core.R(ctx).OkWithData(wines) +} + +// DeleteLikeWine 删除我喜欢的酒单 +func (u userApi) DeleteLikeWine(ctx *gin.Context) { + var p param.DeleteLikeWine + if err := ctx.ShouldBind(&p); err != nil { + core.R(ctx).FailWithMessage("参数错误: " + err.Error()) + return + } + + // 删除我喜欢的酒单 + if err := repository.User().DeleteLikeWine(&p); err != nil { + log.Errorf("删除我喜欢的酒单失败:%s", err.Error()) + core.R(ctx).FailWithMessage("系统错误,请稍后再试") + return + } + + core.R(ctx).Ok() +} diff --git a/api/app/wine.go b/api/app/wine.go index 1f57b70..d4b9046 100644 --- a/api/app/wine.go +++ b/api/app/wine.go @@ -2,6 +2,7 @@ package app import ( "Lee-WineList/core" + "Lee-WineList/model/entity" "Lee-WineList/model/param" "Lee-WineList/repository" "github.com/gin-gonic/gin" @@ -34,7 +35,7 @@ func (w *wineApi) GetList(ctx *gin.Context) { // Add 添加酒单 func (w *wineApi) Add(ctx *gin.Context) { - var p param.AddWine + var p entity.Wine if err := ctx.ShouldBind(&p); err != nil { core.R(ctx).FailWithMessage(err.Error()) @@ -48,3 +49,37 @@ func (w *wineApi) Add(ctx *gin.Context) { core.R(ctx).OkWithMessage("添加成功") } + +// Update 更新酒单 +func (w *wineApi) Update(ctx *gin.Context) { + var p entity.Wine + + if err := ctx.ShouldBind(&p); err != nil { + core.R(ctx).FailWithMessage(err.Error()) + return + } + + if err := repository.Wine().Update(p); err != nil { + core.R(ctx).FailWithMessage(err.Error()) + return + } + + core.R(ctx).OkWithMessage("更新成功") +} + +// Delete 删除酒单 +func (w *wineApi) Delete(ctx *gin.Context) { + var p entity.Wine + + if err := ctx.ShouldBind(&p); err != nil { + core.R(ctx).FailWithMessage(err.Error()) + return + } + + if err := repository.Wine().Delete(p); err != nil { + core.R(ctx).FailWithMessage(err.Error()) + return + } + + core.R(ctx).OkWithMessage("删除成功") +} diff --git a/initialize/db_table.go b/initialize/db_table.go index feafe39..d7edfda 100644 --- a/initialize/db_table.go +++ b/initialize/db_table.go @@ -11,6 +11,9 @@ func databaseTable() { dbs := []any{ new(entity.User), // 普通用户 new(entity.OAuth2Client), // OAuth2客户端 + new(entity.Wine), // 酒单 + new(entity.Material), // 酒单材料 + new(entity.LikeWine), // 喜欢的酒单 } diff --git a/model/entity/like_wine.go b/model/entity/like_wine.go new file mode 100644 index 0000000..9784f60 --- /dev/null +++ b/model/entity/like_wine.go @@ -0,0 +1,13 @@ +package entity + +import "Lee-WineList/common/types" + +type LikeWine struct { + types.BaseDbModel + UserId int `gorm:"column:user_id;type:int(11) unsigned;comment:用户ID;NOT NULL" json:"user_id"` + WineId int `gorm:"column:wine_id;type:int(11) unsigned;comment:酒ID;NOT NULL" json:"wine_id"` +} + +func (LikeWine) TableName() string { + return "like_wines" +} diff --git a/model/entity/material.go b/model/entity/material.go index 2e9d952..707d10f 100644 --- a/model/entity/material.go +++ b/model/entity/material.go @@ -4,9 +4,10 @@ import "Lee-WineList/common/types" type Material struct { types.BaseDbModel - Name string `gorm:"column:name;type:varchar(255);comment:材料名;NOT NULL" json:"name"` - ML string `gorm:"column:ml;type:int(11) unsigned;comment:毫升;NOT NULL" json:"ml"` - WindId int `gorm:"column:wine_id;type:int(11) unsigned;comment:酒ID;NOT NULL" json:"wine_id"` + Name string `gorm:"column:name;type:varchar(255);comment:材料名;NOT NULL" json:"name"` + ML string `gorm:"column:ml;type:int(11) unsigned;comment:毫升" json:"ml"` + OtherUnit string `gorm:"column:other_unit;type:varchar(255);comment:其他单位" json:"other_unit"` + WindId int `gorm:"column:wine_id;type:int(11) unsigned;comment:酒ID;NOT NULL" json:"wine_id"` } func (Material) TableName() string { diff --git a/model/entity/wine.go b/model/entity/wine.go index 6abf47c..e576efb 100644 --- a/model/entity/wine.go +++ b/model/entity/wine.go @@ -12,7 +12,7 @@ type Wine struct { Category string `gorm:"column:category;type:varchar(255);comment:分类;NOT NULL" json:"category"` Steps string `gorm:"column:steps;type:varchar(255);comment:步骤;NOT NULL" json:"steps"` Context string `gorm:"column:context;type:varchar(255);comment:酒文;NOT NULL" json:"context"` - Materials []Material `gorm:"many2many:wine_materials"` + Materials []Material `gorm:"many2many:wine_materials" json:"materials"` UserId int `gorm:"column:user_id;type:int(11) unsigned;comment:用户ID;NOT NULL" json:"user_id"` } diff --git a/model/param/user.go b/model/param/user.go index e5cd078..3d7dbdc 100644 --- a/model/param/user.go +++ b/model/param/user.go @@ -42,3 +42,13 @@ type SaveUser struct { Phone string `json:"phone" form:"phone"` Status constant.UserStatus `json:"status" form:"status" binding:"oneof=NORMAL DISABLE"` // 用户状态 } + +// GetLikeWineList 获取酒列表 +type GetLikeWineList struct { + page + UserId int `json:"userId" form:"userId" binding:"required"` // 用户ID +} + +type DeleteLikeWine struct { + LikeWineId int `json:"likeWineId" form:"likeWineId" binding:"required"` +} diff --git a/model/param/wine.go b/model/param/wine.go index bc5bae2..5d5564f 100644 --- a/model/param/wine.go +++ b/model/param/wine.go @@ -11,5 +11,3 @@ type GetWineListByUser struct { Category string `json:"category" form:"category"` // 分类 UserId int `json:"userId" form:"userId"` // 用户ID } - -type AddWine struct{} diff --git a/repository/user.go b/repository/user.go index 7562ac2..8b79f6a 100644 --- a/repository/user.go +++ b/repository/user.go @@ -3,6 +3,7 @@ package repository import ( "Lee-WineList/client" "Lee-WineList/model/entity" + "Lee-WineList/model/param" "errors" "git.echol.cn/loser/logger/log" "gorm.io/gorm" @@ -54,3 +55,20 @@ func (user) CheckUnionIdIsExist(unionId, openId string) bool { func (user) UpdateUserInfo(e *entity.User) (err error) { return client.MySQL.Updates(&e).Error } + +// GetLikeWineList 获取用户喜欢的酒单列表 +func (u user) GetLikeWineList(e *param.GetLikeWineList) (wines []entity.Wine, err error) { + var ids []int64 + client.MySQL.Model(&entity.LikeWine{}).Where("user_id = ?", e.UserId).Pluck("wine_id", &ids) + + err = client.MySQL.Model(&entity.Wine{}). + Where("wine_id in ?", ids). + Preload("Materials"). + Scopes(page(e.Current, e.Size)). + Find(&wines).Error + return +} + +func (u user) DeleteLikeWine(p *param.DeleteLikeWine) (err error) { + return client.MySQL.Delete(&entity.LikeWine{}, "wine_id = ?", p.LikeWineId).Error +} diff --git a/repository/wine.go b/repository/wine.go index 6d9245f..2cd89b5 100644 --- a/repository/wine.go +++ b/repository/wine.go @@ -34,7 +34,17 @@ func (w *wine) GetWineList(p param.GetWineList) (wines []entity.Wine, err error) return } -func (w *wine) Add(p param.AddWine) (err error) { +func (w *wine) Add(p entity.Wine) (err error) { + err = client.MySQL.Create(&p).Error + return +} + +func (w *wine) Update(p entity.Wine) (err error) { + err = client.MySQL.Save(&p).Error + return +} +func (w *wine) Delete(p entity.Wine) (err error) { + err = client.MySQL.Delete(&p).Error return } diff --git a/router/route.go b/router/route.go index 0ca8de6..9fb0f52 100644 --- a/router/route.go +++ b/router/route.go @@ -1,13 +1,12 @@ package router import ( - "Lee-WineList/middleware" "github.com/gin-gonic/gin" ) // InitRoute 初始化路由 func InitRoute(g *gin.RouterGroup) { - login(g) // 登录相关路由 - user(g.Group("/user", middleware.AuthorizeToken())) // 用户相关路由 + login(g) // 登录相关路由 + user(g.Group("/user")) // 用户相关路由 wine(g.Group("/wine")) } diff --git a/router/user.go b/router/user.go index bdfa134..4920965 100644 --- a/router/user.go +++ b/router/user.go @@ -10,4 +10,5 @@ func user(g *gin.RouterGroup) { g.GET("", app.UserApi().GetUser) // 获取当前登录用户信息 g.POST("/binding/wechat", app.UserApi().BindingWeChat) // 绑定微信 g.POST("/update", app.UserApi().UpdateUser) // 修改用户信息 + g.GET("/like", app.UserApi().GetLikeWineList) // 获取用户收藏的酒品 } diff --git a/router/wine.go b/router/wine.go index 8d00506..b2d30e9 100644 --- a/router/wine.go +++ b/router/wine.go @@ -7,4 +7,7 @@ import ( func wine(g *gin.RouterGroup) { g.GET("", app.WineApi().GetList) + g.POST("", app.WineApi().Add) + g.PUT("", app.WineApi().Update) + g.DELETE("", app.WineApi().Delete) }