gitee.com/woood2/luca@v1.0.4/cmd/backend/internal/controller/app.go (about)

     1  package controller
     2  
     3  import (
     4  	_ "gitee.com/woood2/luca/internal/errcode"
     5  	"gitee.com/woood2/luca/internal/layer"
     6  	"gitee.com/woood2/luca/internal/service"
     7  	"gitee.com/woood2/luca/internal/trace"
     8  	"gitee.com/woood2/luca/internal/util"
     9  	"github.com/gin-gonic/gin"
    10  	"go.uber.org/zap"
    11  	"time"
    12  )
    13  
    14  type CreateAppParam struct {
    15  	Appkey   string `json:"appkey"  binding:"required,min=2,max=32" example:"weibo"`
    16  	Secret   string `json:"secret" binding:"required,min=2,max=64" example:"wow24^_^"`
    17  	Name     string `json:"name" binding:"required,min=2,max=32" example:"新浪微博"`
    18  	Url      string `json:"url" binding:"omitempty,url" example:"https://weibo.com"`
    19  	Logo     string `json:"logo" binding:"omitempty,url" example:"https://img.t.sinajs.cn/t6/style/images/apple/wbfont.svg"`
    20  	Contacts string `json:"contacts" binding:"max=256" example:"@来去之间"`
    21  	Remark   string `json:"remark" binding:"max=256" example:"备注"`
    22  }
    23  
    24  type EditAppParam struct {
    25  	Name     string `json:"name" binding:"required,min=2,max=32" example:"新浪微博"`
    26  	Secret   string `json:"secret" binding:"required,min=2,max=64" example:"wow24^_^"`
    27  	Url      string `json:"url" binding:"omitempty,url" example:"https://weibo.com"`
    28  	Logo     string `json:"logo" binding:"omitempty,url" example:"https://img.t.sinajs.cn/t6/style/images/apple/wbfont.svg"`
    29  	Contacts string `json:"contacts" binding:"max=256" example:"@来去之间"`
    30  	Remark   string `form:"remark" json:"remark" binding:"max=256" example:"备注"`
    31  }
    32  
    33  type ChangeAppDisabledParam struct {
    34  	Disabled bool `json:"disabled" example:"true"`
    35  }
    36  
    37  type App struct {
    38  	ID         int           `json:"id" example:"24"`
    39  	Appkey     string        `json:"appkey" example:"weibo"`
    40  	Secret     string        `json:"secret" example:"wow24^_^"`
    41  	Name       string        `json:"name" example:"新浪微博"`
    42  	Url        string        `json:"url" example:"https://weibo.com"`
    43  	Logo       string        `json:"logo" example:"https://img.t.sinajs.cn/t6/style/images/apple/wbfont.svg"`
    44  	Contacts   string        `json:"contacts" example:"@来去之间"`
    45  	Disabled   int           `json:"disabled" example:"0"`
    46  	Createtime util.JsonTime `json:"createtime" example:"2006-01-02 15:04:05"`
    47  	Updatetime util.JsonTime `json:"updatetime" example:"2006-01-02 15:04:05"`
    48  	Remark     string        `json:"remark" example:"备注"`
    49  }
    50  
    51  func NewAppController(logger *zap.Logger, env string, svc service.AppService) *AppController {
    52  	return &AppController{
    53  		Controller: layer.NewController(env, logger),
    54  		svc:        svc,
    55  	}
    56  }
    57  
    58  type AppController struct {
    59  	layer.Controller
    60  	svc service.AppService
    61  }
    62  
    63  // @Summary 应用创建
    64  // @Description 创建一个新的应用
    65  // @ID create-app
    66  // @Tags 应用
    67  // @Accept  json
    68  // @Produce  json
    69  // @Security ApiKeyAuth
    70  // @Param CreateAppParam body CreateAppParam true "应用参数"
    71  // @Success 200 {object} errcode.Resp{data=App}
    72  // @Failure 400 {string} string
    73  // @Failure 401 {string} string
    74  // @Failure 403 {string} string
    75  // @Failure 500 {object} errcode.Resp
    76  // @Router /apps [post]
    77  func (ctr *AppController) Create(c *gin.Context) {
    78  	var p CreateAppParam
    79  	if err := c.ShouldBindJSON(&p); err != nil {
    80  		ctr.Bad(c, err)
    81  		return
    82  	}
    83  	dto := &service.App{
    84  		Appkey:   p.Appkey,
    85  		Secret:   p.Secret,
    86  		Name:     p.Name,
    87  		Url:      p.Url,
    88  		Logo:     p.Logo,
    89  		Contacts: p.Contacts,
    90  		Remark:   p.Remark,
    91  	}
    92  	id, err := ctr.svc.Create(trace.Ctx(c), dto)
    93  	if err != nil {
    94  		ctr.Error(c, err)
    95  		return
    96  	}
    97  	app := &App{
    98  		ID:         id,
    99  		Appkey:     p.Appkey,
   100  		Secret:     p.Secret,
   101  		Name:       p.Name,
   102  		Url:        p.Url,
   103  		Logo:       p.Logo,
   104  		Contacts:   p.Contacts,
   105  		Disabled:   0,
   106  		Createtime: util.JsonTime(time.Now()),
   107  		Updatetime: util.JsonTime(time.Now()),
   108  		Remark:     p.Remark,
   109  	}
   110  	ctr.OK(c, app)
   111  }
   112  
   113  // @Summary 应用删除
   114  // @Description 删除一个应用
   115  // @ID delete-app
   116  // @Tags 应用
   117  // @Accept  json
   118  // @Produce  json
   119  // @Security ApiKeyAuth
   120  // @Param appkey path string true "应用标识"
   121  // @Success 200 {object} errcode.Resp{data=boolean}
   122  // @Failure 401 {string} string
   123  // @Failure 403 {string} string
   124  // @Failure 500 {object} errcode.Resp
   125  // @Router /apps/{appkey} [delete]
   126  func (ctr *AppController) Delete(c *gin.Context) {
   127  	appkey := c.Param("appkey")
   128  	if err := ctr.svc.DeleteByKey(trace.Ctx(c), appkey); err != nil {
   129  		ctr.Error(c, err)
   130  		return
   131  	}
   132  	ctr.OK(c, true)
   133  }
   134  
   135  // @Summary 应用启用/禁用
   136  // @Description 转变一个应用的状态
   137  // @ID change-app-disabled
   138  // @Tags 应用
   139  // @Accept  json
   140  // @Produce  json
   141  // @Security ApiKeyAuth
   142  // @Param appkey path string true "应用标识"
   143  // @Param ChangeAppDisabledParam body ChangeAppDisabledParam true "参数"
   144  // @Success 200 {object} errcode.Resp{data=boolean}
   145  // @Failure 400 {string} string
   146  // @Failure 401 {string} string
   147  // @Failure 403 {string} string
   148  // @Failure 500 {object} errcode.Resp
   149  // @Router /apps/{appkey}/disabled [put]
   150  func (ctr *AppController) ChangeDisabled(c *gin.Context) {
   151  	appkey := c.Param("appkey")
   152  	var p ChangeAppDisabledParam
   153  	if err := c.ShouldBindJSON(&p); err != nil {
   154  		ctr.Bad(c, err)
   155  		return
   156  	}
   157  	if p.Disabled {
   158  		if err := ctr.svc.Disable(trace.Ctx(c), appkey); err != nil {
   159  			ctr.Error(c, err)
   160  			return
   161  		}
   162  	} else {
   163  		if err := ctr.svc.Enable(trace.Ctx(c), appkey); err != nil {
   164  			ctr.Error(c, err)
   165  			return
   166  		}
   167  	}
   168  	ctr.OK(c, true)
   169  }
   170  
   171  // @Summary 应用修改
   172  // @Description 修改应用的属性
   173  // @ID edit-app
   174  // @Tags 应用
   175  // @Accept  json
   176  // @Produce  json
   177  // @Security ApiKeyAuth
   178  // @Param appkey path string true "应用标识"
   179  // @Param EditAppParam body EditAppParam true "应用参数"
   180  // @Success 200 {object} errcode.Resp{data=boolean}
   181  // @Failure 400 {string} string
   182  // @Failure 401 {string} string
   183  // @Failure 403 {string} string
   184  // @Failure 500 {object} errcode.Resp
   185  // @Router /apps/{appkey} [put]
   186  func (ctr *AppController) Edit(c *gin.Context) {
   187  	appkey := c.Param("appkey")
   188  	var p EditAppParam
   189  	if err := c.ShouldBindJSON(&p); err != nil {
   190  		ctr.Bad(c, err)
   191  		return
   192  	}
   193  	dto := &service.App{
   194  		Appkey:   appkey,
   195  		Secret:   p.Secret,
   196  		Name:     p.Name,
   197  		Url:      p.Url,
   198  		Logo:     p.Logo,
   199  		Contacts: p.Contacts,
   200  		Remark:   p.Remark,
   201  	}
   202  	if err := ctr.svc.EditByKey(trace.Ctx(c), appkey, dto); err != nil {
   203  		ctr.Error(c, err)
   204  		return
   205  	}
   206  	ctr.OK(c, true)
   207  }
   208  
   209  // @Summary 应用查询
   210  // @Description 查询单个应用详情
   211  // @ID get-app
   212  // @Tags 应用
   213  // @Accept  json
   214  // @Produce  json
   215  // @Security ApiKeyAuth
   216  // @Param appkey path string true "应用标识"
   217  // @Success 200 {object} errcode.Resp{data=App}
   218  // @Failure 401 {string} string
   219  // @Failure 403 {string} string
   220  // @Failure 500 {object} errcode.Resp
   221  // @Router /apps/{appkey} [get]
   222  func (ctr *AppController) Get(c *gin.Context) {
   223  	appkey := c.Param("appkey")
   224  	dto, err := ctr.svc.GetByKey(trace.Ctx(c), appkey)
   225  	if err != nil {
   226  		ctr.Error(c, err)
   227  		return
   228  	}
   229  	app := &App{
   230  		ID:         dto.ID,
   231  		Appkey:     dto.Appkey,
   232  		Secret:     dto.Secret,
   233  		Name:       dto.Name,
   234  		Url:        dto.Url,
   235  		Logo:       dto.Logo,
   236  		Contacts:   dto.Contacts,
   237  		Disabled:   dto.Disabled,
   238  		Createtime: util.JsonTime(dto.Createtime),
   239  		Updatetime: util.JsonTime(dto.Updatetime),
   240  		Remark:     dto.Remark,
   241  	}
   242  	ctr.OK(c, app)
   243  }
   244  
   245  // @Summary 应用列表
   246  // @Description 批量获取应用列表,支持模糊查询
   247  // @ID list-app
   248  // @Tags 应用
   249  // @Accept  json
   250  // @Produce  json
   251  // @Security ApiKeyAuth
   252  // @Param appkey query string false "应用标识"
   253  // @Param name query string false "应用名称"
   254  // @Success 200 {object} errcode.Resp{data=[]App}
   255  // @Failure 400 {string} string
   256  // @Failure 401 {string} string
   257  // @Failure 403 {string} string
   258  // @Failure 500 {object} errcode.Resp
   259  // @Router /apps [get]
   260  func (ctr *AppController) List(c *gin.Context) {
   261  	//ctr.Error(c, errors.New("$%^#"))
   262  	//return
   263  	appkey := c.DefaultQuery("appkey", "")
   264  	name := c.DefaultQuery("name", "")
   265  	dtos, err := ctr.svc.List(trace.Ctx(c), appkey, name)
   266  	if err != nil {
   267  		ctr.Error(c, err)
   268  		return
   269  	}
   270  	apps := make([]*App, 0)
   271  	for _, dto := range dtos {
   272  		apps = append(apps, &App{
   273  			ID:         dto.ID,
   274  			Appkey:     dto.Appkey,
   275  			Secret:     dto.Secret,
   276  			Name:       dto.Name,
   277  			Url:        dto.Url,
   278  			Logo:       dto.Logo,
   279  			Contacts:   dto.Contacts,
   280  			Disabled:   dto.Disabled,
   281  			Createtime: util.JsonTime(dto.Createtime),
   282  			Updatetime: util.JsonTime(dto.Updatetime),
   283  			Remark:     dto.Remark,
   284  		})
   285  	}
   286  	ctr.OK(c, apps)
   287  }