Skip to content

Commit

Permalink
wip: challenge attachment service has back
Browse files Browse the repository at this point in the history
  • Loading branch information
ElaBosak233 committed Apr 19, 2024
1 parent 3dce13c commit 01767a4
Show file tree
Hide file tree
Showing 9 changed files with 318 additions and 106 deletions.
90 changes: 65 additions & 25 deletions api/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,71 @@ const docTemplate = `{
"responses": {}
}
},
"/challenges/{id}/attachment": {
"get": {
"security": [
{
"ApiKeyAuth": []
}
],
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Challenge"
],
"summary": "查找附件",
"responses": {}
},
"post": {
"security": [
{
"ApiKeyAuth": []
}
],
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Challenge"
],
"summary": "保存附件",
"parameters": [
{
"type": "file",
"description": "attachment",
"name": "file",
"in": "formData",
"required": true
}
],
"responses": {}
},
"delete": {
"security": [
{
"ApiKeyAuth": []
}
],
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Challenge"
],
"summary": "删除附件",
"responses": {}
}
},
"/challenges/{id}/flags": {
"post": {
"security": [
Expand Down Expand Up @@ -983,31 +1048,6 @@ const docTemplate = `{
"responses": {}
}
},
"/media/games/writeups/{id}": {
"get": {
"description": "通过团队 Id 获取比赛 Writeup",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Media"
],
"summary": "通过团队 Id 获取比赛 Writeup",
"parameters": [
{
"type": "string",
"description": "团队 Id",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {}
}
},
"/pods/": {
"get": {
"security": [
Expand Down
90 changes: 65 additions & 25 deletions api/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,71 @@
"responses": {}
}
},
"/challenges/{id}/attachment": {
"get": {
"security": [
{
"ApiKeyAuth": []
}
],
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Challenge"
],
"summary": "查找附件",
"responses": {}
},
"post": {
"security": [
{
"ApiKeyAuth": []
}
],
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Challenge"
],
"summary": "保存附件",
"parameters": [
{
"type": "file",
"description": "attachment",
"name": "file",
"in": "formData",
"required": true
}
],
"responses": {}
},
"delete": {
"security": [
{
"ApiKeyAuth": []
}
],
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Challenge"
],
"summary": "删除附件",
"responses": {}
}
},
"/challenges/{id}/flags": {
"post": {
"security": [
Expand Down Expand Up @@ -974,31 +1039,6 @@
"responses": {}
}
},
"/media/games/writeups/{id}": {
"get": {
"description": "通过团队 Id 获取比赛 Writeup",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Media"
],
"summary": "通过团队 Id 获取比赛 Writeup",
"parameters": [
{
"type": "string",
"description": "团队 Id",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {}
}
},
"/pods/": {
"get": {
"security": [
Expand Down
57 changes: 40 additions & 17 deletions api/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -995,6 +995,46 @@ paths:
summary: 更新题目
tags:
- Challenge
/challenges/{id}/attachment:
delete:
consumes:
- application/json
produces:
- application/json
responses: {}
security:
- ApiKeyAuth: []
summary: 删除附件
tags:
- Challenge
get:
consumes:
- application/json
produces:
- application/json
responses: {}
security:
- ApiKeyAuth: []
summary: 查找附件
tags:
- Challenge
post:
consumes:
- application/json
parameters:
- description: attachment
in: formData
name: file
required: true
type: file
produces:
- application/json
responses: {}
security:
- ApiKeyAuth: []
summary: 保存附件
tags:
- Challenge
/challenges/{id}/flags:
post:
consumes:
Expand Down Expand Up @@ -1399,23 +1439,6 @@ paths:
summary: 允许加入比赛
tags:
- Game
/media/games/writeups/{id}:
get:
consumes:
- application/json
description: 通过团队 Id 获取比赛 Writeup
parameters:
- description: 团队 Id
in: path
name: id
required: true
type: string
produces:
- application/json
responses: {}
summary: 通过团队 Id 获取比赛 Writeup
tags:
- Media
/pods/:
get:
description: 实例查询
Expand Down
85 changes: 85 additions & 0 deletions internal/controller/challenge.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,24 @@ type IChallengeController interface {
CreateFlag(ctx *gin.Context)
UpdateFlag(ctx *gin.Context)
DeleteFlag(ctx *gin.Context)
FindAttachment(ctx *gin.Context)
SaveAttachment(ctx *gin.Context)
DeleteAttachment(ctx *gin.Context)
}

type ChallengeController struct {
challengeService service.IChallengeService
flagService service.IFlagService
hintService service.IHintService
mediaService service.IMediaService
}

func NewChallengeController(appService *service.Service) IChallengeController {
return &ChallengeController{
challengeService: appService.ChallengeService,
flagService: appService.FlagService,
hintService: appService.HintService,
mediaService: appService.MediaService,
}
}

Expand Down Expand Up @@ -338,3 +343,83 @@ func (c *ChallengeController) DeleteFlag(ctx *gin.Context) {
"code": http.StatusOK,
})
}

// FindAttachment
// @Summary 查找附件
// @Description
// @Tags Challenge
// @Accept json
// @Produce json
// @Security ApiKeyAuth
// @Router /challenges/{id}/attachment [get]
func (c *ChallengeController) FindAttachment(ctx *gin.Context) {
id := convertor.ToUintD(ctx.Param("id"), 0)
filename, size, err := c.mediaService.FindChallengeAttachment(id)
if err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{
"code": http.StatusBadRequest,
"msg": err.Error(),
})
return
}
ctx.JSON(http.StatusOK, gin.H{
"code": http.StatusOK,
"filename": filename,
"size": size,
})
}

// SaveAttachment
// @Summary 保存附件
// @Description
// @Tags Challenge
// @Accept json
// @Produce json
// @Security ApiKeyAuth
// @Param file formData file true "attachment"
// @Router /challenges/{id}/attachment [post]
func (c *ChallengeController) SaveAttachment(ctx *gin.Context) {
id := convertor.ToUintD(ctx.Param("id"), 0)
fileHeader, err := ctx.FormFile("file")
if err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{
"code": http.StatusBadRequest,
"msg": err.Error(),
})
return
}
err = c.mediaService.SaveChallengeAttachment(id, fileHeader)
if err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{
"code": http.StatusBadRequest,
"msg": err.Error(),
})
return
}
ctx.JSON(http.StatusOK, gin.H{
"code": http.StatusOK,
})
}

// DeleteAttachment
// @Summary 删除附件
// @Description
// @Tags Challenge
// @Accept json
// @Produce json
// @Security ApiKeyAuth
// @Router /challenges/{id}/attachment [delete]
func (c *ChallengeController) DeleteAttachment(ctx *gin.Context) {
id := convertor.ToUintD(ctx.Param("id"), 0)
err := c.mediaService.DeleteChallengeAttachment(id)
if err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{
"code": http.StatusBadRequest,
"msg": err.Error(),
})
return
}
ctx.JSON(http.StatusOK, gin.H{
"code": http.StatusOK,
})
}
Loading

0 comments on commit 01767a4

Please sign in to comment.