1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
package handlers
import (
"fmt"
"net/http"
"time"
"github.com/gin-gonic/gin"
"github.com/pektezol/leastportalshub/backend/database"
"github.com/pektezol/leastportalshub/backend/models"
)
const (
LogTypeMod string = "Mod"
LogTypeLogin string = "Login"
LogDescriptionLoginSuccess string = "Success"
LogDescriptionLoginFailToken string = "TokenFail"
LogDescriptionLoginFailValidate string = "ValidateFail"
LogDescriptionLoginFailSummary string = "SummaryFail"
)
type Log struct {
User models.UserShort `json:"user"`
Type string `json:"type"`
Description string `json:"description"`
}
type LogsResponse struct {
Logs []LogsResponseDetails `json:"logs"`
}
type LogsResponseDetails struct {
User models.UserShort `json:"user"`
Log string `json:"detail"`
}
type ScoreLogsResponse struct {
Logs []ScoreLogsResponseDetails `json:"scores"`
}
type ScoreLogsResponseDetails struct {
User models.UserShort `json:"user"`
Map models.MapShort `json:"map"`
ScoreCount int `json:"score_count"`
ScoreTime int `json:"score_time"`
DemoID string `json:"demo_id"`
Date time.Time `json:"date"`
}
func ModLogs(c *gin.Context) {
mod, exists := c.Get("mod")
if !exists || !mod.(bool) {
c.JSON(http.StatusUnauthorized, models.ErrorResponse("Insufficient permissions."))
return
}
response := LogsResponse{Logs: []LogsResponseDetails{}}
sql := `SELECT u.user_name, l.user_id, l.type, l.description
FROM logs l INNER JOIN users u ON l.user_id = u.steam_id WHERE type != 'Score'`
rows, err := database.DB.Query(sql)
if err != nil {
c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error()))
return
}
for rows.Next() {
log := Log{}
err = rows.Scan(&log.User.UserName, &log.User.SteamID, &log.Type, &log.Description)
if err != nil {
c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error()))
return
}
detail := fmt.Sprintf("%s.%s", log.Type, log.Description)
response.Logs = append(response.Logs, LogsResponseDetails{
User: models.UserShort{
SteamID: log.User.SteamID,
UserName: log.User.UserName,
},
Log: detail,
})
}
c.JSON(http.StatusOK, models.Response{
Success: true,
Message: "Successfully retrieved logs.",
Data: response,
})
}
func ScoreLogs(c *gin.Context) {
response := ScoreLogsResponse{Logs: []ScoreLogsResponseDetails{}}
sql := `SELECT rs.map_id,
m.name AS map_name,
u.steam_id,
u.user_name,
rs.score_count,
rs.score_time,
rs.demo_id,
rs.record_date
FROM (
SELECT id, map_id, user_id, score_count, score_time, demo_id, record_date
FROM public.records_sp
UNION ALL
SELECT id, map_id, host_id AS user_id, score_count, score_time, host_demo_id AS demo_id, record_date
FROM public.records_mp
UNION ALL
SELECT id, map_id, partner_id AS user_id, score_count, score_time, partner_demo_id AS demo_id, record_date
FROM public.records_mp
) AS rs
JOIN public.users AS u ON rs.user_id = u.steam_id
JOIN public.maps AS m ON rs.map_id = m.id
ORDER BY rs.record_date DESC LIMIT 100;`
rows, err := database.DB.Query(sql)
if err != nil {
c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error()))
return
}
for rows.Next() {
score := ScoreLogsResponseDetails{}
err = rows.Scan(&score.Map.ID, &score.Map.Name, &score.User.SteamID, &score.User.UserName, &score.ScoreCount, &score.ScoreTime, &score.DemoID, &score.Date)
if err != nil {
c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error()))
return
}
response.Logs = append(response.Logs, score)
}
c.JSON(http.StatusOK, models.Response{
Success: true,
Message: "Successfully retrieved score logs.",
Data: response,
})
}
func CreateLog(user_id string, log_type string, log_description string) (err error) {
sql := `INSERT INTO logs (user_id, "type", description) VALUES($1, $2, $3)`
_, err = database.DB.Exec(sql, user_id, log_type, log_description)
if err != nil {
return err
}
return nil
}
|