123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- package controller
- import (
- "git.cestong.com.cn/cecf/cecf-golib/pkg/apollo"
- "git.cestong.com.cn/cecf/cecf-golib/pkg/errors"
- gin2 "git.cestong.com.cn/cecf/cecf-golib/pkg/gin"
- "git.cestong.com.cn/cecf/config-center-server/pkg/model"
- "github.com/gin-gonic/gin"
- "github.com/go-playground/validator/v10"
- "gorm.io/gorm"
- )
- type AppController struct {
- db *gorm.DB
- val *validator.Validate
- apolloCli *apollo.HTTPClient
- }
- func NewAppController(db *gorm.DB, val *validator.Validate, apolloCli *apollo.HTTPClient) *AppController {
- return &AppController{db: db, val: val, apolloCli: apolloCli}
- }
- type CreateAppReqVO struct {
- Name string `json:"name" validate:"min=1"`
- ImgURL string `json:"imgURL" validate:"max=500"`
- Desc string `json:"desc" validate:"max=250"`
- }
- func (ac *AppController) Create(c *gin.Context) (interface{}, error) {
- var req CreateAppReqVO
- if errReq := c.BindJSON(&req); errReq != nil {
- return nil, errors.ErrBadRequest
- }
- if errVal := ac.val.Struct(&req); errVal != nil {
- return nil, errors.ErrBadRequest
- }
- {
- var nameCount int64
- if errCnt := ac.db.Model(&model.AppModel{}).Where("name=?", req.Name).Count(&nameCount).Error; errCnt != nil {
- return nil, errCnt
- }
- if nameCount > 0 {
- return nil, errors.Wrap(errors.ErrBadRequest, "包含相同名称(%s)的应用", req.Name)
- }
- }
- apolloID := req.Name
- if errCreateApollo := ac.apolloCli.CreateApp(apolloID); errCreateApollo != nil {
- return nil, errors.Wrap(errCreateApollo, "create apollo app")
- }
- var m model.AppModel
- {
- m.Name = req.Name
- m.Desc = req.Desc
- m.ImgURL = req.ImgURL
- m.ApolloAppID = apolloID
- }
- if errCreate := ac.db.Create(&m).Error; errCreate != nil {
- return nil, errors.Wrap(errCreate, "db:%+v", m)
- }
- return gin2.SuccessGeneralVO(), nil
- }
- type ListAppRespVO struct {
- List []ListAppItemVO `json:"list"`
- }
- type ListAppItemVO struct {
- ID uint `json:"id"`
- Name string `json:"name" validate:"min=1"`
- ImgURL string `json:"imgURL" validate:"max=500"`
- Desc string `json:"desc" validate:"max=250"`
- }
- func (ac *AppController) List(c *gin.Context) (interface{}, error) {
- var resp ListAppRespVO
- var appList []model.AppModel
- if errFind := ac.db.Order("id desc").Find(&appList).Error; errFind != nil {
- return nil, errors.Wrap(errFind, "db find")
- }
- resp.List = make([]ListAppItemVO, 0, len(appList))
- for _, appModel := range appList {
- var item ListAppItemVO
- item.ID = appModel.ID
- item.Name = appModel.Name
- item.ImgURL = appModel.ImgURL
- item.Desc = appModel.Desc
- resp.List = append(resp.List, item)
- }
- return resp, nil
- }
|