@@ -86,11 +86,40 @@ func main() {
86
86
router .GET ("/players" , IsPlayerExist )
87
87
router .POST ("/scores" , AddScore )
88
88
router .GET ("/scores" , AllUserScores )
89
+ router .GET ("/scorescount" , UserScoresCount )
89
90
router .GET ("/bestscores" , AllBestScores )
90
91
var url = fmt .Sprintf ("%s:%s" , host , port )
91
92
router .Run (url )
92
93
}
93
94
95
+ func UserScoresCount (c * gin.Context ) {
96
+ var curId idStruct
97
+ if err := c .BindJSON (& curId ); err != nil {
98
+ c .AbortWithStatusJSON (http .StatusBadRequest , gin.H {"error" : "Invalid request payload" })
99
+ return
100
+ }
101
+
102
+ var playerExists bool
103
+ err := db .QueryRow ("SELECT EXISTS(SELECT 1 FROM players WHERE player_id = $1)" , curId .Id ).Scan (& playerExists )
104
+ if err != nil {
105
+ log .Fatal (err )
106
+ }
107
+
108
+ fmt .Println (playerExists )
109
+ if ! playerExists {
110
+ c .AbortWithStatusJSON (http .StatusBadRequest , gin.H {"error" : "Player does not exist" })
111
+ return
112
+ }
113
+
114
+ var totalScore int
115
+ err = db .QueryRow ("SELECT COUNT(score_id) FROM scores WHERE fk_player_id = $1" , curId .Id ).Scan (& totalScore )
116
+ if err != nil {
117
+ log .Fatal (err )
118
+ }
119
+
120
+ c .JSON (http .StatusOK , gin.H {"total_scores" : totalScore })
121
+ }
122
+
94
123
func AddPlayer (c * gin.Context ) {
95
124
var newPlayer player
96
125
if err := c .BindJSON (& newPlayer ); err != nil {
@@ -215,7 +244,7 @@ func AllUserScores(c *gin.Context) {
215
244
return
216
245
}
217
246
218
- rows , err := db .Query ("SELECT score_id, points FROM scores WHERE fk_player_id = $1 LIMIT 30" , curId .Id )
247
+ rows , err := db .Query ("SELECT score_id, points FROM scores WHERE fk_player_id = $1 ORDER BY score_id DESC LIMIT 30" , curId .Id )
219
248
if err != nil {
220
249
log .Fatal (err )
221
250
}
0 commit comments