diff options
| author | Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> | 2023-06-17 12:30:25 +0300 |
|---|---|---|
| committer | Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> | 2023-06-17 12:30:25 +0300 |
| commit | 1f47d78a5f82998fe62fefe6d6f076583c101800 (patch) | |
| tree | f20dafaeccc264ce6b05c1b599e9cf31286c22f3 | |
| parent | feat: update main (diff) | |
| download | lphub-1f47d78a5f82998fe62fefe6d6f076583c101800.tar.gz lphub-1f47d78a5f82998fe62fefe6d6f076583c101800.tar.bz2 lphub-1f47d78a5f82998fe62fefe6d6f076583c101800.zip | |
feat: search with query (#40)
| -rw-r--r-- | backend/controllers/homeController.go | 36 | ||||
| -rw-r--r-- | backend/routes/routes.go | 2 | ||||
| -rw-r--r-- | docs/docs.go | 10 | ||||
| -rw-r--r-- | docs/swagger.json | 10 | ||||
| -rw-r--r-- | docs/swagger.yaml | 7 |
5 files changed, 56 insertions, 9 deletions
diff --git a/backend/controllers/homeController.go b/backend/controllers/homeController.go index edb770f..d30c835 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" |
| @@ -122,15 +123,19 @@ 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 | // @Summary Get all user and map data matching to the query. |
| 128 | // @Tags search | 129 | // @Tags search |
| 129 | // @Produce json | 130 | // @Produce json |
| 131 | // @Param q query string false "Search user or map name." | ||
| 130 | // @Success 200 {object} models.Response{data=models.SearchResponse} | 132 | // @Success 200 {object} models.Response{data=models.SearchResponse} |
| 131 | // @Failure 400 {object} models.Response | 133 | // @Failure 400 {object} models.Response |
| 132 | // @Router /search [get] | 134 | // @Router /search [get] |
| 133 | func Search(c *gin.Context) { | 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 = []struct { |
| @@ -248,8 +253,23 @@ func Search(c *gin.Context) { | |||
| 248 | {ID: 109, Name: "Gel Maze"}, | 253 | {ID: 109, Name: "Gel Maze"}, |
| 249 | {ID: 110, Name: "Crazier Box"}, | 254 | {ID: 110, Name: "Crazier Box"}, |
| 250 | } | 255 | } |
| 251 | response.Maps = maps | 256 | var filteredMaps []struct { |
| 252 | rows, err := database.DB.Query("SELECT steam_id, user_name FROM users") //WHERE player_name LIKE ?", "%"+query+"%") | 257 | ID int `json:"id"` |
| 258 | Name string `json:"name"` | ||
| 259 | } | ||
| 260 | for _, m := range maps { | ||
| 261 | if strings.Contains(strings.ToLower(m.Name), strings.ToLower(query)) { | ||
| 262 | filteredMaps = append(filteredMaps, m) | ||
| 263 | } | ||
| 264 | } | ||
| 265 | response.Maps = filteredMaps | ||
| 266 | if len(response.Maps) == 0 { | ||
| 267 | response.Maps = []struct { | ||
| 268 | ID int "json:\"id\"" | ||
| 269 | Name string "json:\"name\"" | ||
| 270 | }{} | ||
| 271 | } | ||
| 272 | rows, err := database.DB.Query("SELECT steam_id, user_name FROM users WHERE lower(user_name) LIKE $1", "%"+query+"%") | ||
| 253 | if err != nil { | 273 | if err != nil { |
| 254 | log.Fatal(err) | 274 | log.Fatal(err) |
| 255 | } | 275 | } |
| @@ -265,6 +285,12 @@ func Search(c *gin.Context) { | |||
| 265 | } | 285 | } |
| 266 | response.Players = append(response.Players, user) | 286 | response.Players = append(response.Players, user) |
| 267 | } | 287 | } |
| 288 | if len(response.Players) == 0 { | ||
| 289 | response.Players = []struct { | ||
| 290 | SteamID string "json:\"steam_id\"" | ||
| 291 | UserName string "json:\"user_name\"" | ||
| 292 | }{} | ||
| 293 | } | ||
| 268 | c.JSON(http.StatusOK, models.Response{ | 294 | c.JSON(http.StatusOK, models.Response{ |
| 269 | Success: true, | 295 | Success: true, |
| 270 | Message: "Search successfully retrieved.", | 296 | Message: "Search successfully retrieved.", |
diff --git a/backend/routes/routes.go b/backend/routes/routes.go index 96da1ce..bf8a995 100644 --- a/backend/routes/routes.go +++ b/backend/routes/routes.go | |||
| @@ -29,7 +29,7 @@ func InitRoutes(router *gin.Engine) { | |||
| 29 | v1.GET("/maps/:id/leaderboards", middleware.CheckAuth, controllers.FetchMapLeaderboards) | 29 | v1.GET("/maps/:id/leaderboards", middleware.CheckAuth, controllers.FetchMapLeaderboards) |
| 30 | v1.POST("/maps/:id/record", middleware.CheckAuth, controllers.CreateRecordWithDemo) | 30 | v1.POST("/maps/:id/record", middleware.CheckAuth, controllers.CreateRecordWithDemo) |
| 31 | v1.GET("/rankings", middleware.CheckAuth, controllers.Rankings) | 31 | v1.GET("/rankings", middleware.CheckAuth, controllers.Rankings) |
| 32 | v1.GET("/search", controllers.Search) | 32 | v1.GET("/search", controllers.SearchWithQuery) |
| 33 | v1.GET("/games", controllers.FetchGames) | 33 | v1.GET("/games", controllers.FetchGames) |
| 34 | v1.GET("/games/:id", controllers.FetchChapters) | 34 | v1.GET("/games/:id", controllers.FetchChapters) |
| 35 | v1.GET("/chapters/:id", controllers.FetchChapterMaps) | 35 | v1.GET("/chapters/:id", controllers.FetchChapterMaps) |
diff --git a/docs/docs.go b/docs/docs.go index d39fd1c..34aa7f4 100644 --- a/docs/docs.go +++ b/docs/docs.go | |||
| @@ -651,7 +651,15 @@ const docTemplate = `{ | |||
| 651 | "tags": [ | 651 | "tags": [ |
| 652 | "search" | 652 | "search" |
| 653 | ], | 653 | ], |
| 654 | "summary": "Get all user and map data.", | 654 | "summary": "Get all user and map data matching to the query.", |
| 655 | "parameters": [ | ||
| 656 | { | ||
| 657 | "type": "string", | ||
| 658 | "description": "Search user or map name.", | ||
| 659 | "name": "q", | ||
| 660 | "in": "query" | ||
| 661 | } | ||
| 662 | ], | ||
| 655 | "responses": { | 663 | "responses": { |
| 656 | "200": { | 664 | "200": { |
| 657 | "description": "OK", | 665 | "description": "OK", |
diff --git a/docs/swagger.json b/docs/swagger.json index ad2a659..8491d2e 100644 --- a/docs/swagger.json +++ b/docs/swagger.json | |||
| @@ -644,7 +644,15 @@ | |||
| 644 | "tags": [ | 644 | "tags": [ |
| 645 | "search" | 645 | "search" |
| 646 | ], | 646 | ], |
| 647 | "summary": "Get all user and map data.", | 647 | "summary": "Get all user and map data matching to the query.", |
| 648 | "parameters": [ | ||
| 649 | { | ||
| 650 | "type": "string", | ||
| 651 | "description": "Search user or map name.", | ||
| 652 | "name": "q", | ||
| 653 | "in": "query" | ||
| 654 | } | ||
| 655 | ], | ||
| 648 | "responses": { | 656 | "responses": { |
| 649 | "200": { | 657 | "200": { |
| 650 | "description": "OK", | 658 | "description": "OK", |
diff --git a/docs/swagger.yaml b/docs/swagger.yaml index d62b46b..597df9f 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml | |||
| @@ -574,6 +574,11 @@ paths: | |||
| 574 | - users | 574 | - users |
| 575 | /search: | 575 | /search: |
| 576 | get: | 576 | get: |
| 577 | parameters: | ||
| 578 | - description: Search user or map name. | ||
| 579 | in: query | ||
| 580 | name: q | ||
| 581 | type: string | ||
| 577 | produces: | 582 | produces: |
| 578 | - application/json | 583 | - application/json |
| 579 | responses: | 584 | responses: |
| @@ -590,7 +595,7 @@ paths: | |||
| 590 | description: Bad Request | 595 | description: Bad Request |
| 591 | schema: | 596 | schema: |
| 592 | $ref: '#/definitions/models.Response' | 597 | $ref: '#/definitions/models.Response' |
| 593 | summary: Get all user and map data. | 598 | summary: Get all user and map data matching to the query. |
| 594 | tags: | 599 | tags: |
| 595 | - search | 600 | - search |
| 596 | /token: | 601 | /token: |