Skip to content

Commit

Permalink
feat: Add endpoint for many quests request
Browse files Browse the repository at this point in the history
  • Loading branch information
BasedDepartment1 committed Mar 24, 2024
1 parent ad6f264 commit f919fa7
Show file tree
Hide file tree
Showing 16 changed files with 628 additions and 20 deletions.
1 change: 1 addition & 0 deletions src/cmd/questspace/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ func Init(app application.App) error {
questGroup := app.Router().Group("/quest")
questHandler := quest.NewHandler(clientFactory, client)
questGroup.POST("", application.AsGinHandler(jwt.WithJWTMiddleware(jwtParser, questHandler.HandleCreate)))
questGroup.GET("", application.AsGinHandler(jwt.WithJWTMiddleware(jwtParser, questHandler.HandleGetMany)))
questGroup.GET("/:id", application.AsGinHandler(questHandler.HandleGet))
questGroup.POST("/:id", application.AsGinHandler(jwt.WithJWTMiddleware(jwtParser, questHandler.HandleUpdate)))
questGroup.DELETE("/:id", application.AsGinHandler(jwt.WithJWTMiddleware(jwtParser, questHandler.HandleDelete)))
Expand Down
81 changes: 79 additions & 2 deletions src/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,55 @@ const docTemplate = `{
}
},
"/quest": {
"get": {
"summary": "Get many quests sorted by start time and finished status",
"parameters": [
{
"maxLength": 3,
"minLength": 0,
"type": "array",
"items": {
"enum": [
"all",
"registered",
"owned"
],
"type": "string"
},
"collectionFormat": "csv",
"description": "Fields to return",
"name": "fields",
"in": "query"
},
{
"type": "integer",
"default": 50,
"description": "Number of quests to return for each field",
"name": "page_size",
"in": "query"
},
{
"type": "string",
"description": "Page ID to return. Mutually exclusive to multiple fields",
"name": "page_id",
"in": "query"
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/quests.Quests"
}
},
"400": {
"description": "Bad Request"
},
"401": {
"description": "Unauthorized"
}
}
},
"post": {
"summary": "Create new quest",
"parameters": [
Expand Down Expand Up @@ -498,15 +547,43 @@ const docTemplate = `{
}
}
},
"quests.PaginatedQuestsResponse": {
"type": "object",
"properties": {
"next_page_id": {
"type": "string"
},
"quests": {
"type": "array",
"items": {
"$ref": "#/definitions/storage.Quest"
}
}
}
},
"quests.Quests": {
"type": "object",
"properties": {
"all": {
"$ref": "#/definitions/quests.PaginatedQuestsResponse"
},
"owned": {
"$ref": "#/definitions/quests.PaginatedQuestsResponse"
},
"registered": {
"$ref": "#/definitions/quests.PaginatedQuestsResponse"
}
}
},
"storage.AccessType": {
"type": "string",
"enum": [
"public",
"link_only"
],
"x-enum-varnames": [
"Public",
"LinkOnly"
"AccessPublic",
"AccessLinkOnly"
]
},
"storage.CreateQuestRequest": {
Expand Down
81 changes: 79 additions & 2 deletions src/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,55 @@
}
},
"/quest": {
"get": {
"summary": "Get many quests sorted by start time and finished status",
"parameters": [
{
"maxLength": 3,
"minLength": 0,
"type": "array",
"items": {
"enum": [
"all",
"registered",
"owned"
],
"type": "string"
},
"collectionFormat": "csv",
"description": "Fields to return",
"name": "fields",
"in": "query"
},
{
"type": "integer",
"default": 50,
"description": "Number of quests to return for each field",
"name": "page_size",
"in": "query"
},
{
"type": "string",
"description": "Page ID to return. Mutually exclusive to multiple fields",
"name": "page_id",
"in": "query"
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/quests.Quests"
}
},
"400": {
"description": "Bad Request"
},
"401": {
"description": "Unauthorized"
}
}
},
"post": {
"summary": "Create new quest",
"parameters": [
Expand Down Expand Up @@ -487,15 +536,43 @@
}
}
},
"quests.PaginatedQuestsResponse": {
"type": "object",
"properties": {
"next_page_id": {
"type": "string"
},
"quests": {
"type": "array",
"items": {
"$ref": "#/definitions/storage.Quest"
}
}
}
},
"quests.Quests": {
"type": "object",
"properties": {
"all": {
"$ref": "#/definitions/quests.PaginatedQuestsResponse"
},
"owned": {
"$ref": "#/definitions/quests.PaginatedQuestsResponse"
},
"registered": {
"$ref": "#/definitions/quests.PaginatedQuestsResponse"
}
}
},
"storage.AccessType": {
"type": "string",
"enum": [
"public",
"link_only"
],
"x-enum-varnames": [
"Public",
"LinkOnly"
"AccessPublic",
"AccessLinkOnly"
]
},
"storage.CreateQuestRequest": {
Expand Down
56 changes: 54 additions & 2 deletions src/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,32 @@ definitions:
username:
type: string
type: object
quests.PaginatedQuestsResponse:
properties:
next_page_id:
type: string
quests:
items:
$ref: '#/definitions/storage.Quest'
type: array
type: object
quests.Quests:
properties:
all:
$ref: '#/definitions/quests.PaginatedQuestsResponse'
owned:
$ref: '#/definitions/quests.PaginatedQuestsResponse'
registered:
$ref: '#/definitions/quests.PaginatedQuestsResponse'
type: object
storage.AccessType:
enum:
- public
- link_only
type: string
x-enum-varnames:
- Public
- LinkOnly
- AccessPublic
- AccessLinkOnly
storage.CreateQuestRequest:
properties:
access:
Expand Down Expand Up @@ -264,6 +282,40 @@ paths:
description: Unsupported Media Type
summary: Sign in to user account and return auth data
/quest:
get:
parameters:
- collectionFormat: csv
description: Fields to return
in: query
items:
enum:
- all
- registered
- owned
type: string
maxLength: 3
minLength: 0
name: fields
type: array
- default: 50
description: Number of quests to return for each field
in: query
name: page_size
type: integer
- description: Page ID to return. Mutually exclusive to multiple fields
in: query
name: page_id
type: string
responses:
"200":
description: OK
schema:
$ref: '#/definitions/quests.Quests'
"400":
description: Bad Request
"401":
description: Unauthorized
summary: Get many quests sorted by start time and finished status
post:
parameters:
- description: Main quest information
Expand Down
45 changes: 45 additions & 0 deletions src/internal/handlers/quest/quest.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package quest
import (
"errors"
"net/http"
"strconv"

"github.com/gin-gonic/gin"
"go.uber.org/zap"
Expand Down Expand Up @@ -100,6 +101,50 @@ func (h *Handler) HandleGet(c *gin.Context) error {
return nil
}

const defaultPageSize = 50

// HandleGetMany handles GET /quest request
//
// @Summary Get many quests sorted by start time and finished status
// @Param fields query []string false "Fields to return" Enums(all, registered, owned) minlength(0) maxlength(3)
//
// @Param page_size query int false "Number of quests to return for each field" default(50)
// @Param page_id query string false "Page ID to return. Mutually exclusive to multiple fields"
// @Success 200 {object} quests.Quests
// @Failure 400
// @Failure 401
// @Router /quest [get]
func (h *Handler) HandleGetMany(c *gin.Context) error {
uauth, err := jwt.GetUserFromContext(c)
if err != nil {
return xerrors.Errorf("%w", err)
}

fields := c.QueryArray("fields")
pageSizeStr := c.Query("page_size")
pageSize := defaultPageSize
if pageSizeStr != "" {
var err error
pageSize, err = strconv.Atoi(pageSizeStr)
if err != nil {
return httperrors.Errorf(http.StatusBadRequest, "parse page size: %w", err)
}
}
pageID := c.Query("page_id")

s, err := h.clientFactory.NewStorage(c, dbnode.Alive)
if err != nil {
return xerrors.Errorf("get storage: %w", err)
}
gotQuests, err := quests.ReadQuests(c, s, uauth, fields, pageID, pageSize)
if err != nil {
return xerrors.Errorf("read quests: %w", err)
}

c.JSON(http.StatusOK, gotQuests)
return nil
}

// HandleUpdate handles POST /quest/:id request
//
// @Summary Update main quest information
Expand Down
2 changes: 1 addition & 1 deletion src/internal/handlers/quest/quest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func TestHandleCreate(t *testing.T) {
req := storage.CreateQuestRequest{
Name: "questname",
Description: "test description yea",
Access: storage.Public,
Access: storage.AccessPublic,
StartTime: now,
MaxTeamCap: ptr.Int(228),
}
Expand Down
Loading

0 comments on commit f919fa7

Please sign in to comment.