diff options
| author | Nidboj132 <lol2s@vp.plm> | 2023-07-12 17:58:23 +0200 |
|---|---|---|
| committer | Nidboj132 <lol2s@vp.plm> | 2023-07-12 17:58:23 +0200 |
| commit | 781289455037431d8adbaa0b293b755c88169747 (patch) | |
| tree | 773824f97c3b21d353b9066afdbde30bee2da4c5 /backend/controllers/homeController.go | |
| parent | summary (diff) | |
| parent | fix: 0 score count / showcase not required (#47) (diff) | |
| download | lphub-781289455037431d8adbaa0b293b755c88169747.tar.gz lphub-781289455037431d8adbaa0b293b755c88169747.tar.bz2 lphub-781289455037431d8adbaa0b293b755c88169747.zip | |
Merge branch 'main' of https://github.com/pektezol/LeastPortals
Former-commit-id: af8d8680aafc3d662f8b53a4f50f0ea356b26c26
Diffstat (limited to 'backend/controllers/homeController.go')
| -rw-r--r-- | backend/controllers/homeController.go | 59 |
1 files changed, 35 insertions, 24 deletions
diff --git a/backend/controllers/homeController.go b/backend/controllers/homeController.go index edb770f..2780e63 100644 --- a/backend/controllers/homeController.go +++ b/backend/controllers/homeController.go | |||
| @@ -3,6 +3,7 @@ package controllers | |||
| 3 | import ( | 3 | import ( |
| 4 | "log" | 4 | "log" |
| 5 | "net/http" | 5 | "net/http" |
| 6 | "strings" | ||
| 6 | 7 | ||
| 7 | "github.com/gin-gonic/gin" | 8 | "github.com/gin-gonic/gin" |
| 8 | "github.com/pektezol/leastportals/backend/database" | 9 | "github.com/pektezol/leastportals/backend/database" |
| @@ -22,12 +23,12 @@ func Home(c *gin.Context) { | |||
| 22 | 23 | ||
| 23 | // GET Rankings | 24 | // GET Rankings |
| 24 | // | 25 | // |
| 25 | // @Summary Get rankings of every player. | 26 | // @Description Get rankings of every player. |
| 26 | // @Tags rankings | 27 | // @Tags rankings |
| 27 | // @Produce json | 28 | // @Produce json |
| 28 | // @Success 200 {object} models.Response{data=models.RankingsResponse} | 29 | // @Success 200 {object} models.Response{data=models.RankingsResponse} |
| 29 | // @Failure 400 {object} models.Response | 30 | // @Failure 400 {object} models.Response |
| 30 | // @Router /demo [get] | 31 | // @Router /rankings [get] |
| 31 | func Rankings(c *gin.Context) { | 32 | func Rankings(c *gin.Context) { |
| 32 | rows, err := database.DB.Query(`SELECT steam_id, user_name FROM users`) | 33 | rows, err := database.DB.Query(`SELECT steam_id, user_name FROM users`) |
| 33 | if err != nil { | 34 | if err != nil { |
| @@ -122,21 +123,22 @@ func Rankings(c *gin.Context) { | |||
| 122 | }) | 123 | }) |
| 123 | } | 124 | } |
| 124 | 125 | ||
| 125 | // GET Search | 126 | // GET Search With Query |
| 126 | // | 127 | // |
| 127 | // @Summary Get all user and map data. | 128 | // @Description Get all user and map data matching to the query. |
| 128 | // @Tags search | 129 | // @Tags search |
| 129 | // @Produce json | 130 | // @Produce json |
| 130 | // @Success 200 {object} models.Response{data=models.SearchResponse} | 131 | // @Param q query string false "Search user or map name." |
| 131 | // @Failure 400 {object} models.Response | 132 | // @Success 200 {object} models.Response{data=models.SearchResponse} |
| 132 | // @Router /search [get] | 133 | // @Failure 400 {object} models.Response |
| 133 | func Search(c *gin.Context) { | 134 | // @Router /search [get] |
| 135 | func SearchWithQuery(c *gin.Context) { | ||
| 136 | query := c.Query("q") | ||
| 137 | query = strings.ToLower(query) | ||
| 138 | log.Println(query) | ||
| 134 | var response models.SearchResponse | 139 | var response models.SearchResponse |
| 135 | // Cache all maps for faster response | 140 | // Cache all maps for faster response |
| 136 | var maps = []struct { | 141 | var maps = []models.MapShort{ |
| 137 | ID int `json:"id"` | ||
| 138 | Name string `json:"name"` | ||
| 139 | }{ | ||
| 140 | {ID: 1, Name: "Container Ride"}, | 142 | {ID: 1, Name: "Container Ride"}, |
| 141 | {ID: 2, Name: "Portal Carousel"}, | 143 | {ID: 2, Name: "Portal Carousel"}, |
| 142 | {ID: 3, Name: "Portal Gun"}, | 144 | {ID: 3, Name: "Portal Gun"}, |
| @@ -248,23 +250,32 @@ func Search(c *gin.Context) { | |||
| 248 | {ID: 109, Name: "Gel Maze"}, | 250 | {ID: 109, Name: "Gel Maze"}, |
| 249 | {ID: 110, Name: "Crazier Box"}, | 251 | {ID: 110, Name: "Crazier Box"}, |
| 250 | } | 252 | } |
| 251 | response.Maps = maps | 253 | var filteredMaps []models.MapShort |
| 252 | rows, err := database.DB.Query("SELECT steam_id, user_name FROM users") //WHERE player_name LIKE ?", "%"+query+"%") | 254 | for _, m := range maps { |
| 255 | if strings.Contains(strings.ToLower(m.Name), strings.ToLower(query)) { | ||
| 256 | filteredMaps = append(filteredMaps, m) | ||
| 257 | } | ||
| 258 | } | ||
| 259 | response.Maps = filteredMaps | ||
| 260 | if len(response.Maps) == 0 { | ||
| 261 | response.Maps = []models.MapShort{} | ||
| 262 | } | ||
| 263 | rows, err := database.DB.Query("SELECT steam_id, user_name FROM users WHERE lower(user_name) LIKE $1", "%"+query+"%") | ||
| 253 | if err != nil { | 264 | if err != nil { |
| 254 | log.Fatal(err) | 265 | log.Fatal(err) |
| 255 | } | 266 | } |
| 256 | defer rows.Close() | 267 | defer rows.Close() |
| 257 | for rows.Next() { | 268 | for rows.Next() { |
| 258 | var user struct { | 269 | var user models.UserShort |
| 259 | SteamID string `json:"steam_id"` | ||
| 260 | UserName string `json:"user_name"` | ||
| 261 | } | ||
| 262 | if err := rows.Scan(&user.SteamID, &user.UserName); err != nil { | 270 | if err := rows.Scan(&user.SteamID, &user.UserName); err != nil { |
| 263 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | 271 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) |
| 264 | return | 272 | return |
| 265 | } | 273 | } |
| 266 | response.Players = append(response.Players, user) | 274 | response.Players = append(response.Players, user) |
| 267 | } | 275 | } |
| 276 | if len(response.Players) == 0 { | ||
| 277 | response.Players = []models.UserShort{} | ||
| 278 | } | ||
| 268 | c.JSON(http.StatusOK, models.Response{ | 279 | c.JSON(http.StatusOK, models.Response{ |
| 269 | Success: true, | 280 | Success: true, |
| 270 | Message: "Search successfully retrieved.", | 281 | Message: "Search successfully retrieved.", |