From c804986a68c1efddc6ced5dbd8d45cbb588fd3f5 Mon Sep 17 00:00:00 2001 From: Echo <1711788888@qq.com> Date: Mon, 11 Dec 2023 18:55:52 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20=E6=96=B0=E5=A2=9E=E5=90=8E=E5=8F=B0?= =?UTF-8?q?=E6=B5=B7=E6=8A=A5=E7=AE=A1=E7=90=86=E7=9B=B8=E5=85=B3=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/v1/system/poster.go | 114 +++++++++++++++++++++++++++++++++++++++ model/common/poster.go | 9 ++++ router/system/poster.go | 28 ++++++++++ service/system/poster.go | 39 ++++++++++++++ 4 files changed, 190 insertions(+) create mode 100644 api/v1/system/poster.go create mode 100644 model/common/poster.go create mode 100644 router/system/poster.go create mode 100644 service/system/poster.go diff --git a/api/v1/system/poster.go b/api/v1/system/poster.go new file mode 100644 index 0000000..d15bd36 --- /dev/null +++ b/api/v1/system/poster.go @@ -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) +} diff --git a/model/common/poster.go b/model/common/poster.go new file mode 100644 index 0000000..8f6a935 --- /dev/null +++ b/model/common/poster.go @@ -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:备注"` +} diff --git a/router/system/poster.go b/router/system/poster.go new file mode 100644 index 0000000..a02bf1f --- /dev/null +++ b/router/system/poster.go @@ -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) // 获取单条医院消息 + } +} diff --git a/service/system/poster.go b/service/system/poster.go new file mode 100644 index 0000000..671cb0e --- /dev/null +++ b/service/system/poster.go @@ -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 +}