diff options
| author | Nidboj132 <lol2s@vp.plm> | 2023-09-05 18:23:11 +0200 |
|---|---|---|
| committer | Nidboj132 <lol2s@vp.plm> | 2023-09-05 18:23:11 +0200 |
| commit | 3869cb67351ccf3bc45b076f31afdc7133292c39 (patch) | |
| tree | dc03341e147dde0964bf6be84b14e13424c647b7 /backend/handlers/logs.go | |
| parent | added graph and fixed some css (diff) | |
| parent | fix: create map summary, why the fuck does this have to be a pointer integer?? (diff) | |
| download | lphub-3869cb67351ccf3bc45b076f31afdc7133292c39.tar.gz lphub-3869cb67351ccf3bc45b076f31afdc7133292c39.tar.bz2 lphub-3869cb67351ccf3bc45b076f31afdc7133292c39.zip | |
Merge branch 'main' of https://github.com/pektezol/LeastPortalsHub
Former-commit-id: 221385f463b7f5b0fc43a093b2c7c46e68d46d68
Diffstat (limited to 'backend/handlers/logs.go')
| -rw-r--r-- | backend/handlers/logs.go | 189 |
1 files changed, 189 insertions, 0 deletions
diff --git a/backend/handlers/logs.go b/backend/handlers/logs.go new file mode 100644 index 0000000..2b8223a --- /dev/null +++ b/backend/handlers/logs.go | |||
| @@ -0,0 +1,189 @@ | |||
| 1 | package handlers | ||
| 2 | |||
| 3 | import ( | ||
| 4 | "fmt" | ||
| 5 | "net/http" | ||
| 6 | "time" | ||
| 7 | |||
| 8 | "github.com/gin-gonic/gin" | ||
| 9 | "github.com/pektezol/leastportalshub/backend/database" | ||
| 10 | "github.com/pektezol/leastportalshub/backend/models" | ||
| 11 | ) | ||
| 12 | |||
| 13 | const ( | ||
| 14 | LogTypeMod string = "Mod" | ||
| 15 | LogTypeUser string = "User" | ||
| 16 | LogTypeRecord string = "Record" | ||
| 17 | |||
| 18 | LogDescriptionUserLoginSuccess string = "LoginSuccess" | ||
| 19 | LogDescriptionUserLoginFailToken string = "LoginTokenFail" | ||
| 20 | LogDescriptionUserLoginFailValidate string = "LoginValidateFail" | ||
| 21 | LogDescriptionUserLoginFailSummary string = "LoginSummaryFail" | ||
| 22 | LogDescriptionUserUpdateSuccess string = "UpdateSuccess" | ||
| 23 | LogDescriptionUserUpdateFail string = "UpdateFail" | ||
| 24 | LogDescriptionUserUpdateSummaryFail string = "UpdateSummaryFail" | ||
| 25 | LogDescriptionUserUpdateCountrySuccess string = "UpdateCountrySuccess" | ||
| 26 | LogDescriptionUserUpdateCountryFail string = "UpdateCountryFail" | ||
| 27 | |||
| 28 | LogDescriptionMapSummaryCreate string = "MapSummaryCreate" | ||
| 29 | LogDescriptionMapSummaryEdit string = "MapSummaryEdit" | ||
| 30 | LogDescriptionMapSummaryEditImage string = "MapSummaryEditImage" | ||
| 31 | LogDescriptionMapSummaryDelete string = "MapSummaryDelete" | ||
| 32 | |||
| 33 | LogDescriptionRecordSuccess string = "Success" | ||
| 34 | LogDescriptionRecordFailInsertRecord string = "InsertRecordFail" | ||
| 35 | LogDescriptionRecordFailInsertDemo string = "InsertDemoFail" | ||
| 36 | LogDescriptionRecordFailProcessDemo string = "ProcessDemoFail" | ||
| 37 | LogDescriptionRecordFailCreateDemo string = "CreateDemoFail" | ||
| 38 | LogDescriptionRecordFailOpenDemo string = "OpenDemoFail" | ||
| 39 | LogDescriptionRecordFailSaveDemo string = "SaveDemoFail" | ||
| 40 | LogDescriptionRecordFailInvalidRequest string = "InvalidRequestFail" | ||
| 41 | ) | ||
| 42 | |||
| 43 | type Log struct { | ||
| 44 | User models.UserShort `json:"user"` | ||
| 45 | Type string `json:"type"` | ||
| 46 | Description string `json:"description"` | ||
| 47 | Date time.Time `json:"date"` | ||
| 48 | } | ||
| 49 | |||
| 50 | type LogsResponse struct { | ||
| 51 | Logs []LogsResponseDetails `json:"logs"` | ||
| 52 | } | ||
| 53 | |||
| 54 | type LogsResponseDetails struct { | ||
| 55 | User models.UserShort `json:"user"` | ||
| 56 | Log string `json:"detail"` | ||
| 57 | Date time.Time `json:"date"` | ||
| 58 | } | ||
| 59 | |||
| 60 | type ScoreLogsResponse struct { | ||
| 61 | Logs []ScoreLogsResponseDetails `json:"scores"` | ||
| 62 | } | ||
| 63 | |||
| 64 | type ScoreLogsResponseDetails struct { | ||
| 65 | Game models.Game `json:"game"` | ||
| 66 | User models.UserShort `json:"user"` | ||
| 67 | Map models.MapShort `json:"map"` | ||
| 68 | ScoreCount int `json:"score_count"` | ||
| 69 | ScoreTime int `json:"score_time"` | ||
| 70 | DemoID string `json:"demo_id"` | ||
| 71 | Date time.Time `json:"date"` | ||
| 72 | } | ||
| 73 | |||
| 74 | // GET Mod Logs | ||
| 75 | // | ||
| 76 | // @Description Get mod logs. | ||
| 77 | // @Tags logs | ||
| 78 | // @Produce json | ||
| 79 | // @Param Authorization header string true "JWT Token" | ||
| 80 | // @Success 200 {object} models.Response{data=LogsResponse} | ||
| 81 | // @Failure 400 {object} models.Response | ||
| 82 | // @Router /logs/mod [get] | ||
| 83 | func ModLogs(c *gin.Context) { | ||
| 84 | mod, exists := c.Get("mod") | ||
| 85 | if !exists || !mod.(bool) { | ||
| 86 | c.JSON(http.StatusUnauthorized, models.ErrorResponse("Insufficient permissions.")) | ||
| 87 | return | ||
| 88 | } | ||
| 89 | response := LogsResponse{Logs: []LogsResponseDetails{}} | ||
| 90 | sql := `SELECT u.user_name, l.user_id, l.type, l.description, l.date | ||
| 91 | FROM logs l INNER JOIN users u ON l.user_id = u.steam_id WHERE type != 'Score' | ||
| 92 | ORDER BY l.date DESC LIMIT 100;` | ||
| 93 | rows, err := database.DB.Query(sql) | ||
| 94 | if err != nil { | ||
| 95 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | ||
| 96 | return | ||
| 97 | } | ||
| 98 | for rows.Next() { | ||
| 99 | log := Log{} | ||
| 100 | err = rows.Scan(&log.User.UserName, &log.User.SteamID, &log.Type, &log.Description, &log.Date) | ||
| 101 | if err != nil { | ||
| 102 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | ||
| 103 | return | ||
| 104 | } | ||
| 105 | detail := fmt.Sprintf("%s.%s", log.Type, log.Description) | ||
| 106 | response.Logs = append(response.Logs, LogsResponseDetails{ | ||
| 107 | User: models.UserShort{ | ||
| 108 | SteamID: log.User.SteamID, | ||
| 109 | UserName: log.User.UserName, | ||
| 110 | }, | ||
| 111 | Log: detail, | ||
| 112 | Date: log.Date, | ||
| 113 | }) | ||
| 114 | } | ||
| 115 | c.JSON(http.StatusOK, models.Response{ | ||
| 116 | Success: true, | ||
| 117 | Message: "Successfully retrieved logs.", | ||
| 118 | Data: response, | ||
| 119 | }) | ||
| 120 | } | ||
| 121 | |||
| 122 | // GET Score Logs | ||
| 123 | // | ||
| 124 | // @Description Get score logs of every player. | ||
| 125 | // @Tags logs | ||
| 126 | // @Produce json | ||
| 127 | // @Success 200 {object} models.Response{data=ScoreLogsResponse} | ||
| 128 | // @Failure 400 {object} models.Response | ||
| 129 | // @Router /logs/score [get] | ||
| 130 | func ScoreLogs(c *gin.Context) { | ||
| 131 | response := ScoreLogsResponse{Logs: []ScoreLogsResponseDetails{}} | ||
| 132 | sql := `SELECT g.id, | ||
| 133 | g."name", | ||
| 134 | g.is_coop, | ||
| 135 | rs.map_id, | ||
| 136 | m.name AS map_name, | ||
| 137 | u.steam_id, | ||
| 138 | u.user_name, | ||
| 139 | rs.score_count, | ||
| 140 | rs.score_time, | ||
| 141 | rs.demo_id, | ||
| 142 | rs.record_date | ||
| 143 | FROM ( | ||
| 144 | SELECT id, map_id, user_id, score_count, score_time, demo_id, record_date | ||
| 145 | FROM records_sp | ||
| 146 | |||
| 147 | UNION ALL | ||
| 148 | |||
| 149 | SELECT id, map_id, host_id AS user_id, score_count, score_time, host_demo_id AS demo_id, record_date | ||
| 150 | FROM records_mp | ||
| 151 | |||
| 152 | UNION ALL | ||
| 153 | |||
| 154 | SELECT id, map_id, partner_id AS user_id, score_count, score_time, partner_demo_id AS demo_id, record_date | ||
| 155 | FROM records_mp | ||
| 156 | ) AS rs | ||
| 157 | JOIN users u ON rs.user_id = u.steam_id | ||
| 158 | JOIN maps m ON rs.map_id = m.id | ||
| 159 | JOIN games g ON m.game_id = g.id | ||
| 160 | ORDER BY rs.record_date DESC LIMIT 100;` | ||
| 161 | rows, err := database.DB.Query(sql) | ||
| 162 | if err != nil { | ||
| 163 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | ||
| 164 | return | ||
| 165 | } | ||
| 166 | for rows.Next() { | ||
| 167 | score := ScoreLogsResponseDetails{} | ||
| 168 | err = rows.Scan(&score.Game.ID, &score.Game.Name, &score.Game.IsCoop, &score.Map.ID, &score.Map.Name, &score.User.SteamID, &score.User.UserName, &score.ScoreCount, &score.ScoreTime, &score.DemoID, &score.Date) | ||
| 169 | if err != nil { | ||
| 170 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | ||
| 171 | return | ||
| 172 | } | ||
| 173 | response.Logs = append(response.Logs, score) | ||
| 174 | } | ||
| 175 | c.JSON(http.StatusOK, models.Response{ | ||
| 176 | Success: true, | ||
| 177 | Message: "Successfully retrieved score logs.", | ||
| 178 | Data: response, | ||
| 179 | }) | ||
| 180 | } | ||
| 181 | |||
| 182 | func CreateLog(user_id string, log_type string, log_description string) (err error) { | ||
| 183 | sql := `INSERT INTO logs (user_id, "type", description) VALUES($1, $2, $3)` | ||
| 184 | _, err = database.DB.Exec(sql, user_id, log_type, log_description) | ||
| 185 | if err != nil { | ||
| 186 | return err | ||
| 187 | } | ||
| 188 | return nil | ||
| 189 | } | ||