app.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package controller
  2. import (
  3. "git.cestong.com.cn/cecf/cecf-golib/pkg/apollo"
  4. "git.cestong.com.cn/cecf/cecf-golib/pkg/errors"
  5. gin2 "git.cestong.com.cn/cecf/cecf-golib/pkg/gin"
  6. "git.cestong.com.cn/cecf/config-center-server/pkg/model"
  7. "github.com/gin-gonic/gin"
  8. "github.com/go-playground/validator/v10"
  9. "gorm.io/gorm"
  10. )
  11. type AppController struct {
  12. db *gorm.DB
  13. val *validator.Validate
  14. apolloCli *apollo.HTTPClient
  15. }
  16. func NewAppController(db *gorm.DB, val *validator.Validate, apolloCli *apollo.HTTPClient) *AppController {
  17. return &AppController{db: db, val: val, apolloCli: apolloCli}
  18. }
  19. type CreateAppReqVO struct {
  20. Name string `json:"name" validate:"min=1"`
  21. ImgURL string `json:"imgURL" validate:"max=500"`
  22. Desc string `json:"desc" validate:"max=250"`
  23. }
  24. func (ac *AppController) Create(c *gin.Context) (interface{}, error) {
  25. var req CreateAppReqVO
  26. if errReq := c.BindJSON(&req); errReq != nil {
  27. return nil, errors.ErrBadRequest
  28. }
  29. if errVal := ac.val.Struct(&req); errVal != nil {
  30. return nil, errors.ErrBadRequest
  31. }
  32. {
  33. var nameCount int64
  34. if errCnt := ac.db.Model(&model.AppModel{}).Where("name=?", req.Name).Count(&nameCount).Error; errCnt != nil {
  35. return nil, errCnt
  36. }
  37. if nameCount > 0 {
  38. return nil, errors.Wrap(errors.ErrBadRequest, "包含相同名称(%s)的应用", req.Name)
  39. }
  40. }
  41. apolloID := req.Name
  42. if errCreateApollo := ac.apolloCli.CreateApp(apolloID); errCreateApollo != nil {
  43. return nil, errors.Wrap(errCreateApollo, "create apollo app")
  44. }
  45. var m model.AppModel
  46. {
  47. m.Name = req.Name
  48. m.Desc = req.Desc
  49. m.ImgURL = req.ImgURL
  50. m.ApolloAppID = apolloID
  51. }
  52. if errCreate := ac.db.Create(&m).Error; errCreate != nil {
  53. return nil, errors.Wrap(errCreate, "db:%+v", m)
  54. }
  55. return gin2.SuccessGeneralVO(), nil
  56. }
  57. type ListAppRespVO struct {
  58. List []ListAppItemVO `json:"list"`
  59. }
  60. type ListAppItemVO struct {
  61. ID uint `json:"id"`
  62. Name string `json:"name" validate:"min=1"`
  63. ImgURL string `json:"imgURL" validate:"max=500"`
  64. Desc string `json:"desc" validate:"max=250"`
  65. }
  66. func (ac *AppController) List(c *gin.Context) (interface{}, error) {
  67. var resp ListAppRespVO
  68. var appList []model.AppModel
  69. if errFind := ac.db.Order("id desc").Find(&appList).Error; errFind != nil {
  70. return nil, errors.Wrap(errFind, "db find")
  71. }
  72. resp.List = make([]ListAppItemVO, 0, len(appList))
  73. for _, appModel := range appList {
  74. var item ListAppItemVO
  75. item.ID = appModel.ID
  76. item.Name = appModel.Name
  77. item.ImgURL = appModel.ImgURL
  78. item.Desc = appModel.Desc
  79. resp.List = append(resp.List, item)
  80. }
  81. return resp, nil
  82. }