From 985cd22213f054de337c4b6a791fccc18e1334cc Mon Sep 17 00:00:00 2001 From: Dan Whitacre Date: Sat, 18 May 2024 21:03:02 -0400 Subject: [PATCH] add weekly and weekly match to db --- api/.dockerignore | 1 - api/Dockerfile | 1 - api/domain/weekly.go | 75 +++++++++++++++++++++++++++++++--------- api/weekly/2024-04-27 | 51 --------------------------- api/weekly/2024-05-04 | 27 --------------- api/weekly/2024-05-11 | 27 --------------- api/weekly/2024-05-17 | 27 --------------- db/005_create_weekly.sql | 16 +++++++++ db/006_seed_weekly.sql | 54 +++++++++++++++++++++++++++++ 9 files changed, 129 insertions(+), 150 deletions(-) delete mode 100644 api/weekly/2024-04-27 delete mode 100644 api/weekly/2024-05-04 delete mode 100644 api/weekly/2024-05-11 delete mode 100644 api/weekly/2024-05-17 create mode 100644 db/005_create_weekly.sql create mode 100644 db/006_seed_weekly.sql diff --git a/api/.dockerignore b/api/.dockerignore index c7e5c80..7cba30e 100644 --- a/api/.dockerignore +++ b/api/.dockerignore @@ -3,7 +3,6 @@ !leaderboard !player -!weekly !go.mod !go.sum !*.go diff --git a/api/Dockerfile b/api/Dockerfile index 892293f..907b028 100644 --- a/api/Dockerfile +++ b/api/Dockerfile @@ -16,7 +16,6 @@ WORKDIR / COPY --from=build /hdstmevents-api /hdstmevents-api COPY ./leaderboard /leaderboard COPY ./player /player -COPY ./weekly /weekly EXPOSE 8080 USER nonroot:nonroot diff --git a/api/domain/weekly.go b/api/domain/weekly.go index f18fe5c..584b99e 100644 --- a/api/domain/weekly.go +++ b/api/domain/weekly.go @@ -1,10 +1,11 @@ package domain import ( - "encoding/json" + "context" "errors" - "os" "slices" + + "github.com/jackc/pgx/v5" ) type Weekly struct { @@ -22,43 +23,85 @@ type WeeklyResult struct { Score int `json:"score"` } -func WeeklyGet(weekly *Weekly) error { - if weekly.WeeklyId == "" { - return errors.New("WeeklyGet: missing weekly id, nothing to gets") +type WeeklyData struct { + WeeklyId string + MatchId string +} + +func getWeeklyData(weeklyId string) ([]WeeklyData, error) { + var weeklyData []WeeklyData + + rows, err := db.Query( + context.Background(), + `select w.WeeklyId, wm.MatchId + from Weekly w + join WeeklyMatch wm on w.WeeklyId = wm.WeeklyId + where w.WeeklyId=$1`, + weeklyId, + ) + if err != nil { + return weeklyData, err } - file, err := os.Open("weekly/" + weekly.WeeklyId) - + weeklyData, err = pgx.CollectRows(rows, pgx.RowToStructByName[WeeklyData]) if err != nil { - return err + return weeklyData, err } - defer file.Close() - jsonParser := json.NewDecoder(file) - if err = jsonParser.Decode(weekly); err != nil { - return err + return weeklyData, nil +} + +func toWeekly(weeklyData []WeeklyData, weekly *Weekly) error { + if len(weeklyData) < 1 { + return errors.New("WeeklyGet: no weeklyData to create weekly from") } - for i := 0; i < len(weekly.Matches); i++ { - err = MatchGet(weekly.Matches[i].Match) + weekly.WeeklyId = weeklyData[0].WeeklyId + + for i := 0; i < len(weeklyData); i++ { + var match Match + match.MatchId = weeklyData[i].MatchId + err := MatchGet(&match) if err != nil { return err } + var weeklyMatch WeeklyMatch + weeklyMatch.Match = &match + weekly.Matches = append(weekly.Matches, &weeklyMatch) + for j := 0; j < len(weekly.Matches[i].Match.PointsResults); j++ { idx := slices.IndexFunc(weekly.Results, func (weeklyResult *WeeklyResult) bool { return weeklyResult.Player.AccountId == weekly.Matches[i].Match.PointsResults[j].Player.AccountId }) if idx == -1 { - weeklyResult := &WeeklyResult{} + var weeklyResult WeeklyResult weeklyResult.Player = weekly.Matches[i].Match.PointsResults[j].Player idx = len(weekly.Results) - weekly.Results = append(weekly.Results, weeklyResult) + weekly.Results = append(weekly.Results, &weeklyResult) } weekly.Results[idx].Score += weekly.Matches[i].Match.PointsResults[j].Score } } + return nil +} + +func WeeklyGet(weekly *Weekly) error { + if weekly.WeeklyId == "" { + return errors.New("WeeklyGet: missing weekly id, nothing to gets") + } + + weeklyData, err := getWeeklyData(weekly.WeeklyId) + if err != nil { + return err + } + + err = toWeekly(weeklyData, weekly) + if err != nil { + return err + } + slices.SortFunc(weekly.Results, func (weeklyResultA *WeeklyResult, weeklyResultB *WeeklyResult) int { return weeklyResultB.Score - weeklyResultA.Score }) diff --git a/api/weekly/2024-04-27 b/api/weekly/2024-04-27 deleted file mode 100644 index 8e3dfc6..0000000 --- a/api/weekly/2024-04-27 +++ /dev/null @@ -1,51 +0,0 @@ -{ - "weeklyId": "2024-04-27", - "matches": [{ - "match": { "matchId": "2024-04-27-finals" } - }, - { - "match": { "matchId": "2024-04-27-firstround-a" } - }, - { - "match": { "matchId": "2024-04-27-firstround-b" } - }, - { - "match": { "matchId": "2024-04-27-firstround-c" } - }, - { - "match": { "matchId": "2024-04-27-firstround-d" } - }, - { - "match": { "matchId": "2024-04-27-firstround-e" } - }, - { - "match": { "matchId": "2024-04-27-firstround-f" } - }, - { - "match": { "matchId": "2024-04-27-firstround-g" } - }, - { - "match": { "matchId": "2024-04-27-firstround-h" } - }, - { - "match": { "matchId": "2024-04-27-qualifying" } - }, - { - "match": { "matchId": "2024-04-27-quarterfinal-i" } - }, - { - "match": { "matchId": "2024-04-27-quarterfinal-j" } - }, - { - "match": { "matchId": "2024-04-27-quarterfinal-k" } - }, - { - "match": { "matchId": "2024-04-27-quarterfinal-l" } - }, - { - "match": { "matchId": "2024-04-27-semifinal-m" } - }, - { - "match": { "matchId": "2024-04-27-semifinal-n" } - }] -} \ No newline at end of file diff --git a/api/weekly/2024-05-04 b/api/weekly/2024-05-04 deleted file mode 100644 index 31b1803..0000000 --- a/api/weekly/2024-05-04 +++ /dev/null @@ -1,27 +0,0 @@ -{ - "weeklyId": "2024-05-04", - "matches": [{ - "match": { "matchId": "2024-05-04-finals" } - }, - { - "match": { "matchId": "2024-05-04-qualifying" } - }, - { - "match": { "matchId": "2024-05-04-quarterfinal-a" } - }, - { - "match": { "matchId": "2024-05-04-quarterfinal-b" } - }, - { - "match": { "matchId": "2024-05-04-quarterfinal-c" } - }, - { - "match": { "matchId": "2024-05-04-quarterfinal-d" } - }, - { - "match": { "matchId": "2024-05-04-semifinal-a" } - }, - { - "match": { "matchId": "2024-05-04-semifinal-b" } - }] -} \ No newline at end of file diff --git a/api/weekly/2024-05-11 b/api/weekly/2024-05-11 deleted file mode 100644 index 2ad1c05..0000000 --- a/api/weekly/2024-05-11 +++ /dev/null @@ -1,27 +0,0 @@ -{ - "weeklyId": "2024-05-11", - "matches": [{ - "match": { "matchId": "2024-05-11-finals" } - }, - { - "match": { "matchId": "2024-05-11-qualifying" } - }, - { - "match": { "matchId": "2024-05-11-quarterfinal-a" } - }, - { - "match": { "matchId": "2024-05-11-quarterfinal-b" } - }, - { - "match": { "matchId": "2024-05-11-quarterfinal-c" } - }, - { - "match": { "matchId": "2024-05-11-quarterfinal-d" } - }, - { - "match": { "matchId": "2024-05-11-semifinal-a" } - }, - { - "match": { "matchId": "2024-05-11-semifinal-b" } - }] -} \ No newline at end of file diff --git a/api/weekly/2024-05-17 b/api/weekly/2024-05-17 deleted file mode 100644 index a4389bb..0000000 --- a/api/weekly/2024-05-17 +++ /dev/null @@ -1,27 +0,0 @@ -{ - "weeklyId": "2024-05-17", - "matches": [{ - "match": { "matchId": "2024-05-17-qualifying" } - }, - { - "match": { "matchId": "2024-05-17-quarterfinal-a" } - }, - { - "match": { "matchId": "2024-05-17-quarterfinal-b" } - }, - { - "match": { "matchId": "2024-05-17-quarterfinal-c" } - }, - { - "match": { "matchId": "2024-05-17-quarterfinal-d" } - }, - { - "match": { "matchId": "2024-05-17-semifinal-a" } - }, - { - "match": { "matchId": "2024-05-17-semifinal-b" } - }, - { - "match": { "matchId": "2024-05-17-finals" } - }] -} \ No newline at end of file diff --git a/db/005_create_weekly.sql b/db/005_create_weekly.sql new file mode 100644 index 0000000..c11ee71 --- /dev/null +++ b/db/005_create_weekly.sql @@ -0,0 +1,16 @@ +create table Weekly( + WeeklyId varchar not null primary key +); + +create table WeeklyMatch( + WeeklyMatchId serial primary key, + WeeklyId varchar not null, + MatchId varchar not null, + foreign key(WeeklyId) references Weekly(WeeklyId), + foreign key(MatchId) references Match(MatchId) +); + +---- create above / drop below ---- + +drop table WeeklyMatch; +drop table Weekly; diff --git a/db/006_seed_weekly.sql b/db/006_seed_weekly.sql new file mode 100644 index 0000000..3ac3aa0 --- /dev/null +++ b/db/006_seed_weekly.sql @@ -0,0 +1,54 @@ +insert into Weekly (WeeklyId) +values + ('2024-04-27'), + ('2024-05-04'), + ('2024-05-11'), + ('2024-05-17'); + +insert into WeeklyMatch (WeeklyId, MatchId) +values + ('2024-04-27', '2024-04-27-finals'), + ('2024-04-27', '2024-04-27-firstround-a'), + ('2024-04-27', '2024-04-27-firstround-b'), + ('2024-04-27', '2024-04-27-firstround-c'), + ('2024-04-27', '2024-04-27-firstround-d'), + ('2024-04-27', '2024-04-27-firstround-e'), + ('2024-04-27', '2024-04-27-firstround-f'), + ('2024-04-27', '2024-04-27-firstround-g'), + ('2024-04-27', '2024-04-27-firstround-h'), + ('2024-04-27', '2024-04-27-qualifying'), + ('2024-04-27', '2024-04-27-quarterfinal-i'), + ('2024-04-27', '2024-04-27-quarterfinal-j'), + ('2024-04-27', '2024-04-27-quarterfinal-k'), + ('2024-04-27', '2024-04-27-quarterfinal-l'), + ('2024-04-27', '2024-04-27-semifinal-m'), + ('2024-04-27', '2024-04-27-semifinal-n'), + ('2024-05-04', '2024-05-04-finals'), + ('2024-05-04', '2024-05-04-qualifying'), + ('2024-05-04', '2024-05-04-quarterfinal-a'), + ('2024-05-04', '2024-05-04-quarterfinal-b'), + ('2024-05-04', '2024-05-04-quarterfinal-c'), + ('2024-05-04', '2024-05-04-quarterfinal-d'), + ('2024-05-04', '2024-05-04-semifinal-a'), + ('2024-05-04', '2024-05-04-semifinal-b'), + ('2024-05-11', '2024-05-11-finals'), + ('2024-05-11', '2024-05-11-qualifying'), + ('2024-05-11', '2024-05-11-quarterfinal-a'), + ('2024-05-11', '2024-05-11-quarterfinal-b'), + ('2024-05-11', '2024-05-11-quarterfinal-c'), + ('2024-05-11', '2024-05-11-quarterfinal-d'), + ('2024-05-11', '2024-05-11-semifinal-a'), + ('2024-05-11', '2024-05-11-semifinal-b'), + ('2024-05-17', '2024-05-17-qualifying'), + ('2024-05-17', '2024-05-17-quarterfinal-a'), + ('2024-05-17', '2024-05-17-quarterfinal-b'), + ('2024-05-17', '2024-05-17-quarterfinal-c'), + ('2024-05-17', '2024-05-17-quarterfinal-d'), + ('2024-05-17', '2024-05-17-semifinal-a'), + ('2024-05-17', '2024-05-17-semifinal-b'), + ('2024-05-17', '2024-05-17-finals'); + +---- create above / drop below ---- + +delete from WeeklyMatch; +delete from Weekly;