@@ -5,24 +5,24 @@ import (
55 "log/slog"
66 "net/http"
77
8+ openapi_types "github.com/oapi-codegen/runtime/types"
9+
10+ "ctf01d/internal/helper"
811 "ctf01d/internal/model"
912 "ctf01d/internal/repository"
1013 "ctf01d/internal/server"
11- api_helpers "ctf01d/internal/utils"
12-
13- openapi_types "github.com/oapi-codegen/runtime/types"
1414)
1515
1616func (h * Handler ) CreateGame (w http.ResponseWriter , r * http.Request ) {
1717 var game server.GameRequest
1818 var err error
1919 if err := json .NewDecoder (r .Body ).Decode (& game ); err != nil {
2020 slog .Warn (err .Error (), "handler" , "CreateGame" )
21- api_helpers .RespondWithJSON (w , http .StatusBadRequest , map [string ]string {"error" : "Invalid request payload" })
21+ helper .RespondWithJSON (w , http .StatusBadRequest , map [string ]string {"error" : "Invalid request payload" })
2222 return
2323 }
2424 if game .EndTime .Before (game .StartTime ) {
25- api_helpers .RespondWithJSON (w , http .StatusBadRequest , map [string ]string {"error" : "EndTime must be after StartTime" })
25+ helper .RespondWithJSON (w , http .StatusBadRequest , map [string ]string {"error" : "EndTime must be after StartTime" })
2626 return
2727 }
2828 repo := repository .NewGameRepository (h .DB )
@@ -35,55 +35,55 @@ func (h *Handler) CreateGame(w http.ResponseWriter, r *http.Request) {
3535 err = repo .Create (r .Context (), newGame )
3636 if err != nil {
3737 slog .Warn (err .Error (), "handler" , "CreateGame" )
38- api_helpers .RespondWithJSON (w , http .StatusInternalServerError , map [string ]string {"error" : "Failed to create game" })
38+ helper .RespondWithJSON (w , http .StatusInternalServerError , map [string ]string {"error" : "Failed to create game" })
3939 return
4040 }
41- api_helpers .RespondWithJSON (w , http .StatusOK , newGame .ToResponse ())
41+ helper .RespondWithJSON (w , http .StatusOK , newGame .ToResponse ())
4242}
4343
4444func (h * Handler ) DeleteGame (w http.ResponseWriter , r * http.Request , id openapi_types.UUID ) {
4545 repo := repository .NewGameRepository (h .DB )
4646 if err := repo .Delete (r .Context (), id ); err != nil {
4747 slog .Warn (err .Error (), "handler" , "DeleteGame" )
48- api_helpers .RespondWithJSON (w , http .StatusInternalServerError , map [string ]string {"error" : "Failed to delete game" })
48+ helper .RespondWithJSON (w , http .StatusInternalServerError , map [string ]string {"error" : "Failed to delete game" })
4949 return
5050 }
51- api_helpers .RespondWithJSON (w , http .StatusOK , map [string ]string {"data" : "Game deleted successfully" })
51+ helper .RespondWithJSON (w , http .StatusOK , map [string ]string {"data" : "Game deleted successfully" })
5252}
5353
5454func (h * Handler ) GetGameById (w http.ResponseWriter , r * http.Request , id openapi_types.UUID ) {
5555 repo := repository .NewGameRepository (h .DB )
5656 game , err := repo .GetGameDetails (r .Context (), id ) // короткий ответ, если нужен см. GetById
5757 if err != nil {
5858 slog .Warn (err .Error (), "handler" , "GetGameById" )
59- api_helpers .RespondWithJSON (w , http .StatusInternalServerError , map [string ]string {"error" : "Failed to fetch game" })
59+ helper .RespondWithJSON (w , http .StatusInternalServerError , map [string ]string {"error" : "Failed to fetch game" })
6060 return
6161 }
62- api_helpers .RespondWithJSON (w , http .StatusOK , game .ToResponseGameDetails ())
62+ helper .RespondWithJSON (w , http .StatusOK , game .ToResponseGameDetails ())
6363}
6464
6565func (h * Handler ) ListGames (w http.ResponseWriter , r * http.Request ) {
6666 repo := repository .NewGameRepository (h .DB )
6767 games , err := repo .ListGamesDetails (r .Context ())
6868 if err != nil {
6969 slog .Warn (err .Error (), "handler" , "ListGames" )
70- api_helpers .RespondWithJSON (w , http .StatusBadRequest , map [string ]string {"error" : "Failed to fetch games" })
70+ helper .RespondWithJSON (w , http .StatusBadRequest , map [string ]string {"error" : "Failed to fetch games" })
7171 return
7272 }
7373 gameResponses := make ([]* server.GameResponse , 0 , len (games ))
7474 for _ , game := range games {
7575 gameResponses = append (gameResponses , game .ToResponseGameDetails ())
7676 }
7777
78- api_helpers .RespondWithJSON (w , http .StatusOK , gameResponses )
78+ helper .RespondWithJSON (w , http .StatusOK , gameResponses )
7979}
8080
8181func (h * Handler ) UpdateGame (w http.ResponseWriter , r * http.Request , id openapi_types.UUID ) {
8282 // fixme update не проверяет есть ли запись в бд
8383 var game server.GameRequest
8484 if err := json .NewDecoder (r .Body ).Decode (& game ); err != nil {
8585 slog .Warn (err .Error (), "handler" , "UpdateGame" )
86- api_helpers .RespondWithJSON (w , http .StatusBadRequest , map [string ]string {"error" : "Invalid request payload" })
86+ helper .RespondWithJSON (w , http .StatusBadRequest , map [string ]string {"error" : "Invalid request payload" })
8787 return
8888 }
8989 repo := repository .NewGameRepository (h .DB )
@@ -96,8 +96,8 @@ func (h *Handler) UpdateGame(w http.ResponseWriter, r *http.Request, id openapi_
9696 err := repo .Update (r .Context (), updateGame )
9797 if err != nil {
9898 slog .Warn (err .Error (), "handler" , "UpdateGame" )
99- api_helpers .RespondWithJSON (w , http .StatusInternalServerError , map [string ]string {"error" : "Invalid request payload" })
99+ helper .RespondWithJSON (w , http .StatusInternalServerError , map [string ]string {"error" : "Invalid request payload" })
100100 return
101101 }
102- api_helpers .RespondWithJSON (w , http .StatusOK , map [string ]string {"data" : "Game updated successfully" })
102+ helper .RespondWithJSON (w , http .StatusOK , map [string ]string {"data" : "Game updated successfully" })
103103}
0 commit comments