🎨 新增后台海报管理相关接口

This commit is contained in:
loser 2023-12-11 18:55:52 +08:00
parent 8d71e58734
commit c804986a68
4 changed files with 190 additions and 0 deletions

114
api/v1/system/poster.go Normal file
View File

@ -0,0 +1,114 @@
package system
import (
"github.com/gin-gonic/gin"
"miniapp/model/common"
"miniapp/model/common/request"
r "miniapp/model/common/response"
)
type PosterApi struct {
}
// GetPosterList 获取海报列表
func (p PosterApi) GetPosterList(ctx *gin.Context) {
var page request.PageInfo
if err := ctx.ShouldBind(&p); err != nil {
r.FailWithMessage("参数错误"+err.Error(), ctx)
return
}
list, total, err := posterService.GetPosterList(page)
if err != nil {
r.FailWithMessage("获取海报列表失败", ctx)
return
}
r.OkWithDetailed(r.PageResult{List: list, Total: total, Page: page.Page, PageSize: page.PageSize}, "获取成功", ctx)
}
// CreatePoster 创建海报
func (p PosterApi) CreatePoster(ctx *gin.Context) {
var poster common.Poster
if err := ctx.ShouldBindJSON(&poster); err != nil {
r.FailWithMessage("参数错误"+err.Error(), ctx)
return
}
err := posterService.AddPoster(&poster)
if err != nil {
r.FailWithMessage("创建失败"+err.Error(), ctx)
return
}
r.OkWithMessage("创建成功", ctx)
}
// UpdatePoster 更新海报
func (p PosterApi) UpdatePoster(ctx *gin.Context) {
var poster common.Poster
if err := ctx.ShouldBindJSON(&poster); err != nil {
r.FailWithMessage("参数错误"+err.Error(), ctx)
return
}
err := posterService.UpdatePoster(&poster)
if err != nil {
r.FailWithMessage("更新失败"+err.Error(), ctx)
return
}
r.OkWithMessage("更新成功", ctx)
}
// DeletePoster 删除海报
func (p PosterApi) DeletePoster(ctx *gin.Context) {
var poster common.Poster
if err := ctx.ShouldBind(&poster); err != nil {
r.FailWithMessage("参数错误"+err.Error(), ctx)
return
}
err := posterService.DeletePoster(&poster)
if err != nil {
r.FailWithMessage("删除失败"+err.Error(), ctx)
return
}
r.OkWithMessage("删除成功", ctx)
}
// GetPosterById 根据id获取海报
func (p PosterApi) GetPosterById(ctx *gin.Context) {
id := ctx.Param("id")
if id == "" {
r.FailWithMessage("参数错误", ctx)
return
}
poster, err := posterService.GetPosterById(id)
if err != nil {
r.FailWithMessage("获取失败"+err.Error(), ctx)
return
}
r.OkWithDetailed(poster, "获取成功", ctx)
}
// UpdatePosterSort 更新海报排序
func (p PosterApi) UpdatePosterSort(ctx *gin.Context) {
var poster common.Poster
if err := ctx.ShouldBind(&poster); err != nil {
r.FailWithMessage("参数错误"+err.Error(), ctx)
return
}
err := posterService.UpdatePoster(&poster)
if err != nil {
r.FailWithMessage("更新失败"+err.Error(), ctx)
return
}
r.OkWithMessage("更新成功", ctx)
}

9
model/common/poster.go Normal file
View File

@ -0,0 +1,9 @@
package common
import "miniapp/global"
type Poster struct {
global.GVA_MODEL
Url string `json:"url" gorm:"comment:海报地址"`
Remark string `json:"remark" gorm:"comment:备注"`
}

28
router/system/poster.go Normal file
View File

@ -0,0 +1,28 @@
package system
import (
"github.com/gin-gonic/gin"
v1 "miniapp/api/v1"
"miniapp/middleware"
)
type PosterRouter struct {
}
// InitPosterRouter 注册医院路由
func (s *RouterGroup) InitPosterRouter(Router *gin.RouterGroup) {
hospitalRouter := Router.Group("poster").Use(middleware.OperationRecord()).Use(middleware.JWTAuth())
publicRouter := Router.Group("poster")
posterApi := v1.ApiGroupApp.SystemApiGroup.PosterApi
{
hospitalRouter.POST("", posterApi.CreatePoster) // 创建医院
hospitalRouter.DELETE("", posterApi.DeletePoster) // 删除医院
hospitalRouter.PUT("", posterApi.UpdatePosterSort) // 更新医院
}
{
publicRouter.GET("list", posterApi.GetPosterList) // 获取医院列表
publicRouter.GET(":id", posterApi.GetPosterById) // 获取单条医院消息
}
}

39
service/system/poster.go Normal file
View File

@ -0,0 +1,39 @@
package system
import (
"miniapp/global"
"miniapp/model/common"
"miniapp/model/common/request"
)
type PosterService struct{}
// AddPoster 新增
func (p *PosterService) AddPoster(poster *common.Poster) (err error) {
err = global.GVA_DB.Create(&poster).Error
return
}
// UpdatePoster 更新
func (p *PosterService) UpdatePoster(poster *common.Poster) (err error) {
err = global.GVA_DB.Save(&poster).Error
return
}
// DeletePoster 删除
func (p *PosterService) DeletePoster(poster *common.Poster) (err error) {
err = global.GVA_DB.Delete(&poster).Error
return
}
// GetPosterList 获取列表
func (p *PosterService) GetPosterList(page request.PageInfo) (list []common.Poster, total int64, err error) {
err = global.GVA_DB.Scopes(Page(page.Page, page.PageSize)).Find(&list).Limit(-1).Offset(-1).Count(&total).Error
return
}
// GetPosterById 根据id获取
func (p *PosterService) GetPosterById(id string) (poster common.Poster, err error) {
err = global.GVA_DB.Where("id = ?", id).First(&poster).Error
return
}