Skip to content

Commit

Permalink
Merge pull request #35 from antony-ramos/add-strikes-search-player
Browse files Browse the repository at this point in the history
feat(backend): add strikes to playerSearch
  • Loading branch information
antony-ramos authored Sep 24, 2023
2 parents 5becdae + d1aa52f commit a04fad9
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .codacy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
exclude_paths:
- "*.md"
2 changes: 1 addition & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
- [ ] Add tracing for each request
- [ ] Add API as controller
- [ ] Fix In UpdatePlayer: placeholder $1 already has type int, cannot assign varchar
- [ ] Fix in SearchPlayer: player strikes must be import there not in usecase
- [ ] Add notes to players (for example: player cannot play on wednesday)

### In Progress
Expand All @@ -23,3 +22,4 @@
- [X] Fix DeleteStrike always return success
- [X] Add In CreatePlayer id to player entity
- [X] Add Usecase: player name is not discord name. Must implement a way to link them
- [X] Fix in SearchPlayer: player strikes must be import there not in usecase
23 changes: 23 additions & 0 deletions internal/usecase/postgresbackend/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,29 @@ func (pg *PG) SearchPlayer(ctx context.Context, playerID int, name, discordName

player.MissedRaids = append(player.MissedRaids, raid)
}

// populate player.Strikes list
sql, _, err = pg.Builder.
Select("id", "season", "reason", "created_at").
From("strikes").
Where("player_id = $1").ToSql()
if err != nil {
return entity.Player{}, fmt.Errorf("database - SearchPlayer - playerRows.Builder.Select: %w", err)
}
strikesRows, err := pg.Pool.Query(ctx, sql, strconv.FormatInt(int64(player.ID), 10))
if err != nil {
return entity.Player{}, fmt.Errorf("database - SearchPlayer - playerRows.Pool.Query: %w", err)
}
defer strikesRows.Close()
for strikesRows.Next() {
strike := entity.Strike{}
err := strikesRows.Scan(&strike.ID, &strike.Season, &strike.Reason, &strike.Date)
if err != nil {
return entity.Player{}, fmt.Errorf("database - SearchPlayer - rows.Scan: %w", err)
}
player.Strikes = append(player.Strikes, strike)
}

return player, nil
}()
if err != nil {
Expand Down
36 changes: 35 additions & 1 deletion internal/usecase/postgresbackend/player_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"strconv"
"testing"
"time"

"github.com/jackc/pgconn"

Expand Down Expand Up @@ -71,6 +72,12 @@ func TestPG_CreatePlayer(t *testing.T) {
"WHERE absences.player_id = $1", "1").
Return(pgxRows, nil)

columnsStrike := []string{"id", "season", "reason", "created_at"}
pgxRowsStrike := pgxpoolmock.NewRows(columnsStrike).ToPgxRows()
mockPool.EXPECT().Query(gomock.Any(),
"SELECT id, season, reason, created_at FROM strikes WHERE player_id = $1", strconv.Itoa(player.ID)).
Return(pgxRowsStrike, nil)

p, err := pgBackend.CreatePlayer(context.Background(), player)
assert.NoError(t, err)
assert.Equal(t, player, p)
Expand Down Expand Up @@ -267,7 +274,7 @@ func TestPG_ReadPlayer(t *testing.T) {
})
}

//nolint:dupl,maintidx
//nolint:maintidx
func TestPG_SearchPlayer(t *testing.T) {
t.Parallel()
t.Run("Success with playerID", func(t *testing.T) {
Expand Down Expand Up @@ -318,6 +325,12 @@ func TestPG_SearchPlayer(t *testing.T) {
"WHERE absences.player_id = $1", "1").
Return(pgxRows, nil)

columnsStrike := []string{"id", "season", "reason", "created_at"}
pgxRowsStrike := pgxpoolmock.NewRows(columnsStrike).ToPgxRows()
mockPool.EXPECT().Query(gomock.Any(),
"SELECT id, season, reason, created_at FROM strikes WHERE player_id = $1", strconv.Itoa(player.ID)).
Return(pgxRowsStrike, nil)

p, err := pgBackend.SearchPlayer(context.Background(), playerID, name, discordName)
assert.NoError(t, err)
assert.Equal(t, player, p[0])
Expand Down Expand Up @@ -397,6 +410,12 @@ func TestPG_SearchPlayer(t *testing.T) {
"WHERE absences.player_id = $1", "1").
Return(pgxRows, nil)

columnsStrike := []string{"id", "season", "reason", "created_at"}
pgxRowsStrike := pgxpoolmock.NewRows(columnsStrike).ToPgxRows()
mockPool.EXPECT().Query(gomock.Any(),
"SELECT id, season, reason, created_at FROM strikes WHERE player_id = $1", strconv.Itoa(player.ID)).
Return(pgxRowsStrike, nil)

p, err := pgBackend.SearchPlayer(context.Background(), playerID, name, discordName)
assert.NoError(t, err)
assert.Equal(t, player, p[0])
Expand Down Expand Up @@ -476,7 +495,22 @@ func TestPG_SearchPlayer(t *testing.T) {
"WHERE absences.player_id = $1", "1").
Return(pgxRows, nil)

strike := entity.Strike{
ID: 1,
Season: "DF/S2",
Reason: "reason",
Date: time.Now(),
}
columnsStrike := []string{"id", "season", "reason", "created_at"}
pgxRowsStrike := pgxpoolmock.NewRows(columnsStrike).
AddRow(strike.ID, strike.Season, strike.Reason, strike.Date).ToPgxRows()
mockPool.EXPECT().Query(gomock.Any(),
"SELECT id, season, reason, created_at FROM strikes WHERE player_id = $1", strconv.Itoa(player.ID)).
Return(pgxRowsStrike, nil)

p, err := pgBackend.SearchPlayer(context.Background(), playerID, name, discordName)
player.Strikes = append(player.Strikes, strike)

assert.NoError(t, err)
assert.Equal(t, player, p[0])
})
Expand Down

0 comments on commit a04fad9

Please sign in to comment.