154 lines
4.9 KiB
Go
154 lines
4.9 KiB
Go
package v1
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
"leafdev.top/Ecosystem/recommender/internal/handler/http/response"
|
|
"leafdev.top/Ecosystem/recommender/internal/schema"
|
|
"leafdev.top/Ecosystem/recommender/internal/service/application"
|
|
"leafdev.top/Ecosystem/recommender/internal/service/auth"
|
|
"net/http"
|
|
)
|
|
|
|
type ApplicationController struct {
|
|
authService *auth.Service
|
|
applicationService *application.Service
|
|
}
|
|
|
|
func NewApplicationController(authService *auth.Service, applicationService *application.Service) *ApplicationController {
|
|
return &ApplicationController{
|
|
authService,
|
|
applicationService,
|
|
}
|
|
}
|
|
|
|
// List godoc
|
|
// @Summary List Applications
|
|
// @Description 列出当前用户下的应用程序列表
|
|
// @Tags applications
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security ApiKeyAuth
|
|
// @Success 200 {object} schema.ResponseBody{data=[]entity.Application}
|
|
// @Failure 400 {object} schema.ResponseBody
|
|
// @Router /api/v1/applications [get]
|
|
func (u *ApplicationController) List(c *gin.Context) {
|
|
user := u.authService.GetUser(c)
|
|
|
|
applications, err := u.applicationService.ListApplications(c, &user.ID)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
response.Ctx(c).Status(http.StatusOK).Data(applications).Send()
|
|
}
|
|
|
|
// Save godoc
|
|
// @Summary 创建并保存一个应用程序
|
|
// @Description 创建一个应用程序
|
|
// @Tags applications
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security ApiKeyAuth
|
|
// @Param ApplicationCreateRequest body schema.ApplicationCreateRequest true "创建应用程序的请求"
|
|
// @Success 200 {object} schema.ResponseBody{data=[]entity.Application}
|
|
// @Failure 400 {object} schema.ResponseBody
|
|
// @Router /api/v1/applications [post]
|
|
func (u *ApplicationController) Save(c *gin.Context) {
|
|
user := u.authService.GetUser(c)
|
|
|
|
var applicationCreateRequest = &schema.ApplicationCreateRequest{}
|
|
if err := c.ShouldBindJSON(applicationCreateRequest); err != nil {
|
|
response.Ctx(c).Error(err).Status(http.StatusBadRequest).Send()
|
|
return
|
|
}
|
|
|
|
applications, err := u.applicationService.CreateApplication(c, applicationCreateRequest.Name, user.ID)
|
|
if err != nil {
|
|
response.Ctx(c).Error(err).Send()
|
|
return
|
|
}
|
|
|
|
response.Ctx(c).Status(http.StatusOK).Data(applications).Send()
|
|
}
|
|
|
|
// ListToken godoc
|
|
// @Summary 获取应用程序的 Token 列表
|
|
// @Description 获取应用程序的 Token 列表
|
|
// @Tags applications
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security ApiKeyAuth
|
|
// @Param application_id path schema.ApplicationId true "应用程序 ID"
|
|
// @Success 200 {object} schema.ResponseBody{data=[]entity.ApplicationToken}
|
|
// @Failure 400 {object} schema.ResponseBody
|
|
// @Router /api/v1/application/{application_id}/tokens [get]
|
|
func (u *ApplicationController) ListToken(c *gin.Context) {
|
|
user := u.authService.GetUser(c)
|
|
|
|
var applicationRequest = &schema.ApplicationId{}
|
|
if err := c.ShouldBindUri(applicationRequest); err != nil {
|
|
response.Ctx(c).Error(err).Status(http.StatusBadRequest).Send()
|
|
return
|
|
}
|
|
|
|
applicationEntity, err := u.applicationService.GetApplicationById(c, applicationRequest.ApplicationId)
|
|
if err != nil {
|
|
response.Ctx(c).Error(err).Send()
|
|
return
|
|
}
|
|
|
|
if applicationEntity.UserId != user.ID {
|
|
response.Ctx(c).Status(http.StatusForbidden).Send()
|
|
return
|
|
}
|
|
|
|
applicationTokens, err := u.applicationService.ListToken(c, applicationEntity)
|
|
if err != nil {
|
|
response.Ctx(c).Error(err).Send()
|
|
return
|
|
}
|
|
|
|
response.Ctx(c).Status(http.StatusOK).Data(applicationTokens).Send()
|
|
}
|
|
|
|
// SaveToken godoc
|
|
// @Summary 获取应用程序的 Token 列表
|
|
// @Description 获取应用程序的 Token 列表
|
|
// @Tags applications
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security ApiKeyAuth
|
|
// @Param application_id path schema.ApplicationId true "应用程序 ID"
|
|
// @Param ApplicationCreateRequest body schema.ApplicationCreateRequest true "创建应用程序的请求"
|
|
// @Success 200 {object} schema.ResponseBody{data=entity.ApplicationToken}
|
|
// @Failure 400 {object} schema.ResponseBody
|
|
// @Router /api/v1/application/{application_id}/tokens [post]
|
|
func (u *ApplicationController) SaveToken(c *gin.Context) {
|
|
user := u.authService.GetUser(c)
|
|
|
|
var applicationRequest = &schema.ApplicationId{}
|
|
if err := c.ShouldBindUri(applicationRequest); err != nil {
|
|
response.Ctx(c).Error(err).Status(http.StatusBadRequest).Send()
|
|
return
|
|
}
|
|
|
|
applicationEntity, err := u.applicationService.GetApplicationById(c, applicationRequest.ApplicationId)
|
|
if err != nil {
|
|
response.Ctx(c).Error(err).Send()
|
|
return
|
|
}
|
|
|
|
if applicationEntity.UserId != user.ID {
|
|
response.Ctx(c).Status(http.StatusForbidden).Send()
|
|
return
|
|
}
|
|
|
|
applicationToken, err := u.applicationService.CreateToken(c, applicationEntity)
|
|
if err != nil {
|
|
response.Ctx(c).Error(err).Send()
|
|
return
|
|
}
|
|
|
|
response.Ctx(c).Status(http.StatusOK).Data(applicationToken).Send()
|
|
}
|