diff options
| -rw-r--r-- | backend/controllers/homeController.go | 25 | ||||
| -rw-r--r-- | backend/controllers/userController.go | 2 | ||||
| -rw-r--r-- | backend/models/models.go | 113 | ||||
| -rw-r--r-- | backend/models/requests.go | 8 | ||||
| -rw-r--r-- | backend/models/responses.go | 54 |
5 files changed, 94 insertions, 108 deletions
diff --git a/backend/controllers/homeController.go b/backend/controllers/homeController.go index d30c835..b5c6b60 100644 --- a/backend/controllers/homeController.go +++ b/backend/controllers/homeController.go | |||
| @@ -138,10 +138,7 @@ func SearchWithQuery(c *gin.Context) { | |||
| 138 | log.Println(query) | 138 | log.Println(query) |
| 139 | var response models.SearchResponse | 139 | var response models.SearchResponse |
| 140 | // Cache all maps for faster response | 140 | // Cache all maps for faster response |
| 141 | var maps = []struct { | 141 | var maps = []models.MapShort{ |
| 142 | ID int `json:"id"` | ||
| 143 | Name string `json:"name"` | ||
| 144 | }{ | ||
| 145 | {ID: 1, Name: "Container Ride"}, | 142 | {ID: 1, Name: "Container Ride"}, |
| 146 | {ID: 2, Name: "Portal Carousel"}, | 143 | {ID: 2, Name: "Portal Carousel"}, |
| 147 | {ID: 3, Name: "Portal Gun"}, | 144 | {ID: 3, Name: "Portal Gun"}, |
| @@ -253,10 +250,7 @@ func SearchWithQuery(c *gin.Context) { | |||
| 253 | {ID: 109, Name: "Gel Maze"}, | 250 | {ID: 109, Name: "Gel Maze"}, |
| 254 | {ID: 110, Name: "Crazier Box"}, | 251 | {ID: 110, Name: "Crazier Box"}, |
| 255 | } | 252 | } |
| 256 | var filteredMaps []struct { | 253 | var filteredMaps []models.MapShort |
| 257 | ID int `json:"id"` | ||
| 258 | Name string `json:"name"` | ||
| 259 | } | ||
| 260 | for _, m := range maps { | 254 | for _, m := range maps { |
| 261 | if strings.Contains(strings.ToLower(m.Name), strings.ToLower(query)) { | 255 | if strings.Contains(strings.ToLower(m.Name), strings.ToLower(query)) { |
| 262 | filteredMaps = append(filteredMaps, m) | 256 | filteredMaps = append(filteredMaps, m) |
| @@ -264,10 +258,7 @@ func SearchWithQuery(c *gin.Context) { | |||
| 264 | } | 258 | } |
| 265 | response.Maps = filteredMaps | 259 | response.Maps = filteredMaps |
| 266 | if len(response.Maps) == 0 { | 260 | if len(response.Maps) == 0 { |
| 267 | response.Maps = []struct { | 261 | response.Maps = []models.MapShort{} |
| 268 | ID int "json:\"id\"" | ||
| 269 | Name string "json:\"name\"" | ||
| 270 | }{} | ||
| 271 | } | 262 | } |
| 272 | rows, err := database.DB.Query("SELECT steam_id, user_name FROM users WHERE lower(user_name) LIKE $1", "%"+query+"%") | 263 | rows, err := database.DB.Query("SELECT steam_id, user_name FROM users WHERE lower(user_name) LIKE $1", "%"+query+"%") |
| 273 | if err != nil { | 264 | if err != nil { |
| @@ -275,10 +266,7 @@ func SearchWithQuery(c *gin.Context) { | |||
| 275 | } | 266 | } |
| 276 | defer rows.Close() | 267 | defer rows.Close() |
| 277 | for rows.Next() { | 268 | for rows.Next() { |
| 278 | var user struct { | 269 | var user models.UserShort |
| 279 | SteamID string `json:"steam_id"` | ||
| 280 | UserName string `json:"user_name"` | ||
| 281 | } | ||
| 282 | if err := rows.Scan(&user.SteamID, &user.UserName); err != nil { | 270 | if err := rows.Scan(&user.SteamID, &user.UserName); err != nil { |
| 283 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | 271 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) |
| 284 | return | 272 | return |
| @@ -286,10 +274,7 @@ func SearchWithQuery(c *gin.Context) { | |||
| 286 | response.Players = append(response.Players, user) | 274 | response.Players = append(response.Players, user) |
| 287 | } | 275 | } |
| 288 | if len(response.Players) == 0 { | 276 | if len(response.Players) == 0 { |
| 289 | response.Players = []struct { | 277 | response.Players = []models.UserShort{} |
| 290 | SteamID string "json:\"steam_id\"" | ||
| 291 | UserName string "json:\"user_name\"" | ||
| 292 | }{} | ||
| 293 | } | 278 | } |
| 294 | c.JSON(http.StatusOK, models.Response{ | 279 | c.JSON(http.StatusOK, models.Response{ |
| 295 | Success: true, | 280 | Success: true, |
diff --git a/backend/controllers/userController.go b/backend/controllers/userController.go index adf936b..cbce0fe 100644 --- a/backend/controllers/userController.go +++ b/backend/controllers/userController.go | |||
| @@ -251,7 +251,7 @@ func UpdateUser(c *gin.Context) { | |||
| 251 | // @Produce json | 251 | // @Produce json |
| 252 | // @Param Authorization header string true "JWT Token" | 252 | // @Param Authorization header string true "JWT Token" |
| 253 | // @Param country_code query string true "Country Code [XX]" | 253 | // @Param country_code query string true "Country Code [XX]" |
| 254 | // @Success 200 {object} models.Response{data=models.ProfileResponse} | 254 | // @Success 200 {object} models.Response |
| 255 | // @Failure 400 {object} models.Response | 255 | // @Failure 400 {object} models.Response |
| 256 | // @Failure 401 {object} models.Response | 256 | // @Failure 401 {object} models.Response |
| 257 | // @Router /profile [put] | 257 | // @Router /profile [put] |
diff --git a/backend/models/models.go b/backend/models/models.go index 6f5173a..49a4f82 100644 --- a/backend/models/models.go +++ b/backend/models/models.go | |||
| @@ -4,16 +4,6 @@ import ( | |||
| 4 | "time" | 4 | "time" |
| 5 | ) | 5 | ) |
| 6 | 6 | ||
| 7 | type Response struct { | ||
| 8 | Success bool `json:"success"` | ||
| 9 | Message string `json:"message"` | ||
| 10 | Data any `json:"data"` | ||
| 11 | } | ||
| 12 | |||
| 13 | type LoginResponse struct { | ||
| 14 | Token string `json:"token"` | ||
| 15 | } | ||
| 16 | |||
| 17 | type User struct { | 7 | type User struct { |
| 18 | SteamID string `json:"steam_id"` | 8 | SteamID string `json:"steam_id"` |
| 19 | UserName string `json:"user_name"` | 9 | UserName string `json:"user_name"` |
| @@ -23,6 +13,11 @@ type User struct { | |||
| 23 | UpdatedAt time.Time `json:"updated_at"` | 13 | UpdatedAt time.Time `json:"updated_at"` |
| 24 | } | 14 | } |
| 25 | 15 | ||
| 16 | type UserShort struct { | ||
| 17 | SteamID string `json:"steam_id"` | ||
| 18 | UserName string `json:"user_name"` | ||
| 19 | } | ||
| 20 | |||
| 26 | type Map struct { | 21 | type Map struct { |
| 27 | ID int `json:"id"` | 22 | ID int `json:"id"` |
| 28 | GameName string `json:"game_name"` | 23 | GameName string `json:"game_name"` |
| @@ -31,6 +26,11 @@ type Map struct { | |||
| 31 | Data any `json:"data"` | 26 | Data any `json:"data"` |
| 32 | } | 27 | } |
| 33 | 28 | ||
| 29 | type MapShort struct { | ||
| 30 | ID int `json:"id"` | ||
| 31 | Name string `json:"name"` | ||
| 32 | } | ||
| 33 | |||
| 34 | type MapSummary struct { | 34 | type MapSummary struct { |
| 35 | Description string `json:"description"` | 35 | Description string `json:"description"` |
| 36 | Showcase string `json:"showcase"` | 36 | Showcase string `json:"showcase"` |
| @@ -57,6 +57,22 @@ type MapHistory struct { | |||
| 57 | Date time.Time `json:"date"` | 57 | Date time.Time `json:"date"` |
| 58 | } | 58 | } |
| 59 | 59 | ||
| 60 | type UserRanking struct { | ||
| 61 | UserID string `json:"user_id"` | ||
| 62 | UserName string `json:"user_name"` | ||
| 63 | TotalScore int `json:"total_score"` | ||
| 64 | } | ||
| 65 | |||
| 66 | type Game struct { | ||
| 67 | ID int `json:"id"` | ||
| 68 | Name string `json:"name"` | ||
| 69 | } | ||
| 70 | |||
| 71 | type Chapter struct { | ||
| 72 | ID int `json:"id"` | ||
| 73 | Name string `json:"name"` | ||
| 74 | } | ||
| 75 | |||
| 60 | type RecordSP struct { | 76 | type RecordSP struct { |
| 61 | RecordID int `json:"record_id"` | 77 | RecordID int `json:"record_id"` |
| 62 | Placement int `json:"placement"` | 78 | Placement int `json:"placement"` |
| @@ -85,50 +101,6 @@ type RecordMP struct { | |||
| 85 | RecordDate time.Time `json:"record_date"` | 101 | RecordDate time.Time `json:"record_date"` |
| 86 | } | 102 | } |
| 87 | 103 | ||
| 88 | type RecordRequest struct { | ||
| 89 | ScoreCount int `json:"score_count" form:"score_count" binding:"required"` | ||
| 90 | ScoreTime int `json:"score_time" form:"score_time" binding:"required"` | ||
| 91 | PartnerID string `json:"partner_id" form:"partner_id" binding:"required"` | ||
| 92 | IsPartnerOrange bool `json:"is_partner_orange" form:"is_partner_orange" binding:"required"` | ||
| 93 | } | ||
| 94 | |||
| 95 | type UserRanking struct { | ||
| 96 | UserID string `json:"user_id"` | ||
| 97 | UserName string `json:"user_name"` | ||
| 98 | TotalScore int `json:"total_score"` | ||
| 99 | } | ||
| 100 | |||
| 101 | type RankingsResponse struct { | ||
| 102 | RankingsSP []UserRanking `json:"rankings_sp"` | ||
| 103 | RankingsMP []UserRanking `json:"rankings_mp"` | ||
| 104 | } | ||
| 105 | |||
| 106 | type ProfileResponse struct { | ||
| 107 | Profile bool `json:"profile"` | ||
| 108 | SteamID string `json:"steam_id"` | ||
| 109 | UserName string `json:"user_name"` | ||
| 110 | AvatarLink string `json:"avatar_link"` | ||
| 111 | CountryCode string `json:"country_code"` | ||
| 112 | ScoresSP []ScoreResponse `json:"scores_sp"` | ||
| 113 | ScoresMP []ScoreResponse `json:"scores_mp"` | ||
| 114 | } | ||
| 115 | |||
| 116 | type ScoreResponse struct { | ||
| 117 | MapID int `json:"map_id"` | ||
| 118 | Records any `json:"records"` | ||
| 119 | } | ||
| 120 | |||
| 121 | type SearchResponse struct { | ||
| 122 | Players []struct { | ||
| 123 | SteamID string `json:"steam_id"` | ||
| 124 | UserName string `json:"user_name"` | ||
| 125 | } `json:"players"` | ||
| 126 | Maps []struct { | ||
| 127 | ID int `json:"id"` | ||
| 128 | Name string `json:"name"` | ||
| 129 | } `json:"maps"` | ||
| 130 | } | ||
| 131 | |||
| 132 | type PlayerSummaries struct { | 104 | type PlayerSummaries struct { |
| 133 | SteamId string `json:"steamid"` | 105 | SteamId string `json:"steamid"` |
| 134 | CommunityVisibilityState int `json:"communityvisibilitystate"` | 106 | CommunityVisibilityState int `json:"communityvisibilitystate"` |
| @@ -152,36 +124,3 @@ type PlayerSummaries struct { | |||
| 152 | GameExtraInfo string `json:"gameextrainfo"` | 124 | GameExtraInfo string `json:"gameextrainfo"` |
| 153 | GameServerIp string `json:"gameserverip"` | 125 | GameServerIp string `json:"gameserverip"` |
| 154 | } | 126 | } |
| 155 | |||
| 156 | type Game struct { | ||
| 157 | ID int `json:"id"` | ||
| 158 | Name string `json:"name"` | ||
| 159 | } | ||
| 160 | |||
| 161 | type ChaptersResponse struct { | ||
| 162 | Game Game `json:"game"` | ||
| 163 | Chapters []Chapter `json:"chapters"` | ||
| 164 | } | ||
| 165 | |||
| 166 | type Chapter struct { | ||
| 167 | ID int `json:"id"` | ||
| 168 | Name string `json:"name"` | ||
| 169 | } | ||
| 170 | |||
| 171 | type MapShort struct { | ||
| 172 | ID int `json:"id"` | ||
| 173 | Name string `json:"name"` | ||
| 174 | } | ||
| 175 | |||
| 176 | type ChapterMapsResponse struct { | ||
| 177 | Chapter Chapter `json:"chapter"` | ||
| 178 | Maps []MapShort `json:"maps"` | ||
| 179 | } | ||
| 180 | |||
| 181 | func ErrorResponse(message string) Response { | ||
| 182 | return Response{ | ||
| 183 | Success: false, | ||
| 184 | Message: message, | ||
| 185 | Data: nil, | ||
| 186 | } | ||
| 187 | } | ||
diff --git a/backend/models/requests.go b/backend/models/requests.go new file mode 100644 index 0000000..49b2d75 --- /dev/null +++ b/backend/models/requests.go | |||
| @@ -0,0 +1,8 @@ | |||
| 1 | package models | ||
| 2 | |||
| 3 | type RecordRequest struct { | ||
| 4 | ScoreCount int `json:"score_count" form:"score_count" binding:"required"` | ||
| 5 | ScoreTime int `json:"score_time" form:"score_time" binding:"required"` | ||
| 6 | PartnerID string `json:"partner_id" form:"partner_id" binding:"required"` | ||
| 7 | IsPartnerOrange bool `json:"is_partner_orange" form:"is_partner_orange" binding:"required"` | ||
| 8 | } | ||
diff --git a/backend/models/responses.go b/backend/models/responses.go new file mode 100644 index 0000000..5a88353 --- /dev/null +++ b/backend/models/responses.go | |||
| @@ -0,0 +1,54 @@ | |||
| 1 | package models | ||
| 2 | |||
| 3 | type Response struct { | ||
| 4 | Success bool `json:"success"` | ||
| 5 | Message string `json:"message"` | ||
| 6 | Data any `json:"data"` | ||
| 7 | } | ||
| 8 | |||
| 9 | type LoginResponse struct { | ||
| 10 | Token string `json:"token"` | ||
| 11 | } | ||
| 12 | |||
| 13 | type RankingsResponse struct { | ||
| 14 | RankingsSP []UserRanking `json:"rankings_sp"` | ||
| 15 | RankingsMP []UserRanking `json:"rankings_mp"` | ||
| 16 | } | ||
| 17 | |||
| 18 | type ProfileResponse struct { | ||
| 19 | Profile bool `json:"profile"` | ||
| 20 | SteamID string `json:"steam_id"` | ||
| 21 | UserName string `json:"user_name"` | ||
| 22 | AvatarLink string `json:"avatar_link"` | ||
| 23 | CountryCode string `json:"country_code"` | ||
| 24 | ScoresSP []ScoreResponse `json:"scores_sp"` | ||
| 25 | ScoresMP []ScoreResponse `json:"scores_mp"` | ||
| 26 | } | ||
| 27 | |||
| 28 | type ScoreResponse struct { | ||
| 29 | MapID int `json:"map_id"` | ||
| 30 | Records any `json:"records"` | ||
| 31 | } | ||
| 32 | |||
| 33 | type SearchResponse struct { | ||
| 34 | Players []UserShort `json:"players"` | ||
| 35 | Maps []MapShort `json:"maps"` | ||
| 36 | } | ||
| 37 | |||
| 38 | type ChaptersResponse struct { | ||
| 39 | Game Game `json:"game"` | ||
| 40 | Chapters []Chapter `json:"chapters"` | ||
| 41 | } | ||
| 42 | |||
| 43 | type ChapterMapsResponse struct { | ||
| 44 | Chapter Chapter `json:"chapter"` | ||
| 45 | Maps []MapShort `json:"maps"` | ||
| 46 | } | ||
| 47 | |||
| 48 | func ErrorResponse(message string) Response { | ||
| 49 | return Response{ | ||
| 50 | Success: false, | ||
| 51 | Message: message, | ||
| 52 | Data: nil, | ||
| 53 | } | ||
| 54 | } | ||