diff options
| author | Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> | 2023-09-03 13:32:25 +0300 |
|---|---|---|
| committer | Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> | 2023-09-03 13:32:25 +0300 |
| commit | 3eb88ce01d1e723dc93971dc25da728437912e8f (patch) | |
| tree | 4c38808e3ca744bbb7cb668df7bad7ded5c73b6f /backend | |
| parent | docs: update log response type (diff) | |
| download | lphub-3eb88ce01d1e723dc93971dc25da728437912e8f.tar.gz lphub-3eb88ce01d1e723dc93971dc25da728437912e8f.tar.bz2 lphub-3eb88ce01d1e723dc93971dc25da728437912e8f.zip | |
feat: update user logs (#55)
Former-commit-id: 9041bf919cbd96eb135abc14318910b81340f95d
Diffstat (limited to 'backend')
| -rw-r--r-- | backend/handlers/login.go | 8 | ||||
| -rw-r--r-- | backend/handlers/logs.go | 21 |
2 files changed, 16 insertions, 13 deletions
diff --git a/backend/handlers/login.go b/backend/handlers/login.go index 80f697e..5949fdd 100644 --- a/backend/handlers/login.go +++ b/backend/handlers/login.go | |||
| @@ -38,7 +38,7 @@ func Login(c *gin.Context) { | |||
| 38 | default: | 38 | default: |
| 39 | steamID, err := openID.ValidateAndGetId() | 39 | steamID, err := openID.ValidateAndGetId() |
| 40 | if err != nil { | 40 | if err != nil { |
| 41 | CreateLog(steamID, LogTypeLogin, LogDescriptionLoginFailValidate) | 41 | CreateLog(steamID, LogTypeLogin, LogDescriptionUserLoginFailValidate) |
| 42 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | 42 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) |
| 43 | return | 43 | return |
| 44 | } | 44 | } |
| @@ -49,7 +49,7 @@ func Login(c *gin.Context) { | |||
| 49 | if checkSteamID == 0 { | 49 | if checkSteamID == 0 { |
| 50 | user, err := GetPlayerSummaries(steamID, os.Getenv("API_KEY")) | 50 | user, err := GetPlayerSummaries(steamID, os.Getenv("API_KEY")) |
| 51 | if err != nil { | 51 | if err != nil { |
| 52 | CreateLog(steamID, LogTypeLogin, LogDescriptionLoginFailSummary) | 52 | CreateLog(steamID, LogTypeLogin, LogDescriptionUserLoginFailSummary) |
| 53 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | 53 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) |
| 54 | return | 54 | return |
| 55 | } | 55 | } |
| @@ -79,12 +79,12 @@ func Login(c *gin.Context) { | |||
| 79 | // Sign and get the complete encoded token as a string using the secret | 79 | // Sign and get the complete encoded token as a string using the secret |
| 80 | tokenString, err := token.SignedString([]byte(os.Getenv("SECRET_KEY"))) | 80 | tokenString, err := token.SignedString([]byte(os.Getenv("SECRET_KEY"))) |
| 81 | if err != nil { | 81 | if err != nil { |
| 82 | CreateLog(steamID, LogTypeLogin, LogDescriptionLoginFailToken) | 82 | CreateLog(steamID, LogTypeLogin, LogDescriptionUserLoginFailToken) |
| 83 | c.JSON(http.StatusBadRequest, models.ErrorResponse("Failed to generate token.")) | 83 | c.JSON(http.StatusBadRequest, models.ErrorResponse("Failed to generate token.")) |
| 84 | return | 84 | return |
| 85 | } | 85 | } |
| 86 | c.SetCookie("token", tokenString, 3600*24*30, "/", "", true, true) | 86 | c.SetCookie("token", tokenString, 3600*24*30, "/", "", true, true) |
| 87 | CreateLog(steamID, LogTypeLogin, LogDescriptionLoginSuccess) | 87 | CreateLog(steamID, LogTypeLogin, LogDescriptionUserLoginSuccess) |
| 88 | c.Redirect(http.StatusTemporaryRedirect, "/") | 88 | c.Redirect(http.StatusTemporaryRedirect, "/") |
| 89 | // c.JSON(http.StatusOK, models.Response{ | 89 | // c.JSON(http.StatusOK, models.Response{ |
| 90 | // Success: true, | 90 | // Success: true, |
diff --git a/backend/handlers/logs.go b/backend/handlers/logs.go index c971f79..b5704d6 100644 --- a/backend/handlers/logs.go +++ b/backend/handlers/logs.go | |||
| @@ -12,13 +12,13 @@ import ( | |||
| 12 | 12 | ||
| 13 | const ( | 13 | const ( |
| 14 | LogTypeMod string = "Mod" | 14 | LogTypeMod string = "Mod" |
| 15 | LogTypeLogin string = "Login" | 15 | LogTypeLogin string = "User" |
| 16 | LogTypeRecord string = "Record" | 16 | LogTypeRecord string = "Record" |
| 17 | 17 | ||
| 18 | LogDescriptionLoginSuccess string = "Success" | 18 | LogDescriptionUserLoginSuccess string = "LoginSuccess" |
| 19 | LogDescriptionLoginFailToken string = "TokenFail" | 19 | LogDescriptionUserLoginFailToken string = "LoginTokenFail" |
| 20 | LogDescriptionLoginFailValidate string = "ValidateFail" | 20 | LogDescriptionUserLoginFailValidate string = "LoginValidateFail" |
| 21 | LogDescriptionLoginFailSummary string = "SummaryFail" | 21 | LogDescriptionUserLoginFailSummary string = "LoginSummaryFail" |
| 22 | 22 | ||
| 23 | LogDescriptionMapSummaryCreate string = "MapSummaryCreate" | 23 | LogDescriptionMapSummaryCreate string = "MapSummaryCreate" |
| 24 | LogDescriptionMapSummaryEdit string = "MapSummaryEdit" | 24 | LogDescriptionMapSummaryEdit string = "MapSummaryEdit" |
| @@ -39,6 +39,7 @@ type Log struct { | |||
| 39 | User models.UserShort `json:"user"` | 39 | User models.UserShort `json:"user"` |
| 40 | Type string `json:"type"` | 40 | Type string `json:"type"` |
| 41 | Description string `json:"description"` | 41 | Description string `json:"description"` |
| 42 | Date time.Time `json:"date"` | ||
| 42 | } | 43 | } |
| 43 | 44 | ||
| 44 | type LogsResponse struct { | 45 | type LogsResponse struct { |
| @@ -48,6 +49,7 @@ type LogsResponse struct { | |||
| 48 | type LogsResponseDetails struct { | 49 | type LogsResponseDetails struct { |
| 49 | User models.UserShort `json:"user"` | 50 | User models.UserShort `json:"user"` |
| 50 | Log string `json:"detail"` | 51 | Log string `json:"detail"` |
| 52 | Date time.Time `json:"date"` | ||
| 51 | } | 53 | } |
| 52 | 54 | ||
| 53 | type ScoreLogsResponse struct { | 55 | type ScoreLogsResponse struct { |
| @@ -70,7 +72,7 @@ type ScoreLogsResponseDetails struct { | |||
| 70 | // @Tags logs | 72 | // @Tags logs |
| 71 | // @Produce json | 73 | // @Produce json |
| 72 | // @Param Authorization header string true "JWT Token" | 74 | // @Param Authorization header string true "JWT Token" |
| 73 | // @Success 200 {object} models.Response{data=ScoreLogsResponse} | 75 | // @Success 200 {object} models.Response{data=LogsResponse} |
| 74 | // @Failure 400 {object} models.Response | 76 | // @Failure 400 {object} models.Response |
| 75 | // @Router /logs/mod [get] | 77 | // @Router /logs/mod [get] |
| 76 | func ModLogs(c *gin.Context) { | 78 | func ModLogs(c *gin.Context) { |
| @@ -80,7 +82,7 @@ func ModLogs(c *gin.Context) { | |||
| 80 | return | 82 | return |
| 81 | } | 83 | } |
| 82 | response := LogsResponse{Logs: []LogsResponseDetails{}} | 84 | response := LogsResponse{Logs: []LogsResponseDetails{}} |
| 83 | sql := `SELECT u.user_name, l.user_id, l.type, l.description | 85 | sql := `SELECT u.user_name, l.user_id, l.type, l.description, l.date |
| 84 | FROM logs l INNER JOIN users u ON l.user_id = u.steam_id WHERE type != 'Score'` | 86 | FROM logs l INNER JOIN users u ON l.user_id = u.steam_id WHERE type != 'Score'` |
| 85 | rows, err := database.DB.Query(sql) | 87 | rows, err := database.DB.Query(sql) |
| 86 | if err != nil { | 88 | if err != nil { |
| @@ -89,7 +91,7 @@ func ModLogs(c *gin.Context) { | |||
| 89 | } | 91 | } |
| 90 | for rows.Next() { | 92 | for rows.Next() { |
| 91 | log := Log{} | 93 | log := Log{} |
| 92 | err = rows.Scan(&log.User.UserName, &log.User.SteamID, &log.Type, &log.Description) | 94 | err = rows.Scan(&log.User.UserName, &log.User.SteamID, &log.Type, &log.Description, &log.Date) |
| 93 | if err != nil { | 95 | if err != nil { |
| 94 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | 96 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) |
| 95 | return | 97 | return |
| @@ -100,7 +102,8 @@ func ModLogs(c *gin.Context) { | |||
| 100 | SteamID: log.User.SteamID, | 102 | SteamID: log.User.SteamID, |
| 101 | UserName: log.User.UserName, | 103 | UserName: log.User.UserName, |
| 102 | }, | 104 | }, |
| 103 | Log: detail, | 105 | Log: detail, |
| 106 | Date: log.Date, | ||
| 104 | }) | 107 | }) |
| 105 | } | 108 | } |
| 106 | c.JSON(http.StatusOK, models.Response{ | 109 | c.JSON(http.StatusOK, models.Response{ |