Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: codechef and codeforces institute level rank feature added #244

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 143 additions & 0 deletions controllers/rank.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package controllers

import (
// "context"
// "encoding/json"
"log"
"net/http"
"sort"
"strconv"

// "os"
// "time"

// "github.com/google/uuid"
// "github.com/mdg-iitr/Codephile/services/mail"

"github.com/astaxie/beego"
// "github.com/dgrijalva/jwt-go"
// "github.com/dgrijalva/jwt-go/request"
"github.com/getsentry/sentry-go"
// "github.com/globalsign/mgo/bson"
// "github.com/gorilla/schema"
// . "github.com/mdg-iitr/Codephile/conf"
. "github.com/mdg-iitr/Codephile/errors"
"github.com/mdg-iitr/Codephile/models"
"github.com/mdg-iitr/Codephile/models/types"
// "github.com/mdg-iitr/Codephile/scrappers"
// "github.com/mdg-iitr/Codephile/services/auth"
// "github.com/mdg-iitr/Codephile/services/firebase"
// "github.com/mdg-iitr/Codephile/services/redis"
// "github.com/mdg-iitr/Codephile/services/worker"
)



// Operations about the User's CP institute level rank
type RankController struct {
beego.Controller
}

// @Title Codechef rank
// @Description Codechef inistitute level rank based on codechef worldrank
// @Security token_auth read:user
// @Param institute query string true "institute"
// @Success 200 {object} []types.SearchDoc
// @Failure 409 no user to belongs to this institute or user doesnt have codechef handle
// @Failure 400 bad request no institute parameter
// @Failure 500 server_error
// @router /codechef[get]
func (u *RankController) codechefRank() {
instituteName := u.GetString("institute")
res, err := models.FilterUsers(instituteName)
if err != nil {
hub := sentry.GetHubFromContext(u.Ctx.Request.Context())
hub.CaptureException(err)
log.Println(err.Error())
u.Ctx.ResponseWriter.WriteHeader(http.StatusInternalServerError)
u.Data["json"] = InternalServerError("server error.. report to admin")
u.ServeJSON()
return
}
if len(res) == 0 {
u.Ctx.ResponseWriter.WriteHeader(http.StatusNotFound)
u.Data["json"] = NotFoundError("no user belongs to this institute")
u.ServeJSON()
return
}
userWithCodechefHandle:= []types.SearchDoc{}

for _,user := range res {
if(user.Handle.Codechef!=""){
userWithCodechefHandle= append(userWithCodechefHandle,user)
}
}
if len(userWithCodechefHandle) == 0 {
u.Ctx.ResponseWriter.WriteHeader(http.StatusNotFound)
u.Data["json"] = NotFoundError("no user have codechef handle")
u.ServeJSON()
return
}
sort.Slice(userWithCodechefHandle,func(i,j int)bool{
p1,_ := models.GetProfiles(userWithCodechefHandle[i].ID)
p2,_ := models.GetProfiles(userWithCodechefHandle[i].ID)
rank1,_ := strconv.Atoi(p1.CodechefProfile.WorldRank)
rank2,_ := strconv.Atoi(p2.CodechefProfile.WorldRank)
return rank1>rank2
})

u.Data["json"] = userWithCodechefHandle
u.ServeJSON()
}

// @Title Codeforces rank
// @Description Codechef inistitute level rank based on codeforces worldrank
// @Security token_auth read:user
// @Param institute query string true "institute"
// @Success 200 {object} []types.SearchDoc
// @Failure 409 no user to belongs to this institute or user doesnt have codechef handle
// @Failure 400 bad request no institute parameter
// @Failure 500 server_error
// @router /codeforces[get]
func (u *RankController) codeforcesRank() {
instituteName := u.GetString("institute")
res, err := models.FilterUsers(instituteName)
if err != nil {
hub := sentry.GetHubFromContext(u.Ctx.Request.Context())
hub.CaptureException(err)
log.Println(err.Error())
u.Ctx.ResponseWriter.WriteHeader(http.StatusInternalServerError)
u.Data["json"] = InternalServerError("server error.. report to admin")
u.ServeJSON()
return
}
if len(res) == 0 {
u.Ctx.ResponseWriter.WriteHeader(http.StatusNotFound)
u.Data["json"] = NotFoundError("no user belongs to this institute")
u.ServeJSON()
return
}
userWithCodeforcesHandle:= []types.SearchDoc{}

for _,user := range res {
if(user.Handle.Codechef!=""){
userWithCodeforcesHandle= append(userWithCodeforcesHandle,user)
}
}
if len(userWithCodeforcesHandle) == 0 {
u.Ctx.ResponseWriter.WriteHeader(http.StatusNotFound)
u.Data["json"] = NotFoundError("no user have codeforces handle")
u.ServeJSON()
return
}
sort.Slice(userWithCodeforcesHandle,func(i,j int)bool{
p1,_ := models.GetProfiles(userWithCodeforcesHandle[i].ID)
p2,_ := models.GetProfiles(userWithCodeforcesHandle[i].ID)
rank1,_ := strconv.Atoi(p1.CodeforcesProfile.WorldRank)
rank2,_ := strconv.Atoi(p2.CodeforcesProfile.WorldRank)
return rank1>rank2
})

u.Data["json"] = userWithCodeforcesHandle
u.ServeJSON()
}
2 changes: 2 additions & 0 deletions controllers/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type UserController struct {
// @router /signup [post]
func (u *UserController) CreateUser() {
user, err := u.parseRequestBody()

if err != nil {
u.Ctx.ResponseWriter.WriteHeader(http.StatusBadRequest)
u.Data["json"] = BadInputError(err.Error())
Expand Down Expand Up @@ -229,6 +230,7 @@ func (u *UserController) Put() {
func (u *UserController) Login() {
username := u.Ctx.Request.FormValue("username")
password := u.Ctx.Request.FormValue("password")

user, err := models.AuthenticateUser(username, password)
if err == UserNotFoundError {
u.Data["json"] = map[string]string{"error": "invalid user credential"}
Expand Down
3 changes: 2 additions & 1 deletion models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"log"
"math/rand"
"time"

"github.com/astaxie/beego"
"github.com/getsentry/sentry-go"
"github.com/mdg-iitr/Codephile/services/firebase"
Expand Down Expand Up @@ -120,6 +120,7 @@ func AddUser(u types.User) (string, error) {
}
u.Password = string(hash)
err = collection.Collection.Insert(u)

if err != nil {
return "", UserAlreadyExistError
}
Expand Down
5 changes: 5 additions & 0 deletions routers/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ func init() {
&controllers.GraphController{},
),
),
beego.NSNamespace("/rank",
beego.NSInclude(
&controllers.RankController{},
),
),
)
beego.SetStaticPath("/static", "static")
beego.Router("/", &controllers.HomePageController{})
Expand Down