Return more data per player on listing all players

This commit is contained in:
2024-10-30 15:47:33 +01:00
parent 460503ba4a
commit 385b22d4e4
5 changed files with 63 additions and 6 deletions

View File

@@ -40,6 +40,9 @@ var as AssociationService
//go:embed selectPlayer.sql
var selectPlayer string
//go:embed selectPlayers.sql
var selectPlayers string
//go:embed selectAssociation.sql
var selectAssociation string
@@ -122,7 +125,7 @@ func GetPlayer(c fiber.Ctx) error {
}
func GetPlayers(c fiber.Ctx) error {
players, err := ps.Query(PlayerServiceQuery{})
players, err := ps.GetAllPlayers()
if err != nil {
Error.Printf("Failed getting players: %v", err)
return c.Status(500).JSON(Response{

View File

@@ -29,7 +29,7 @@ func (ps *PlayerService) Query(query PlayerServiceQuery) ([]Player, error) {
for rows.Next() {
player := Player{}
err := rows.Scan(&player.ID, &player.Name, &player.Guild.ID)
err := rows.Scan(&player.ID, &player.Name, &player.Guild.ID, &player.Notes, &player.Associations)
if err != nil {
return res, fmt.Errorf("failed scanning player: %v", err)
}
@@ -147,3 +147,24 @@ func (ps *PlayerService) GetAllPlayerInfo(name string, nnotes int) (FullPlayer,
return res, nil
}
func (ps *PlayerService) GetAllPlayers() ([]Player, error) {
res := []Player{}
rows, err := ps.db.readConn.Query(selectPlayers)
if err != nil {
return res, fmt.Errorf("failed getting players: %v", err)
}
for rows.Next() {
player := Player{}
err := rows.Scan(&player.ID, &player.Name, &player.Guild.ID, &player.Guild.Name, &player.Notes, &player.Associations)
if err != nil {
return res, fmt.Errorf("failed scanning player: %v", err)
}
res = append(res, player)
}
rows.Close()
return res, nil
}

20
backend/selectPlayers.sql Normal file
View File

@@ -0,0 +1,20 @@
select
p.id,
p.name,
p.guild,
g.name,
count(distinct n.player) as nnotes,
count(
distinct case
when a.lhs = p.id
or a.rhs = p.id then a.id
end
) as nassoc
from
player p
left join note n on n.player = p.id
left join association a on a.lhs = p.id
or a.rhs = p.id
join guild g on p.guild = g.id
group by
p.id

View File

@@ -8,9 +8,11 @@ type (
Name string `json:"name"`
}
Player struct {
ID int64 `json:"id"`
Name string `json:"name"`
Guild Guild `json:"guild,omitempty"`
ID int64 `json:"id"`
Name string `json:"name"`
Guild Guild `json:"guild,omitempty"`
Notes int `json:"notes"`
Associations int `json:"associations"`
}
Association struct {
ID int64 `json:"id"`

View File

@@ -1,4 +1,15 @@
export type Player = {
id: number;
name: string;
guild: {
id: number;
name: string;
};
associations: number;
notes: number;
};
export type PlayerFull = {
id: number;
name: string;
guild: {
@@ -26,4 +37,4 @@ export type APIResponse<T> = {
success: boolean;
message: string;
data: T;
}
};