diff --git a/apps/dashboard/src/app/players/containers/players-list-page/players-list-page.component.ts b/apps/dashboard/src/app/players/containers/players-list-page/players-list-page.component.ts index 9665141..561aeb0 100644 --- a/apps/dashboard/src/app/players/containers/players-list-page/players-list-page.component.ts +++ b/apps/dashboard/src/app/players/containers/players-list-page/players-list-page.component.ts @@ -35,7 +35,6 @@ export class PlayersListPageComponent { this.router.navigate(['.'], { relativeTo: this.activatedRoute, queryParams: { search: newSearch }, - queryParamsHandling: 'merge', }); } diff --git a/apps/dashboard/src/app/players/containers/players-list-page/players-list.store.ts b/apps/dashboard/src/app/players/containers/players-list-page/players-list.store.ts index 407f7f0..1a00018 100644 --- a/apps/dashboard/src/app/players/containers/players-list-page/players-list.store.ts +++ b/apps/dashboard/src/app/players/containers/players-list-page/players-list.store.ts @@ -92,7 +92,7 @@ export class PlayersListStore extends ComponentStore { public readonly getPlayers = this.effect(options$ => options$.pipe( switchMap(({ page, search }) => forkJoin([ this.playersService.find({ page, search }), - this.playersService.count(), + this.playersService.count({ search }), ]).pipe( tapResponse( ([players, { count }]) => { diff --git a/apps/dashboard/src/app/shared/services/players.service.ts b/apps/dashboard/src/app/shared/services/players.service.ts index bc7f2cb..1a4f4e0 100644 --- a/apps/dashboard/src/app/shared/services/players.service.ts +++ b/apps/dashboard/src/app/shared/services/players.service.ts @@ -11,6 +11,10 @@ export interface PlayerFindOptions { search?: string; } +export interface PlayerCountOptions { + search?: string; +} + @Injectable() export class PlayersService { private apiURL: string; @@ -40,8 +44,13 @@ export class PlayersService { return this.http.get(url); } - public count(): Observable { - const url = `${this.apiURL}/${this.apiVersion}/${this.endpoint}/count`; + public count({ search }: PlayerCountOptions = {}): Observable { + let url = `${this.apiURL}/${this.apiVersion}/${this.endpoint}/count`; + + if (search) { + url = url + `?search=${search}`; + } + return this.http.get(url); } diff --git a/libs/go/api/players.go b/libs/go/api/players.go index fd63f59..d0bd0bb 100644 --- a/libs/go/api/players.go +++ b/libs/go/api/players.go @@ -52,6 +52,9 @@ func (server *Server) handlePlayers() http.Handler { // Players handler for GET method. Returns a page of players. func (server *Server) handlePlayersGet(w http.ResponseWriter, r *http.Request) { + // where clause for find and count + where := make(map[string]interface{}) + // get query vars v := r.URL.Query() @@ -70,9 +73,17 @@ func (server *Server) handlePlayersGet(w http.ResponseWriter, r *http.Request) { return } + // get search + search := v.Get("search") + + // only add search to where if there is a length + if len(search) > 0 { + where["name"] = search + } + // get count of players to test page ceiling var count int64 - countResult := server.db.Model(&models.Player{}).Count(&count) + countResult := server.db.Model(&models.Player{}).Where(where).Count(&count) if countResult.Error != nil { server.handleError(w, r, http.StatusInternalServerError, ErrInternalServerError) return @@ -80,7 +91,7 @@ func (server *Server) handlePlayersGet(w http.ResponseWriter, r *http.Request) { // check if page is beyond maximum totalPages := int(math.Ceil(float64(count) / playersPerPage)) - if pageNum > totalPages { + if pageNum > totalPages && totalPages != 0 { server.handleError(w, r, http.StatusUnprocessableEntity, ErrInvalidPageNumber) return } @@ -91,6 +102,7 @@ func (server *Server) handlePlayersGet(w http.ResponseWriter, r *http.Request) { offset := pageNum*playersPerPage - playersPerPage playersResult := server.db. Select("id", "name", "flag_id", "fargo_observable_id", "fargo_id", "fargo_rating"). + Where(where). Order("name"). Limit(playersPerPage). Offset(offset). @@ -152,9 +164,24 @@ func (server *Server) handlePlayersCount() http.Handler { // Players count handler for GET method. func (server *Server) handlePlayersCountGet(w http.ResponseWriter, r *http.Request) { - // get count of players var count int64 - countResult := server.db.Model(&models.Player{}).Count(&count) + + // where clause for count + where := make(map[string]interface{}) + + // get query vars + v := r.URL.Query() + + // get search + search := v.Get("search") + + // only add search to where if there is a length + if len(search) > 0 { + where["name"] = search + } + + // get count of players + countResult := server.db.Model(&models.Player{}).Where(where).Count(&count) if countResult.Error != nil { server.handleError(w, r, http.StatusInternalServerError, ErrInternalServerError) return