diff --git a/backend/main.go b/backend/main.go index 618a15e..2e23296 100644 --- a/backend/main.go +++ b/backend/main.go @@ -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{ diff --git a/backend/playerService.go b/backend/playerService.go index 02913a0..b9d9e8b 100644 --- a/backend/playerService.go +++ b/backend/playerService.go @@ -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 +} diff --git a/backend/selectPlayers.sql b/backend/selectPlayers.sql new file mode 100644 index 0000000..e9649f0 --- /dev/null +++ b/backend/selectPlayers.sql @@ -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 \ No newline at end of file diff --git a/backend/types.go b/backend/types.go index 84debbb..c91f401 100644 --- a/backend/types.go +++ b/backend/types.go @@ -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"` diff --git a/frontend/src/types.ts b/frontend/src/types.ts index 787701c..f1479a5 100644 --- a/frontend/src/types.ts +++ b/frontend/src/types.ts @@ -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 = { success: boolean; message: string; data: T; -} \ No newline at end of file +};