diff options
| author | Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> | 2023-09-02 13:40:58 +0300 |
|---|---|---|
| committer | Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> | 2023-09-02 13:40:58 +0300 |
| commit | 5ee6c4abb03fdb583e94496306627d9d3db62629 (patch) | |
| tree | 1d282842d007608c32a21c0247915d256ec2d4d9 /backend/handlers | |
| parent | fix: row scan pointers (#55) (diff) | |
| download | lphub-5ee6c4abb03fdb583e94496306627d9d3db62629.tar.gz lphub-5ee6c4abb03fdb583e94496306627d9d3db62629.tar.bz2 lphub-5ee6c4abb03fdb583e94496306627d9d3db62629.zip | |
feat: finalize score logs (#55)
Former-commit-id: 5151b31745579814336797091d52c6252b14a4c2
Diffstat (limited to 'backend/handlers')
| -rw-r--r-- | backend/handlers/logs.go | 57 |
1 files changed, 43 insertions, 14 deletions
diff --git a/backend/handlers/logs.go b/backend/handlers/logs.go index 31fd6fd..c77d643 100644 --- a/backend/handlers/logs.go +++ b/backend/handlers/logs.go | |||
| @@ -3,6 +3,7 @@ package handlers | |||
| 3 | import ( | 3 | import ( |
| 4 | "fmt" | 4 | "fmt" |
| 5 | "net/http" | 5 | "net/http" |
| 6 | "time" | ||
| 6 | 7 | ||
| 7 | "github.com/gin-gonic/gin" | 8 | "github.com/gin-gonic/gin" |
| 8 | "github.com/pektezol/leastportalshub/backend/database" | 9 | "github.com/pektezol/leastportalshub/backend/database" |
| @@ -11,7 +12,6 @@ import ( | |||
| 11 | 12 | ||
| 12 | const ( | 13 | const ( |
| 13 | LogTypeMod string = "Mod" | 14 | LogTypeMod string = "Mod" |
| 14 | LogTypeScore string = "Score" | ||
| 15 | LogTypeLogin string = "Login" | 15 | LogTypeLogin string = "Login" |
| 16 | 16 | ||
| 17 | LogDescriptionLoginSuccess string = "Success" | 17 | LogDescriptionLoginSuccess string = "Success" |
| @@ -35,6 +35,19 @@ type LogsResponseDetails struct { | |||
| 35 | Log string `json:"detail"` | 35 | Log string `json:"detail"` |
| 36 | } | 36 | } |
| 37 | 37 | ||
| 38 | type ScoreLogsResponse struct { | ||
| 39 | Logs []ScoreLogsResponseDetails `json:"scores"` | ||
| 40 | } | ||
| 41 | |||
| 42 | type ScoreLogsResponseDetails struct { | ||
| 43 | User models.UserShort `json:"user"` | ||
| 44 | Map models.MapShort `json:"map"` | ||
| 45 | ScoreCount int `json:"score_count"` | ||
| 46 | ScoreTime int `json:"score_time"` | ||
| 47 | DemoID string `json:"demo_id"` | ||
| 48 | Date time.Time `json:"date"` | ||
| 49 | } | ||
| 50 | |||
| 38 | func ModLogs(c *gin.Context) { | 51 | func ModLogs(c *gin.Context) { |
| 39 | mod, exists := c.Get("mod") | 52 | mod, exists := c.Get("mod") |
| 40 | if !exists || !mod.(bool) { | 53 | if !exists || !mod.(bool) { |
| @@ -73,29 +86,45 @@ func ModLogs(c *gin.Context) { | |||
| 73 | } | 86 | } |
| 74 | 87 | ||
| 75 | func ScoreLogs(c *gin.Context) { | 88 | func ScoreLogs(c *gin.Context) { |
| 76 | response := LogsResponse{Logs: []LogsResponseDetails{}} | 89 | response := ScoreLogsResponse{Logs: []ScoreLogsResponseDetails{}} |
| 77 | sql := `SELECT u.user_name, l.user_id, l.type, l.description | 90 | sql := `SELECT rs.map_id, |
| 78 | FROM logs l INNER JOIN users u ON l.user_id = u.steam_id WHERE type = 'Score'` | 91 | m.name AS map_name, |
| 92 | u.steam_id, | ||
| 93 | u.user_name, | ||
| 94 | rs.score_count, | ||
| 95 | rs.score_time, | ||
| 96 | rs.demo_id, | ||
| 97 | rs.record_date | ||
| 98 | FROM ( | ||
| 99 | SELECT id, map_id, user_id, score_count, score_time, demo_id, record_date | ||
| 100 | FROM public.records_sp | ||
| 101 | |||
| 102 | UNION ALL | ||
| 103 | |||
| 104 | SELECT id, map_id, host_id AS user_id, score_count, score_time, host_demo_id AS demo_id, record_date | ||
| 105 | FROM public.records_mp | ||
| 106 | |||
| 107 | UNION ALL | ||
| 108 | |||
| 109 | SELECT id, map_id, partner_id AS user_id, score_count, score_time, partner_demo_id AS demo_id, record_date | ||
| 110 | FROM public.records_mp | ||
| 111 | ) AS rs | ||
| 112 | JOIN public.users AS u ON rs.user_id = u.steam_id | ||
| 113 | JOIN public.maps AS m ON rs.map_id = m.id | ||
| 114 | ORDER BY rs.record_date DESC LIMIT 100;` | ||
| 79 | rows, err := database.DB.Query(sql) | 115 | rows, err := database.DB.Query(sql) |
| 80 | if err != nil { | 116 | if err != nil { |
| 81 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | 117 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) |
| 82 | return | 118 | return |
| 83 | } | 119 | } |
| 84 | for rows.Next() { | 120 | for rows.Next() { |
| 85 | log := Log{} | 121 | score := ScoreLogsResponseDetails{} |
| 86 | err = rows.Scan(&log.User.UserName, &log.User.SteamID, &log.Type, &log.Description) | 122 | err = rows.Scan(&score.Map.ID, &score.Map.Name, &score.User.SteamID, &score.User.UserName, &score.ScoreCount, &score.ScoreTime, &score.DemoID, &score.Date) |
| 87 | if err != nil { | 123 | if err != nil { |
| 88 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | 124 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) |
| 89 | return | 125 | return |
| 90 | } | 126 | } |
| 91 | detail := fmt.Sprintf("%s.%s", log.Type, log.Description) | 127 | response.Logs = append(response.Logs, score) |
| 92 | response.Logs = append(response.Logs, LogsResponseDetails{ | ||
| 93 | User: models.UserShort{ | ||
| 94 | SteamID: log.User.SteamID, | ||
| 95 | UserName: log.User.UserName, | ||
| 96 | }, | ||
| 97 | Log: detail, | ||
| 98 | }) | ||
| 99 | } | 128 | } |
| 100 | c.JSON(http.StatusOK, models.Response{ | 129 | c.JSON(http.StatusOK, models.Response{ |
| 101 | Success: true, | 130 | Success: true, |