🎨 新增酒单CRUD & 喜欢的酒单列表CRUD

This commit is contained in:
loser 2023-04-27 17:36:07 +08:00
parent d6e256ef9e
commit 1212e999cc
13 changed files with 140 additions and 12 deletions

View File

@ -104,3 +104,40 @@ func (u userApi) UpdateUser(ctx *gin.Context) {
// 操作成功,更新头像和昵称 // 操作成功,更新头像和昵称
core.R(ctx).Ok() 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()
}

View File

@ -2,6 +2,7 @@ package app
import ( import (
"Lee-WineList/core" "Lee-WineList/core"
"Lee-WineList/model/entity"
"Lee-WineList/model/param" "Lee-WineList/model/param"
"Lee-WineList/repository" "Lee-WineList/repository"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@ -34,7 +35,7 @@ func (w *wineApi) GetList(ctx *gin.Context) {
// Add 添加酒单 // Add 添加酒单
func (w *wineApi) Add(ctx *gin.Context) { func (w *wineApi) Add(ctx *gin.Context) {
var p param.AddWine var p entity.Wine
if err := ctx.ShouldBind(&p); err != nil { if err := ctx.ShouldBind(&p); err != nil {
core.R(ctx).FailWithMessage(err.Error()) core.R(ctx).FailWithMessage(err.Error())
@ -48,3 +49,37 @@ func (w *wineApi) Add(ctx *gin.Context) {
core.R(ctx).OkWithMessage("添加成功") 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("删除成功")
}

View File

@ -11,6 +11,9 @@ func databaseTable() {
dbs := []any{ dbs := []any{
new(entity.User), // 普通用户 new(entity.User), // 普通用户
new(entity.OAuth2Client), // OAuth2客户端 new(entity.OAuth2Client), // OAuth2客户端
new(entity.Wine), // 酒单
new(entity.Material), // 酒单材料
new(entity.LikeWine), // 喜欢的酒单
} }

13
model/entity/like_wine.go Normal file
View File

@ -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"
}

View File

@ -4,9 +4,10 @@ import "Lee-WineList/common/types"
type Material struct { type Material struct {
types.BaseDbModel types.BaseDbModel
Name string `gorm:"column:name;type:varchar(255);comment:材料名;NOT NULL" json:"name"` 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"` ML string `gorm:"column:ml;type:int(11) unsigned;comment:毫升" json:"ml"`
WindId int `gorm:"column:wine_id;type:int(11) unsigned;comment:酒ID;NOT NULL" json:"wine_id"` 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 { func (Material) TableName() string {

View File

@ -12,7 +12,7 @@ type Wine struct {
Category string `gorm:"column:category;type:varchar(255);comment:分类;NOT NULL" json:"category"` 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"` 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"` 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"` UserId int `gorm:"column:user_id;type:int(11) unsigned;comment:用户ID;NOT NULL" json:"user_id"`
} }

View File

@ -42,3 +42,13 @@ type SaveUser struct {
Phone string `json:"phone" form:"phone"` Phone string `json:"phone" form:"phone"`
Status constant.UserStatus `json:"status" form:"status" binding:"oneof=NORMAL DISABLE"` // 用户状态 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"`
}

View File

@ -11,5 +11,3 @@ type GetWineListByUser struct {
Category string `json:"category" form:"category"` // 分类 Category string `json:"category" form:"category"` // 分类
UserId int `json:"userId" form:"userId"` // 用户ID UserId int `json:"userId" form:"userId"` // 用户ID
} }
type AddWine struct{}

View File

@ -3,6 +3,7 @@ package repository
import ( import (
"Lee-WineList/client" "Lee-WineList/client"
"Lee-WineList/model/entity" "Lee-WineList/model/entity"
"Lee-WineList/model/param"
"errors" "errors"
"git.echol.cn/loser/logger/log" "git.echol.cn/loser/logger/log"
"gorm.io/gorm" "gorm.io/gorm"
@ -54,3 +55,20 @@ func (user) CheckUnionIdIsExist(unionId, openId string) bool {
func (user) UpdateUserInfo(e *entity.User) (err error) { func (user) UpdateUserInfo(e *entity.User) (err error) {
return client.MySQL.Updates(&e).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
}

View File

@ -34,7 +34,17 @@ func (w *wine) GetWineList(p param.GetWineList) (wines []entity.Wine, err error)
return 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 return
} }

View File

@ -1,13 +1,12 @@
package router package router
import ( import (
"Lee-WineList/middleware"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
// InitRoute 初始化路由 // InitRoute 初始化路由
func InitRoute(g *gin.RouterGroup) { func InitRoute(g *gin.RouterGroup) {
login(g) // 登录相关路由 login(g) // 登录相关路由
user(g.Group("/user", middleware.AuthorizeToken())) // 用户相关路由 user(g.Group("/user")) // 用户相关路由
wine(g.Group("/wine")) wine(g.Group("/wine"))
} }

View File

@ -10,4 +10,5 @@ func user(g *gin.RouterGroup) {
g.GET("", app.UserApi().GetUser) // 获取当前登录用户信息 g.GET("", app.UserApi().GetUser) // 获取当前登录用户信息
g.POST("/binding/wechat", app.UserApi().BindingWeChat) // 绑定微信 g.POST("/binding/wechat", app.UserApi().BindingWeChat) // 绑定微信
g.POST("/update", app.UserApi().UpdateUser) // 修改用户信息 g.POST("/update", app.UserApi().UpdateUser) // 修改用户信息
g.GET("/like", app.UserApi().GetLikeWineList) // 获取用户收藏的酒品
} }

View File

@ -7,4 +7,7 @@ import (
func wine(g *gin.RouterGroup) { func wine(g *gin.RouterGroup) {
g.GET("", app.WineApi().GetList) g.GET("", app.WineApi().GetList)
g.POST("", app.WineApi().Add)
g.PUT("", app.WineApi().Update)
g.DELETE("", app.WineApi().Delete)
} }