Skip to content

Commit

Permalink
feat: Ability to request single models.File by id from api (xbapps#1740)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yoooi0 authored Jun 4, 2024
1 parent b3aea59 commit 6a39b31
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions pkg/api/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,31 @@ func (i FilesResource) WebService() *restful.WebService {
ws.Route(ws.POST("/unmatch").To(i.unmatchFile).
Metadata(restfulspec.KeyOpenAPITags, tags))

ws.Route(ws.GET("/file/{file-id}").To(i.getFile).
Param(ws.PathParameter("file-id", "File ID").DataType("int")).
Metadata(restfulspec.KeyOpenAPITags, tags).
Writes(models.File{}))

ws.Route(ws.DELETE("/file/{file-id}").To(i.removeFile).
Metadata(restfulspec.KeyOpenAPITags, tags))

return ws
}

func (i FilesResource) getFile(req *restful.Request, resp *restful.Response) {
var file models.File

id, err := strconv.Atoi(req.PathParameter("file-id"))
if err != nil {
log.Error(err)
return
}

_ = file.GetIfExistByPK(uint(id))

resp.WriteHeaderAndEntity(http.StatusOK, file)
}

func (i FilesResource) listFiles(req *restful.Request, resp *restful.Response) {
db, _ := models.GetDB()
defer db.Close()
Expand Down

0 comments on commit 6a39b31

Please sign in to comment.