|
- package hdl
-
- import (
- "applet/app/admin/lib/validate"
- "applet/app/admin/md"
- "applet/app/db"
- "applet/app/db/model"
- "applet/app/e"
- "applet/app/utils"
- "github.com/gin-gonic/gin"
- "time"
- )
-
- func BannerList(c *gin.Context) {
- bannerDb := db.BannerDb{}
- bannerDb.Set()
- banners, err := bannerDb.FindBanner(0, 0)
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- e.OutSuc(c, map[string]interface{}{
- "list": banners,
- }, nil)
- return
- }
-
- func BannerSort(c *gin.Context) {
- var args struct {
- Ids []string `json:"ids" binding:"required"`
- }
- if err := c.ShouldBindJSON(&args); err != nil {
- e.OutErr(c, e.ERR_INVALID_ARGS, err)
- return
- }
- bannerDb := db.BannerDb{}
- bannerDb.Set()
- banners, err := bannerDb.FindBannerById(args.Ids)
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- for k, v := range *banners {
- v.Sort = k
- _, err1 := bannerDb.BannerUpdate(&v, "sort")
- if err1 != nil {
- e.OutErr(c, e.ERR_DB_ORM, err1.Error())
- return
- }
- }
- e.OutSuc(c, "success", nil)
- }
-
- func BannerAdd(c *gin.Context) {
- var req md.BannerAddReq
- err := c.ShouldBindJSON(&req)
- if err != nil {
- err = validate.HandleValidateErr(err)
- err1 := err.(e.E)
- e.OutErr(c, err1.Code, err1.Error())
- return
- }
- bannerDb := db.BannerDb{}
- bannerDb.Set()
- now := time.Now()
- banner := model.Banner{
- Name: req.Name,
- ImgUrl: req.ImgUrl,
- CreateAt: now,
- UpdateAt: now,
- }
- _, err = bannerDb.BannerInsert(&banner)
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- e.OutSuc(c, "success", nil)
- return
- }
-
- func BannerUpdate(c *gin.Context) {
- var req md.BannerUpdateReq
- err := c.ShouldBindJSON(&req)
- if err != nil {
- err = validate.HandleValidateErr(err)
- err1 := err.(e.E)
- e.OutErr(c, err1.Code, err1.Error())
- return
- }
- bannerDb := db.BannerDb{}
- bannerDb.Set()
- banner, err := bannerDb.GetBanner(req.Id)
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- if banner == nil {
- e.OutErr(c, e.ERR_NO_DATA, "未查询到对应记录")
- return
- }
- now := time.Now()
- banner.Name = req.Name
- banner.ImgUrl = req.ImgUrl
- banner.UpdateAt = now
- _, err = bannerDb.BannerUpdate(banner, "name", "content", "update_at")
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- e.OutSuc(c, "success", nil)
- return
- }
-
- func BannerDelete(c *gin.Context) {
- id := c.Param("id")
- bannerDb := db.BannerDb{}
- bannerDb.Set()
- company, err := bannerDb.GetBanner(utils.StrToInt(id))
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- if company == nil {
- e.OutErr(c, e.ERR_NO_DATA, "未查询到对应记录")
- return
- }
-
- _, err = bannerDb.BannerDelete(id)
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- e.OutSuc(c, "success", nil)
- return
- }
|