Skip to content

Commit

Permalink
grade api added (#68)
Browse files Browse the repository at this point in the history
* grade api added

* fixes

* grades api

* priv checks

---------

Co-authored-by: cmyui <[email protected]>
  • Loading branch information
Mahmood-Miah and cmyui authored Jul 24, 2024
1 parent ad82482 commit f7520a3
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 4 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ main
gitold/
akatsuki-api
akatsuki-api.exe
.env
.env
.vscode/
1 change: 1 addition & 0 deletions app/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ func Start(dbO *sqlx.DB) *fhr.Router {
r.Method("/api/v1/blog/posts", v1.BlogPostsGET)
r.Method("/api/v1/score", v1.ScoreGET)
r.Method("/api/v1/scores", v1.ScoresGET)
r.Method("/api/v1/grades", v1.UserGradesGET)
r.Method("/api/v1/countries", v1.CountriesGET)
r.Method("/api/v1/hypothetical-rank", v1.HypotheticalRankGET)

Expand Down
6 changes: 3 additions & 3 deletions app/v1/clan.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,8 +427,8 @@ func ClanSettingsPOST(md common.MethodData) common.CodeMessager {
Name string `json:"name,omitempty"`
Description string `json:"desc,omitempty"`
// Icon string `json:"icon,omitempty"`
Background string `json:"bg,omitempty"`
Status int `json:"status"`
Background string `json:"bg,omitempty"`
Status int `json:"status"`
}{}

md.Unmarshal(&u)
Expand All @@ -447,7 +447,7 @@ func ClanSettingsPOST(md common.MethodData) common.CodeMessager {
} else if md.DB.QueryRow("SELECT 1 FROM clans WHERE name = ? AND id != ?", u.Name, c.ID).Scan(new(int)) != sql.ErrNoRows {
return common.SimpleResponse(403, "Another clan has already taken this name")
}

tagRunes := []rune(u.Tag)
if len(tagRunes) > 6 || len(tagRunes) < 1 {
return common.SimpleResponse(400, "The given tag is too short or too long")
Expand Down
59 changes: 59 additions & 0 deletions app/v1/user_grades.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package v1

import (
"database/sql"
"fmt"

"github.com/osuAkatsuki/akatsuki-api/common"
)

type userGrades struct {
XHCount int `json:"xh_count"`
XCount int `json:"x_count"`
SHCount int `json:"sh_count"`
SCount int `json:"s_count"`
ACount int `json:"a_count"`
BCount int `json:"b_count"`
CCount int `json:"c_count"`
DCount int `json:"d_count"`
}
type userGradesResponse struct {
common.ResponseBase
Grades userGrades `json:"grades"`
}

func UserGradesGET(md common.MethodData) common.CodeMessager {
var response userGradesResponse
mode := common.Int(md.Query("mode"))
userID := common.Int(md.Query("id"))
query := fmt.Sprintf(`
SELECT
xh_count, x_count, sh_count, s_count,
a_count, b_count, c_count, d_count
FROM user_stats
INNER JOIN users ON users.id = user_stats.user_id
WHERE user_id = ? AND user_stats.mode = ? AND %s`,
md.User.OnlyUserPublic(true),
)

err := md.DB.QueryRow(query, userID, mode).Scan(
&response.Grades.XHCount,
&response.Grades.XCount,
&response.Grades.SHCount,
&response.Grades.SCount,
&response.Grades.ACount,
&response.Grades.BCount,
&response.Grades.CCount,
&response.Grades.DCount,
)

if err != nil {
if err == sql.ErrNoRows {
return common.SimpleResponse(404, "User stats not found")
}
md.Err(err)
return Err500
}
response.Code = 200
return response
}

0 comments on commit f7520a3

Please sign in to comment.