Skip to content

Commit

Permalink
add search to players get and count
Browse files Browse the repository at this point in the history
  • Loading branch information
codephobia committed Mar 9, 2024
1 parent a6a3ffb commit ea4f84f
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ export class PlayersListPageComponent {
this.router.navigate(['.'], {
relativeTo: this.activatedRoute,
queryParams: { search: newSearch },
queryParamsHandling: 'merge',
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export class PlayersListStore extends ComponentStore<PlayersListState> {
public readonly getPlayers = this.effect<PlayerFindOptions>(options$ => options$.pipe(
switchMap(({ page, search }) => forkJoin([
this.playersService.find({ page, search }),
this.playersService.count(),
this.playersService.count({ search }),
]).pipe(
tapResponse(
([players, { count }]) => {
Expand Down
13 changes: 11 additions & 2 deletions apps/dashboard/src/app/shared/services/players.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ export interface PlayerFindOptions {
search?: string;
}

export interface PlayerCountOptions {
search?: string;
}

@Injectable()
export class PlayersService {
private apiURL: string;
Expand Down Expand Up @@ -40,8 +44,13 @@ export class PlayersService {
return this.http.get<IPlayer>(url);
}

public count(): Observable<ICount> {
const url = `${this.apiURL}/${this.apiVersion}/${this.endpoint}/count`;
public count({ search }: PlayerCountOptions = {}): Observable<ICount> {
let url = `${this.apiURL}/${this.apiVersion}/${this.endpoint}/count`;

if (search) {
url = url + `?search=${search}`;
}

return this.http.get<ICount>(url);
}

Expand Down
35 changes: 31 additions & 4 deletions libs/go/api/players.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -70,17 +73,25 @@ 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
}

// 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
}
Expand All @@ -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).
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ea4f84f

Please sign in to comment.