From 608c1bf3a79aed8b0bf2432203d7949e58e62ffa Mon Sep 17 00:00:00 2001 From: Jonni Liljamo Date: Tue, 18 Apr 2023 10:34:11 +0300 Subject: [PATCH] rework: rename sdbapi to api, redo data structure --- {sdbapi => api}/Dockerfile | 0 {sdbapi => api}/LICENSE | 4 +- {sdbapi => api}/README.md | 4 +- {sdbapi => api}/apierror/apierror.go | 7 +++- {sdbapi => api}/auth/auth.go | 6 +-- {sdbapi => api}/db/db.go | 5 +-- {sdbapi => api}/db/game.go | 2 +- {sdbapi => api}/db/user.go | 4 +- {sdbapi => api}/dev-launch-alpine.sh | 0 {sdbapi => api}/dev-launch.sh | 4 +- {sdbapi => api}/docker-compose.yaml | 22 +++++------ {sdbapi => api}/go.mod | 0 {sdbapi => api}/go.sum | 0 {sdbapi => api}/handlers/allforming.go | 2 +- api/handlers/createaction.go | 44 ++++++++++++++++++++++ {sdbapi => api}/handlers/gamecreate.go | 2 +- {sdbapi => api}/handlers/gameinfo.go | 4 +- {sdbapi => api}/handlers/joingame.go | 2 +- {sdbapi => api}/handlers/misc.go | 4 +- {sdbapi => api}/handlers/mygames.go | 2 +- {sdbapi => api}/handlers/patchgamestate.go | 2 +- {sdbapi => api}/handlers/usercreate.go | 4 +- {sdbapi => api}/handlers/userinfo.go | 2 +- {sdbapi => api}/handlers/userinfop.go | 4 +- {sdbapi => api}/handlers/usertoken.go | 4 +- {sdbapi => api}/main.go | 27 ++++--------- {sdbapi => api}/main_test.go | 4 +- {sdbapi => api}/middlewares/auth.go | 4 +- {sdbapi => api}/models/action.go | 17 ++++++--- {sdbapi => api}/models/game.go | 13 +++++-- {sdbapi => api}/models/user.go | 2 +- sdbapi/models/gamedata.go | 35 ----------------- 32 files changed, 125 insertions(+), 111 deletions(-) rename {sdbapi => api}/Dockerfile (100%) rename {sdbapi => api}/LICENSE (86%) rename {sdbapi => api}/README.md (90%) rename {sdbapi => api}/apierror/apierror.go (93%) rename {sdbapi => api}/auth/auth.go (91%) rename {sdbapi => api}/db/db.go (82%) rename {sdbapi => api}/db/game.go (93%) rename {sdbapi => api}/db/user.go (88%) rename {sdbapi => api}/dev-launch-alpine.sh (100%) rename {sdbapi => api}/dev-launch.sh (77%) rename {sdbapi => api}/docker-compose.yaml (58%) rename {sdbapi => api}/go.mod (100%) rename {sdbapi => api}/go.sum (100%) rename {sdbapi => api}/handlers/allforming.go (94%) create mode 100644 api/handlers/createaction.go rename {sdbapi => api}/handlers/gamecreate.go (94%) rename {sdbapi => api}/handlers/gameinfo.go (78%) rename {sdbapi => api}/handlers/joingame.go (97%) rename {sdbapi => api}/handlers/misc.go (75%) rename {sdbapi => api}/handlers/mygames.go (94%) rename {sdbapi => api}/handlers/patchgamestate.go (98%) rename {sdbapi => api}/handlers/usercreate.go (93%) rename {sdbapi => api}/handlers/userinfo.go (93%) rename {sdbapi => api}/handlers/userinfop.go (92%) rename {sdbapi => api}/handlers/usertoken.go (92%) rename {sdbapi => api}/main.go (69%) rename {sdbapi => api}/main_test.go (84%) rename {sdbapi => api}/middlewares/auth.go (91%) rename {sdbapi => api}/models/action.go (54%) rename {sdbapi => api}/models/game.go (81%) rename {sdbapi => api}/models/user.go (97%) delete mode 100644 sdbapi/models/gamedata.go diff --git a/sdbapi/Dockerfile b/api/Dockerfile similarity index 100% rename from sdbapi/Dockerfile rename to api/Dockerfile diff --git a/sdbapi/LICENSE b/api/LICENSE similarity index 86% rename from sdbapi/LICENSE rename to api/LICENSE index 774169e..c8ad322 100644 --- a/sdbapi/LICENSE +++ b/api/LICENSE @@ -1,5 +1,5 @@ -sdbapi is an API for a deck building game. -Copyright (C) 2022 Jonni Liljamo +The API for Laurelin. +Copyright (C) 2023 Jonni Liljamo This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. diff --git a/sdbapi/README.md b/api/README.md similarity index 90% rename from sdbapi/README.md rename to api/README.md index 4a3e81c..3eaf517 100644 --- a/sdbapi/README.md +++ b/api/README.md @@ -1,7 +1,7 @@ -# Deck Builder API +# Laurelin API ## Deployment -Configure `SDBAPI_JWT_SECRET` in `docker-compose.yaml` to be randomized. +Configure `LAURELINAPI_JWT_SECRET` in `docker-compose.yaml` to be randomized. ## API Doc Paths may change in the future. diff --git a/sdbapi/apierror/apierror.go b/api/apierror/apierror.go similarity index 93% rename from sdbapi/apierror/apierror.go rename to api/apierror/apierror.go index 4546bea..40dfe4f 100644 --- a/sdbapi/apierror/apierror.go +++ b/api/apierror/apierror.go @@ -1,5 +1,5 @@ /* - * This file is part of sdbapi + * This file is part of laurelin_api * Copyright (C) 2023 Jonni Liljamo * * Licensed under GPL-3.0-only. @@ -42,6 +42,11 @@ var ( GameFull APIError = APIError{3007, "GameFull", "game is full"} ) +// action related errors +var ( + ActionCreationFailed APIError = APIError{4000, "ActionCreationFailed", "action creation failed"} +) + type APIError struct { ID uint16 `json:"id"` Name string `json:"name"` diff --git a/sdbapi/auth/auth.go b/api/auth/auth.go similarity index 91% rename from sdbapi/auth/auth.go rename to api/auth/auth.go index 8d7423a..a5768c6 100644 --- a/sdbapi/auth/auth.go +++ b/api/auth/auth.go @@ -1,6 +1,6 @@ /* - * This file is part of sdbapi - * Copyright (C) 2022 Jonni Liljamo + * This file is part of laurelin_api + * Copyright (C) 2023 Jonni Liljamo * * Licensed under GPL-3.0-only. * See LICENSE for licensing information. @@ -17,7 +17,7 @@ import ( "github.com/golang-jwt/jwt/v4" ) -var JWTSecret = []byte(os.Getenv("SDBAPI_JWT_SECRET")) +var JWTSecret = []byte(os.Getenv("LAURELINAPI_JWT_SECRET")) type JWTClaims struct { Username string `json:"username"` diff --git a/sdbapi/db/db.go b/api/db/db.go similarity index 82% rename from sdbapi/db/db.go rename to api/db/db.go index 20872f1..9d7b938 100644 --- a/sdbapi/db/db.go +++ b/api/db/db.go @@ -1,6 +1,6 @@ /* - * This file is part of sdbapi - * Copyright (C) 2022 Jonni Liljamo + * This file is part of laurelin_api + * Copyright (C) 2023 Jonni Liljamo * * Licensed under GPL-3.0-only. * See LICENSE for licensing information. @@ -33,5 +33,4 @@ func Migrate() { DbConn.AutoMigrate(&models.User{}) DbConn.AutoMigrate(&models.Game{}) DbConn.AutoMigrate(&models.Action{}) - DbConn.AutoMigrate(&models.GameData{}) } diff --git a/sdbapi/db/game.go b/api/db/game.go similarity index 93% rename from sdbapi/db/game.go rename to api/db/game.go index 13009e5..a9b0719 100644 --- a/sdbapi/db/game.go +++ b/api/db/game.go @@ -1,5 +1,5 @@ /* - * This file is part of sdbapi + * This file is part of laurelin_api * Copyright (C) 2023 Jonni Liljamo * * Licensed under GPL-3.0-only. diff --git a/sdbapi/db/user.go b/api/db/user.go similarity index 88% rename from sdbapi/db/user.go rename to api/db/user.go index d978bcc..5cecefe 100644 --- a/sdbapi/db/user.go +++ b/api/db/user.go @@ -1,6 +1,6 @@ /* - * This file is part of sdbapi - * Copyright (C) 2022 Jonni Liljamo + * This file is part of laurelin_api + * Copyright (C) 2023 Jonni Liljamo * * Licensed under GPL-3.0-only. * See LICENSE for licensing information. diff --git a/sdbapi/dev-launch-alpine.sh b/api/dev-launch-alpine.sh similarity index 100% rename from sdbapi/dev-launch-alpine.sh rename to api/dev-launch-alpine.sh diff --git a/sdbapi/dev-launch.sh b/api/dev-launch.sh similarity index 77% rename from sdbapi/dev-launch.sh rename to api/dev-launch.sh index c264ba9..45e1cb6 100755 --- a/sdbapi/dev-launch.sh +++ b/api/dev-launch.sh @@ -1,6 +1,6 @@ #!/bin/bash -export COMPOSE_PROJECT_NAME=sdbapi +export COMPOSE_PROJECT_NAME=laurelinapi function confirm() { echo -n "$@ [y/N]: " @@ -18,7 +18,7 @@ function confirm() { } confirm reset containers? && docker compose down -confirm reset database? && docker volume rm sdbapi_apidb_data +confirm reset database? && docker volume rm laurelinapi_apidb_data confirm rebuild api? && docker compose build diff --git a/sdbapi/docker-compose.yaml b/api/docker-compose.yaml similarity index 58% rename from sdbapi/docker-compose.yaml rename to api/docker-compose.yaml index 1cae5bb..6a3ad4f 100644 --- a/sdbapi/docker-compose.yaml +++ b/api/docker-compose.yaml @@ -9,9 +9,9 @@ volumes: driver: local services: - sdbapidb: + laurelinapidb: image: postgres:15.1-alpine - container_name: sdbapidb + container_name: laurelinapidb restart: always networks: - internal @@ -20,14 +20,14 @@ services: volumes: - apidb_data:/var/lib/postgresql/data environment: - POSTGRES_USER: sdbapi - POSTGRES_PASSWORD: sdbapi - POSTGRES_DB: sdbapi + POSTGRES_USER: lau + POSTGRES_PASSWORD: lau + POSTGRES_DB: laurelinapi - sdbapi: + laurelinapi: build: . - image: sdbapi - container_name: sdbapi + image: laurelinapi + container_name: laurelinapi restart: always networks: - internal @@ -35,7 +35,7 @@ services: - "8080:3000" environment: GIN_MODE: "release" # or "debug" for debug logs - SDBAPI_JWT_SECRET: "XpNYdG7vgvgPPuezrtZqt4CJIUuxNP7c" - GORM_DB_STRING: "host=sdbapidb user=sdbapi password=sdbapi dbname=sdbapi port=5432 sslmode=disable" + LAURELINAPI_JWT_SECRET: "XpNYdG7vgvgPPuezrtZqt4CJIUuxNP7c" + GORM_DB_STRING: "host=laurelinapidb user=lau password=lau dbname=laurelinapi port=5432 sslmode=disable" depends_on: - - sdbapidb + - laurelinapidb diff --git a/sdbapi/go.mod b/api/go.mod similarity index 100% rename from sdbapi/go.mod rename to api/go.mod diff --git a/sdbapi/go.sum b/api/go.sum similarity index 100% rename from sdbapi/go.sum rename to api/go.sum diff --git a/sdbapi/handlers/allforming.go b/api/handlers/allforming.go similarity index 94% rename from sdbapi/handlers/allforming.go rename to api/handlers/allforming.go index ef7eebb..2a5c4e6 100644 --- a/sdbapi/handlers/allforming.go +++ b/api/handlers/allforming.go @@ -1,5 +1,5 @@ /* - * This file is part of sdbapi + * This file is part of laurelin_api * Copyright (C) 2023 Jonni Liljamo * * Licensed under GPL-3.0-only. diff --git a/api/handlers/createaction.go b/api/handlers/createaction.go new file mode 100644 index 0000000..989bda6 --- /dev/null +++ b/api/handlers/createaction.go @@ -0,0 +1,44 @@ +/* + * This file is part of laurelin_api + * Copyright (C) 2023 Jonni Liljamo + * + * Licensed under GPL-3.0-only. + * See LICENSE for licensing information. + */ + +package handlers + +import ( + "api/db" + "api/models" + "api/apierror" + "net/http" + + "gorm.io/datatypes" + "github.com/gin-gonic/gin" +) + +func CreateAction(c *gin.Context) { + var input models.PostAction + if err := c.ShouldBindJSON(&input); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": apierror.InvalidInput}) + c.Abort() + return + } + + var action models.Action + + action.GameID = input.GameID + action.Invoker = input.Invoker + action.Target = input.Target + action.Command = datatypes.JSON(input.Command) + + entry := db.DbConn.Create(&action) + if entry.Error != nil { + c.JSON(http.StatusInternalServerError, gin.H{"error": apierror.ActionCreationFailed}) + c.Abort() + return + } + + c.JSON(http.StatusCreated, action) +} diff --git a/sdbapi/handlers/gamecreate.go b/api/handlers/gamecreate.go similarity index 94% rename from sdbapi/handlers/gamecreate.go rename to api/handlers/gamecreate.go index 7d1d34a..04a8ada 100644 --- a/sdbapi/handlers/gamecreate.go +++ b/api/handlers/gamecreate.go @@ -1,5 +1,5 @@ /* - * This file is part of sdbapi + * This file is part of laurelin_api * Copyright (C) 2023 Jonni Liljamo * * Licensed under GPL-3.0-only. diff --git a/sdbapi/handlers/gameinfo.go b/api/handlers/gameinfo.go similarity index 78% rename from sdbapi/handlers/gameinfo.go rename to api/handlers/gameinfo.go index 01caf1f..a0c0a0c 100644 --- a/sdbapi/handlers/gameinfo.go +++ b/api/handlers/gameinfo.go @@ -1,5 +1,5 @@ /* - * This file is part of sdbapi + * This file is part of laurelin_api * Copyright (C) 2023 Jonni Liljamo * * Licensed under GPL-3.0-only. @@ -22,7 +22,7 @@ func GameInfo(c *gin.Context) { // check if the game exists var game models.Game - record := db.DbConn.Preload("Host").Preload("Guest").Where("id = ?", id).First(&game) + record := db.DbConn.Preload("Host").Preload("Guest").Preload("Actions").Where("id = ?", id).First(&game) if record.Error != nil { c.JSON(http.StatusNotFound, gin.H{"error": apierror.GameNotFound}) c.Abort() diff --git a/sdbapi/handlers/joingame.go b/api/handlers/joingame.go similarity index 97% rename from sdbapi/handlers/joingame.go rename to api/handlers/joingame.go index 013e5a1..4b799bf 100644 --- a/sdbapi/handlers/joingame.go +++ b/api/handlers/joingame.go @@ -1,5 +1,5 @@ /* - * This file is part of sdbapi + * This file is part of laurelin_api * Copyright (C) 2023 Jonni Liljamo * * Licensed under GPL-3.0-only. diff --git a/sdbapi/handlers/misc.go b/api/handlers/misc.go similarity index 75% rename from sdbapi/handlers/misc.go rename to api/handlers/misc.go index 33872b5..d83793b 100644 --- a/sdbapi/handlers/misc.go +++ b/api/handlers/misc.go @@ -1,6 +1,6 @@ /* - * This file is part of sdbapi - * Copyright (C) 2022 Jonni Liljamo + * This file is part of laurelin_api + * Copyright (C) 2023 Jonni Liljamo * * Licensed under GPL-3.0-only. * See LICENSE for licensing information. diff --git a/sdbapi/handlers/mygames.go b/api/handlers/mygames.go similarity index 94% rename from sdbapi/handlers/mygames.go rename to api/handlers/mygames.go index 610557a..3441710 100644 --- a/sdbapi/handlers/mygames.go +++ b/api/handlers/mygames.go @@ -1,5 +1,5 @@ /* - * This file is part of sdbapi + * This file is part of laurelin_api * Copyright (C) 2023 Jonni Liljamo * * Licensed under GPL-3.0-only. diff --git a/sdbapi/handlers/patchgamestate.go b/api/handlers/patchgamestate.go similarity index 98% rename from sdbapi/handlers/patchgamestate.go rename to api/handlers/patchgamestate.go index a1486b0..7abb8bc 100644 --- a/sdbapi/handlers/patchgamestate.go +++ b/api/handlers/patchgamestate.go @@ -1,5 +1,5 @@ /* - * This file is part of sdbapi + * This file is part of laurelin_api * Copyright (C) 2023 Jonni Liljamo * * Licensed under GPL-3.0-only. diff --git a/sdbapi/handlers/usercreate.go b/api/handlers/usercreate.go similarity index 93% rename from sdbapi/handlers/usercreate.go rename to api/handlers/usercreate.go index be1d31d..b0fb8c3 100644 --- a/sdbapi/handlers/usercreate.go +++ b/api/handlers/usercreate.go @@ -1,6 +1,6 @@ /* - * This file is part of sdbapi - * Copyright (C) 2022 Jonni Liljamo + * This file is part of laurelin_api + * Copyright (C) 2023 Jonni Liljamo * * Licensed under GPL-3.0-only. * See LICENSE for licensing information. diff --git a/sdbapi/handlers/userinfo.go b/api/handlers/userinfo.go similarity index 93% rename from sdbapi/handlers/userinfo.go rename to api/handlers/userinfo.go index e040187..d795cfd 100644 --- a/sdbapi/handlers/userinfo.go +++ b/api/handlers/userinfo.go @@ -1,5 +1,5 @@ /* - * This file is part of sdbapi + * This file is part of laurelin_api * Copyright (C) 2023 Jonni Liljamo * * Licensed under GPL-3.0-only. diff --git a/sdbapi/handlers/userinfop.go b/api/handlers/userinfop.go similarity index 92% rename from sdbapi/handlers/userinfop.go rename to api/handlers/userinfop.go index d5da643..97692a6 100644 --- a/sdbapi/handlers/userinfop.go +++ b/api/handlers/userinfop.go @@ -1,6 +1,6 @@ /* - * This file is part of sdbapi - * Copyright (C) 2022 Jonni Liljamo + * This file is part of laurelin_api + * Copyright (C) 2023 Jonni Liljamo * * Licensed under GPL-3.0-only. * See LICENSE for licensing information. diff --git a/sdbapi/handlers/usertoken.go b/api/handlers/usertoken.go similarity index 92% rename from sdbapi/handlers/usertoken.go rename to api/handlers/usertoken.go index 6debe4f..4b7c4b3 100644 --- a/sdbapi/handlers/usertoken.go +++ b/api/handlers/usertoken.go @@ -1,6 +1,6 @@ /* - * This file is part of sdbapi - * Copyright (C) 2022 Jonni Liljamo + * This file is part of laurelin_api + * Copyright (C) 2023 Jonni Liljamo * * Licensed under GPL-3.0-only. * See LICENSE for licensing information. diff --git a/sdbapi/main.go b/api/main.go similarity index 69% rename from sdbapi/main.go rename to api/main.go index 5b2f8b8..bb145df 100644 --- a/sdbapi/main.go +++ b/api/main.go @@ -1,6 +1,6 @@ /* - * This file is part of sdbapi - * Copyright (C) 2022 Jonni Liljamo + * This file is part of laurelin_api + * Copyright (C) 2023 Jonni Liljamo * * Licensed under GPL-3.0-only. * See LICENSE for licensing information. @@ -14,7 +14,6 @@ import ( "api/middlewares" "log" "os" - "syscall" "github.com/gin-gonic/gin" ) @@ -23,11 +22,6 @@ func main() { // stores various errors during startup var err error - err = setUlimits() - if err != nil { - log.Fatal("failed to set Ulimits") - } - dbConnectionString := os.Getenv("GORM_DB_STRING") if dbConnectionString == "" { log.Fatal("environment variable 'GORM_DB_STRING' is not set") @@ -52,7 +46,7 @@ func createRouter() *gin.Engine { api.GET("/info", handlers.Info) user := api.Group("/user") { - user.POST("/register", handlers.CreateUser) + user.POST("/", handlers.CreateUser) user.POST("/token", handlers.GenerateToken) user.GET("/:id", handlers.UserInfo) userp := user.Group("/_").Use(middlewares.Auth()) @@ -64,21 +58,16 @@ func createRouter() *gin.Engine { { game.GET("/:id", handlers.GameInfo) game.GET("/all_forming", handlers.FormingGames) - game.POST("/create", handlers.CreateGame) + game.POST("/", handlers.CreateGame) game.PATCH("/:id/state", handlers.PatchGameState) game.GET("/my_games", handlers.MyGames) game.POST("/:id/join", handlers.JoinGame) } + action := api.Group("/game/action").Use(middlewares.Auth()) + { + action.POST("/", handlers.CreateAction) + } } return router } -// Set NOFILE limit to max, to allow more concurrent websocket connections -func setUlimits() error { - var rlimit syscall.Rlimit - if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rlimit); err != nil { - return err - } - rlimit.Cur = rlimit.Max - return syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rlimit) -} diff --git a/sdbapi/main_test.go b/api/main_test.go similarity index 84% rename from sdbapi/main_test.go rename to api/main_test.go index 9e992e0..6211f8c 100644 --- a/sdbapi/main_test.go +++ b/api/main_test.go @@ -1,6 +1,6 @@ /* - * This file is part of sdbapi - * Copyright (C) 2022 Jonni Liljamo + * This file is part of laurelin_api + * Copyright (C) 2023 Jonni Liljamo * * Licensed under GPL-3.0-only. * See LICENSE for licensing information. diff --git a/sdbapi/middlewares/auth.go b/api/middlewares/auth.go similarity index 91% rename from sdbapi/middlewares/auth.go rename to api/middlewares/auth.go index 171625d..f5b92bc 100644 --- a/sdbapi/middlewares/auth.go +++ b/api/middlewares/auth.go @@ -1,6 +1,6 @@ /* - * This file is part of sdbapi - * Copyright (C) 2022 Jonni Liljamo + * This file is part of laurelin_api + * Copyright (C) 2023 Jonni Liljamo * * Licensed under GPL-3.0-only. * See LICENSE for licensing information. diff --git a/sdbapi/models/action.go b/api/models/action.go similarity index 54% rename from sdbapi/models/action.go rename to api/models/action.go index 3b39824..697a93f 100644 --- a/sdbapi/models/action.go +++ b/api/models/action.go @@ -1,6 +1,6 @@ /* - * This file is part of sdbapi - * Copyright (C) 2022 Jonni Liljamo + * This file is part of laurelin_api + * Copyright (C) 2023 Jonni Liljamo * * Licensed under GPL-3.0-only. * See LICENSE for licensing information. @@ -20,8 +20,15 @@ type Action struct { CreatedAt time.Time `json:"created_at"` UpdatedAt time.Time `json:"updated_at"` DeletedAt gorm.DeletedAt `json:"-" gorm:"index"` - GameDataID string `json:"game_data_id"` + GameID string `json:"game_id"` Invoker string `json:"invoker"` - Data datatypes.JSON `json:"data"` - Timestamp time.Time `json:"timestamp" gorm:"type:timestamptz"` + Target string `json:"target"` + Command datatypes.JSON `json:"command"` +} + +type PostAction struct { + GameID string `json:"game_id"` + Invoker string `json:"invoker"` + Target string `json:"target"` + Command string `json:"command"` } diff --git a/sdbapi/models/game.go b/api/models/game.go similarity index 81% rename from sdbapi/models/game.go rename to api/models/game.go index d9261e6..1ead7ed 100644 --- a/sdbapi/models/game.go +++ b/api/models/game.go @@ -1,5 +1,5 @@ /* - * This file is part of sdbapi + * This file is part of laurelin_api * Copyright (C) 2023 Jonni Liljamo * * Licensed under GPL-3.0-only. @@ -21,17 +21,22 @@ const ( GAMESTATE_CANCELLED uint8 = 3 ) +const ( + GAMETURN_HOST uint8 = 0 + GAMETURN_GUEST uint8 = 1 +) + type Game struct { ID string `json:"id" gorm:"primarykey;type:uuid;default:gen_random_uuid()"` CreatedAt time.Time `json:"created_at"` UpdatedAt time.Time `json:"updated_at"` DeletedAt gorm.DeletedAt `json:"-" gorm:"index"` + EndedAt time.Time `json:"ended_at" gorm:"type:timestamptz"` HostID string `json:"host_id"` Host User `json:"host" gorm:"constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"` GuestID string `json:"guest_id" gorm:"default:NULL;"` Guest User `json:"guest" gorm:"constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"` State uint8 `json:"state" gorm:"type:smallint"` - EndedAt time.Time `json:"ended_at" gorm:"type:timestamptz"` - GameDataID string `json:"game_data_id" gorm:"default:NULL;"` - GameData GameData `json:"game_data" gorm:"constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"` + Turn uint8 `json:"turn" gorm:"type:smallint"` + Actions []Action `json:"actions" gorm:"foreignKey:GameID"` } diff --git a/sdbapi/models/user.go b/api/models/user.go similarity index 97% rename from sdbapi/models/user.go rename to api/models/user.go index efe2ff5..7ae66e5 100644 --- a/sdbapi/models/user.go +++ b/api/models/user.go @@ -1,5 +1,5 @@ /* - * This file is part of sdbapi + * This file is part of laurelin_api * Copyright (C) 2023 Jonni Liljamo * * Licensed under GPL-3.0-only. diff --git a/sdbapi/models/gamedata.go b/sdbapi/models/gamedata.go deleted file mode 100644 index 9742d6b..0000000 --- a/sdbapi/models/gamedata.go +++ /dev/null @@ -1,35 +0,0 @@ -/* - * This file is part of sdbapi - * Copyright (C) 2023 Jonni Liljamo - * - * Licensed under GPL-3.0-only. - * See LICENSE for licensing information. - */ - -package models - -import ( - "time" - - "gorm.io/datatypes" - "gorm.io/gorm" -) - -const ( - GAMETURN_HOST uint8 = 0 - GAMETURN_GUEST uint8 = 1 -) - -type GameData struct { - ID string `json:"id" gorm:"primarykey;type:uuid;default:gen_random_uuid()"` - CreatedAt time.Time `json:"created_at"` - UpdatedAt time.Time `json:"updated_at"` - DeletedAt gorm.DeletedAt `json:"-" gorm:"index"` - Actions []Action `json:"actions" gorm:"foreignKey:GameDataID"` - Turn uint8 `json:"turn" gorm:"type:smallint"` - // NOTE: These are not final - HostHand datatypes.JSON `json:"host_hand"` - HostDeck datatypes.JSON `json:"host_deck"` - GuestHand datatypes.JSON `json:"guest_hand"` - GuestDeck datatypes.JSON `json:"guest_deck"` -} -- 2.44.1