Skip to content

Commit

Permalink
Added docs to create_game.go
Browse files Browse the repository at this point in the history
  • Loading branch information
justinfarrelldev committed Aug 26, 2024
1 parent 2873a7b commit 9351eba
Show file tree
Hide file tree
Showing 5 changed files with 191 additions and 6 deletions.
56 changes: 56 additions & 0 deletions docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,48 @@ const docTemplate = `{
"host": "{{.Host}}",
"basePath": "{{.BasePath}}",
"paths": {
"/game/create_game": {
"post": {
"description": "This endpoint creates a new multiplayer game, optionally protected by a password.",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"game"
],
"summary": "Create a new game",
"parameters": [
{
"description": "Game creation request body",
"name": "body",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/game.CreateGameArgs"
}
}
],
"responses": {
"201": {
"description": "Game successfully created",
"schema": {
"type": "string"
}
},
"400": {
"description": "Bad Request",
"schema": {}
},
"500": {
"description": "Internal Server Error",
"schema": {}
}
}
}
},
"/health": {
"get": {
"description": "Returns the status of the service.",
Expand Down Expand Up @@ -47,6 +89,20 @@ const docTemplate = `{
}
},
"definitions": {
"game.CreateGameArgs": {
"description": "Structure for the game creation request payload.",
"type": "object",
"properties": {
"password": {
"description": "Password is the password for the game.\nThis field is required if PasswordProtected is true.\nIt must be longer than 6 characters.",
"type": "string"
},
"password_protected": {
"description": "PasswordProtected indicates whether the game is password-protected.\nIf true, a password must be provided.",
"type": "boolean"
}
}
},
"health.Response": {
"type": "object",
"properties": {
Expand Down
56 changes: 56 additions & 0 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,48 @@
}
},
"paths": {
"/game/create_game": {
"post": {
"description": "This endpoint creates a new multiplayer game, optionally protected by a password.",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"game"
],
"summary": "Create a new game",
"parameters": [
{
"description": "Game creation request body",
"name": "body",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/game.CreateGameArgs"
}
}
],
"responses": {
"201": {
"description": "Game successfully created",
"schema": {
"type": "string"
}
},
"400": {
"description": "Bad Request",
"schema": {}
},
"500": {
"description": "Internal Server Error",
"schema": {}
}
}
}
},
"/health": {
"get": {
"description": "Returns the status of the service.",
Expand Down Expand Up @@ -38,6 +80,20 @@
}
},
"definitions": {
"game.CreateGameArgs": {
"description": "Structure for the game creation request payload.",
"type": "object",
"properties": {
"password": {
"description": "Password is the password for the game.\nThis field is required if PasswordProtected is true.\nIt must be longer than 6 characters.",
"type": "string"
},
"password_protected": {
"description": "PasswordProtected indicates whether the game is password-protected.\nIf true, a password must be provided.",
"type": "boolean"
}
}
},
"health.Response": {
"type": "object",
"properties": {
Expand Down
44 changes: 44 additions & 0 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
definitions:
game.CreateGameArgs:
description: Structure for the game creation request payload.
properties:
password:
description: |-
Password is the password for the game.
This field is required if PasswordProtected is true.
It must be longer than 6 characters.
type: string
password_protected:
description: |-
PasswordProtected indicates whether the game is password-protected.
If true, a password must be provided.
type: boolean
type: object
health.Response:
properties:
status:
Expand All @@ -13,6 +28,35 @@ info:
This project is not sponsored, maintained or affiliated with Activision.
title: Open Call to Power Server
paths:
/game/create_game:
post:
consumes:
- application/json
description: This endpoint creates a new multiplayer game, optionally protected
by a password.
parameters:
- description: Game creation request body
in: body
name: body
required: true
schema:
$ref: '#/definitions/game.CreateGameArgs'
produces:
- application/json
responses:
"201":
description: Game successfully created
schema:
type: string
"400":
description: Bad Request
schema: {}
"500":
description: Internal Server Error
schema: {}
summary: Create a new game
tags:
- game
/health:
get:
consumes:
Expand Down
39 changes: 34 additions & 5 deletions internal/game/create_game.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,34 @@ import (
"net/http"
)

type ExpectedBody struct {
PasswordProtected bool `json:"password_protected"`
Password string `json:"password"`
// CreateGameArgs represents the expected structure of the request body for creating a game.
//
// @Description Structure for the game creation request payload.
type CreateGameArgs struct {
// PasswordProtected indicates whether the game is password-protected.
// If true, a password must be provided.
PasswordProtected bool `json:"password_protected"`
// Password is the password for the game.
// This field is required if PasswordProtected is true.
// It must be longer than 6 characters.
Password string `json:"password"`
}

const ERROR_PASSWORD_TOO_SHORT = "password must be longer than 6 characters"

func CreateGame(_ http.ResponseWriter, r *http.Request) error {
// CreateGame handles the creation of a new game.
//
// @Summary Create a new game
// @Description This endpoint creates a new multiplayer game, optionally protected by a password.
// @Tags game
// @Accept json
// @Produce json
// @Param body body CreateGameArgs true "Game creation request body"
// @Success 201 {string} string "Game successfully created"
// @Failure 400 {object} error "Bad Request"
// @Failure 500 {object} error "Internal Server Error"
// @Router /game/create_game [post]
func CreateGame(w http.ResponseWriter, r *http.Request) error {

if r.Method != "POST" {
return errors.New("invalid request; request must be a POST request")
Expand All @@ -23,27 +43,36 @@ func CreateGame(_ http.ResponseWriter, r *http.Request) error {
decoder := json.NewDecoder(r.Body)
decoder.DisallowUnknownFields()

game := ExpectedBody{}
game := CreateGameArgs{}
err := decoder.Decode(&game)

if err != nil {
w.WriteHeader(http.StatusInternalServerError)

return errors.New("an error occurred while decoding the request body:" + err.Error())
}

if game.PasswordProtected && game.Password == "" {
w.WriteHeader(http.StatusBadRequest)

return errors.New("password is required when password_protected is true")
}

if !game.PasswordProtected && len(game.Password) > 0 {
w.WriteHeader(http.StatusBadRequest)

return errors.New("password was provided despite password_protected being set to false")
}

if len(game.Password) < 6 {
w.WriteHeader(http.StatusBadRequest)

return errors.New(ERROR_PASSWORD_TOO_SHORT)
}

// TODO salt & hash password here / handle it in Supabase or something then actually store the game somewhere

w.WriteHeader(http.StatusCreated)
fmt.Println("Successfully created game!")
return nil
}
2 changes: 1 addition & 1 deletion internal/game/create_game_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

func TestCreateGame_PasswordTooShort(t *testing.T) {
// Create a test request with a password that is less than 6 characters
body := ExpectedBody{
body := CreateGameArgs{
PasswordProtected: true,
Password: "123", // This password is less than 6 characters
}
Expand Down

0 comments on commit 9351eba

Please sign in to comment.