From 6940e44c9d6130997936caa2f30e37bbb166efb7 Mon Sep 17 00:00:00 2001 From: NanoNish Date: Fri, 23 Dec 2022 03:06:37 +0530 Subject: [PATCH 1/8] bfix: fixed search - changed query to autocomplete type - added a path var for defining the field to search in - added temp tests for search and checking connection with db - updated swagger to include path in search docs --- controllers/search.go | 15 +++- models/user.go | 14 ++-- swagger/swagger.json | 109 ++++++++++++++++++--------- swagger/swagger.yml | 168 +++++++++++++++++++++++++++++++----------- tests/default_test.go | 31 +++++++- 5 files changed, 243 insertions(+), 94 deletions(-) diff --git a/controllers/search.go b/controllers/search.go index ddba216..208f806 100644 --- a/controllers/search.go +++ b/controllers/search.go @@ -1,12 +1,13 @@ package controllers import ( - "github.com/getsentry/sentry-go" - . "github.com/mdg-iitr/Codephile/errors" - "github.com/mdg-iitr/Codephile/models" "log" "net/http" "strconv" + + "github.com/getsentry/sentry-go" + . "github.com/mdg-iitr/Codephile/errors" + "github.com/mdg-iitr/Codephile/models" ) // @Title Search @@ -14,6 +15,7 @@ import ( // @Security token_auth read:user // @Param count query string false "No of search objects to be returned" // @Param query query string true "Search query" +// @Param path query string false "Field to search in" // @Success 200 {object} []types.SearchDoc // @Failure 400 "search query too small" // @Failure 500 server_error @@ -32,7 +34,12 @@ func (u *UserController) Search() { if err != nil { c = 500 } - results, err := models.SearchUser(query, c) + path := u.GetString("path") + //TODO (nano): check for path to be one of the only pausible paths + if path == "" { + path = "username" + } + results, err := models.SearchUser(query, c, path) if err != nil { hub := sentry.GetHubFromContext(u.Ctx.Request.Context()) hub.CaptureException(err) diff --git a/models/user.go b/models/user.go index a9e8a38..819ca60 100644 --- a/models/user.go +++ b/models/user.go @@ -400,7 +400,7 @@ func UidExists(uid bson.ObjectId) (bool, error) { return false, nil } -//checks if the user is verified, returns error if user doesn't exists +// checks if the user is verified, returns error if user doesn't exists func IsUserVerified(uid bson.ObjectId) (bool, error, string) { sess := db.NewUserCollectionSession() defer sess.Close() @@ -453,20 +453,16 @@ func PasswordResetEmail(email string, hostName string, ctx context.Context) bool return true } -func SearchUser(query string, c int) ([]types.SearchDoc, error) { +func SearchUser(query string, c int, path string) ([]types.SearchDoc, error) { sess := db.NewUserCollectionSession() defer sess.Close() search := bson.M{ "$search": bson.M{ - "index": "name_search", - "text": bson.M{ + "index": "names", + "autocomplete": bson.M{ "query": query, - "path": bson.M{"wildcard": "*"}, - "fuzzy": bson.M{ - "maxEdits": 2, - "maxExpansions": 50, - }, + "path": path, }, }, } diff --git a/swagger/swagger.json b/swagger/swagger.json index 9545635..fd9fdbb 100644 --- a/swagger/swagger.json +++ b/swagger/swagger.json @@ -2,7 +2,7 @@ "swagger": "2.0", "info": { "title": "Codephile Official API", - "description": "Documentation for Codephile API", + "description": "Documentation for Codephile API\n", "version": "1.0.0", "contact": { "email": "mdg@iitr.ac.in" @@ -16,7 +16,7 @@ "tags": [ "contests" ], - "description": "displays all contests", + "description": "displays all contests\n\u003cbr\u003e", "operationId": "ContestController.GetContests", "responses": { "200": { @@ -43,7 +43,7 @@ "tags": [ "contests" ], - "description": "Returns the contests of a specific website", + "description": "Returns the contests of a specific website\n\u003cbr\u003e", "operationId": "ContestController.Get Particular Site's Contests", "parameters": [ { @@ -82,7 +82,7 @@ "tags": [ "feed" ], - "description": "Provides Data for contests in the Feed", + "description": "Provides Data for contests in the Feed\n\u003cbr\u003e", "operationId": "FeedController.ContestsFeed", "responses": { "200": { @@ -109,7 +109,7 @@ "tags": [ "feed" ], - "description": "Gives submission feed in paginated manner giving 100 submissions at a time", + "description": "Gives submission feed in paginated manner giving 100 submissions at a time\n\u003cbr\u003e", "operationId": "FeedController.FriendFeed", "parameters": [ { @@ -151,7 +151,7 @@ "tags": [ "feed" ], - "description": "Gives all submission feed of the user", + "description": "Gives all submission feed of the user\n\u003cbr\u003e", "operationId": "FeedController.All Feed", "responses": { "200": { @@ -181,7 +181,7 @@ "tags": [ "friends" ], - "description": "Compares the data of two users", + "description": "Compares the data of two users\n\u003cbr\u003e", "operationId": "FriendsController.CompareUser", "parameters": [ { @@ -220,7 +220,7 @@ "tags": [ "friends" ], - "description": "Adds the Following user's uid to the database", + "description": "Adds the Following user's uid to the database\n\u003cbr\u003e", "operationId": "FriendsController.FollowUser", "parameters": [ { @@ -256,7 +256,7 @@ "tags": [ "friends" ], - "description": "Fetches the users the user is following", + "description": "Fetches the users the user is following\n\u003cbr\u003e", "operationId": "FriendsController.GetFollowing", "responses": { "200": { @@ -286,7 +286,7 @@ "tags": [ "friends" ], - "description": "Un-follows the user with the given uid", + "description": "Un-follows the user with the given uid\n\u003cbr\u003e", "operationId": "FriendsController.Un-follow User", "parameters": [ { @@ -322,7 +322,7 @@ "tags": [ "graph" ], - "description": "Gives the activity graph for a user with given uid, (Logged-in user if uid is empty)", + "description": "Gives the activity graph for a user with given uid, (Logged-in user if uid is empty)\n\u003cbr\u003e", "operationId": "GraphController.Activity Graph", "parameters": [ { @@ -366,7 +366,7 @@ "tags": [ "graph" ], - "description": "Gives the count of the various different submissions of the user with a uid (Logged-in user if uid is empty)", + "description": "Gives the count of the various different submissions of the user with a uid (Logged-in user if uid is empty)\n\u003cbr\u003e", "operationId": "GraphController.Submissions Status", "parameters": [ { @@ -410,7 +410,7 @@ "tags": [ "submission" ], - "description": "Get all submissions of a user(logged-in if uid is empty) across various platforms", + "description": "Get all submissions of a user(logged-in if uid is empty) across various platforms\n\u003cbr\u003e", "operationId": "SubmissionController.All submissions", "parameters": [ { @@ -451,7 +451,7 @@ "tags": [ "submission" ], - "description": "Triggers saving of user's submissions across a particular platform into database", + "description": "Triggers saving of user's submissions across a particular platform into database\n\u003cbr\u003e", "operationId": "SubmissionController.Post", "parameters": [ { @@ -487,7 +487,7 @@ "tags": [ "submission" ], - "description": "Filter submissions of user on the basis of status, site and tags", + "description": "Filter submissions of user on the basis of status, site and tags\n\u003cbr\u003e", "operationId": "SubmissionController.Filter", "parameters": [ { @@ -544,7 +544,7 @@ "tags": [ "submission" ], - "description": "Get paginated submissions(100 per page) of user(logged-in if uid is empty) across various platforms", + "description": "Get paginated submissions(100 per page) of user(logged-in if uid is empty) across various platforms\n\u003cbr\u003e", "operationId": "SubmissionController.Get Submissions", "parameters": [ { @@ -592,7 +592,7 @@ "tags": [ "user" ], - "description": "update the logged in user", + "description": "update the logged in user\n\u003cbr\u003e", "operationId": "UserController.Update", "parameters": [ { @@ -636,6 +636,12 @@ "name": "handle.spoj", "description": "New Spoj Handle", "type": "string" + }, + { + "in": "formData", + "name": "handle.leetcode", + "description": "New Leetcode Handle", + "type": "string" } ], "responses": { @@ -675,7 +681,7 @@ "tags": [ "user" ], - "description": "get all Users", + "description": "get all Users\n\u003cbr\u003e", "operationId": "UserController.GetAll", "responses": { "200": { @@ -705,13 +711,20 @@ "tags": [ "user" ], - "description": "checks if username is available", - "operationId": "UserController.username available", + "description": "checks if email/username is available(email given pref, in case both params are present)\n\u003cbr\u003e", + "operationId": "UserController.Email/Username available", "parameters": [ + { + "in": "query", + "name": "email", + "description": "E-mail ID", + "required": true, + "type": "string" + }, { "in": "query", "name": "username", - "description": "Username", + "description": "username", "required": true, "type": "string" } @@ -741,7 +754,7 @@ "tags": [ "user" ], - "description": "Fetches user info from different websites and store them into the database", + "description": "Fetches user info from different websites and store them into the database\n\u003cbr\u003e", "operationId": "UserController.Fetch User Info", "parameters": [ { @@ -780,7 +793,7 @@ "tags": [ "user" ], - "description": "Returns info of user(logged in user if uid is empty) from different websites", + "description": "Returns info of user(logged in user if uid is empty) from different websites\n\u003cbr\u003e", "operationId": "UserController.Fetch All User Profiles And returns them", "parameters": [ { @@ -821,7 +834,7 @@ "tags": [ "user" ], - "description": "Filter users on basis of institute name", + "description": "Filter users on basis of institute name\n\u003cbr\u003e", "operationId": "UserController.Filter", "parameters": [ { @@ -863,7 +876,7 @@ "tags": [ "user" ], - "description": "Logs user into the system", + "description": "Logs user into the system\n\u003cbr\u003e", "operationId": "UserController.Login", "parameters": [ { @@ -899,7 +912,7 @@ "tags": [ "user" ], - "description": "Logs out current logged in user session", + "description": "Logs out current logged in user session\n\u003cbr\u003e", "operationId": "UserController.logout", "responses": { "200": { @@ -926,7 +939,7 @@ "tags": [ "user" ], - "description": "Changes password of the user", + "description": "Changes password of the user\n\u003cbr\u003e", "operationId": "UserController.Password Change", "parameters": [ { @@ -970,7 +983,7 @@ "tags": [ "user" ], - "description": "Sends an email to the user to reset the password", + "description": "Sends an email to the user to reset the password\n\u003cbr\u003e", "operationId": "UserController.Password-Reset-Email", "parameters": [ { @@ -996,7 +1009,7 @@ "tags": [ "user" ], - "description": "Resets the password of the user", + "description": "Resets the password of the user\n\u003cbr\u003e", "operationId": "UserController.Password-Reset-Form", "responses": { "200": { @@ -1013,7 +1026,7 @@ "tags": [ "user" ], - "description": "update the profile picture of logged in user", + "description": "update the profile picture of logged in user\n\u003cbr\u003e", "operationId": "UserController.Update Profile Pic", "parameters": [ { @@ -1049,7 +1062,7 @@ "tags": [ "user" ], - "description": "Endpoint to search users", + "description": "Endpoint to search users\n\u003cbr\u003e", "operationId": "UserController.Search", "parameters": [ { @@ -1064,6 +1077,12 @@ "description": "Search query", "required": true, "type": "string" + }, + { + "in": "query", + "name": "path", + "description": "Field to search in", + "type": "string" } ], "responses": { @@ -1104,7 +1123,7 @@ "tags": [ "user" ], - "description": "create users", + "description": "create users\n\u003cbr\u003e", "operationId": "UserController.CreateUser", "parameters": [ { @@ -1164,6 +1183,12 @@ "name": "handle.spoj", "description": "Spoj Handle", "type": "string" + }, + { + "in": "formData", + "name": "handle.leetcode", + "description": "Leetcode Handle", + "type": "string" } ], "responses": { @@ -1187,7 +1212,7 @@ "tags": [ "user" ], - "description": "verify user handles across different websites", + "description": "verify user handles across different websites\n\u003cbr\u003e", "operationId": "UserController.Verify site handles", "parameters": [ { @@ -1230,7 +1255,7 @@ "tags": [ "user" ], - "description": "Get user by uid. Returns logged in user if uid is empty", + "description": "Get user by uid. Returns logged in user if uid is empty\n\u003cbr\u003e", "operationId": "UserController.Get", "parameters": [ { @@ -1273,7 +1298,7 @@ "definitions": { "bson.ObjectId": { "title": "ObjectId", - "type": "object" + "type": "string" }, "types.ActivityGraph": { "title": "ActivityGraph", @@ -1295,6 +1320,9 @@ "hackerrankProfile": { "$ref": "#/definitions/types.ProfileInfo" }, + "leetcodeProfile": { + "$ref": "#/definitions/types.ProfileInfo" + }, "spojProfile": { "$ref": "#/definitions/types.ProfileInfo" } @@ -1390,6 +1418,9 @@ "hackerrank": { "type": "string" }, + "leetcode": { + "type": "string" + }, "spoj": { "type": "string" } @@ -1411,6 +1442,10 @@ "type": "string", "format": "datetime" }, + "Leetcode": { + "type": "string", + "format": "datetime" + }, "Spoj": { "type": "string", "format": "datetime" @@ -1520,6 +1555,10 @@ "type": "integer", "format": "int64" }, + "leetcode": { + "type": "integer", + "format": "int64" + }, "spoj": { "type": "integer", "format": "int64" diff --git a/swagger/swagger.yml b/swagger/swagger.yml index 543cb79..1b2f725 100644 --- a/swagger/swagger.yml +++ b/swagger/swagger.yml @@ -1,7 +1,8 @@ swagger: "2.0" info: title: Codephile Official API - description: Documentation for Codephile API + description: | + Documentation for Codephile API version: 1.0.0 contact: email: mdg@iitr.ac.in @@ -12,7 +13,9 @@ paths: get: tags: - contests - description: displays all contests + description: |- + displays all contests +
operationId: ContestController.GetContests responses: "200": @@ -28,7 +31,9 @@ paths: get: tags: - contests - description: Returns the contests of a specific website + description: |- + Returns the contests of a specific website +
operationId: ContestController.Get Particular Site's Contests parameters: - in: path @@ -52,7 +57,9 @@ paths: get: tags: - feed - description: Provides Data for contests in the Feed + description: |- + Provides Data for contests in the Feed +
operationId: FeedController.ContestsFeed responses: "200": @@ -68,8 +75,9 @@ paths: get: tags: - feed - description: Gives submission feed in paginated manner giving 100 submissions - at a time + description: |- + Gives submission feed in paginated manner giving 100 submissions at a time +
operationId: FeedController.FriendFeed parameters: - in: query @@ -96,7 +104,9 @@ paths: get: tags: - feed - description: Gives all submission feed of the user + description: |- + Gives all submission feed of the user +
operationId: FeedController.All Feed responses: "200": @@ -114,7 +124,9 @@ paths: get: tags: - friends - description: Compares the data of two users + description: |- + Compares the data of two users +
operationId: FriendsController.CompareUser parameters: - in: query @@ -138,7 +150,9 @@ paths: post: tags: - friends - description: Adds the Following user's uid to the database + description: |- + Adds the Following user's uid to the database +
operationId: FriendsController.FollowUser parameters: - in: query @@ -160,7 +174,9 @@ paths: get: tags: - friends - description: Fetches the users the user is following + description: |- + Fetches the users the user is following +
operationId: FriendsController.GetFollowing responses: "200": @@ -178,7 +194,9 @@ paths: post: tags: - friends - description: Un-follows the user with the given uid + description: |- + Un-follows the user with the given uid +
operationId: FriendsController.Un-follow User parameters: - in: query @@ -200,8 +218,9 @@ paths: get: tags: - graph - description: Gives the activity graph for a user with given uid, (Logged-in - user if uid is empty) + description: |- + Gives the activity graph for a user with given uid, (Logged-in user if uid is empty) +
operationId: GraphController.Activity Graph parameters: - in: path @@ -228,8 +247,9 @@ paths: get: tags: - graph - description: Gives the count of the various different submissions of the user - with a uid (Logged-in user if uid is empty) + description: |- + Gives the count of the various different submissions of the user with a uid (Logged-in user if uid is empty) +
operationId: GraphController.Submissions Status parameters: - in: path @@ -256,8 +276,9 @@ paths: post: tags: - submission - description: Triggers saving of user's submissions across a particular platform - into database + description: |- + Triggers saving of user's submissions across a particular platform into database +
operationId: SubmissionController.Post parameters: - in: path @@ -279,7 +300,9 @@ paths: get: tags: - submission - description: Filter submissions of user on the basis of status, site and tags + description: |- + Filter submissions of user on the basis of status, site and tags +
operationId: SubmissionController.Filter parameters: - in: path @@ -315,8 +338,9 @@ paths: get: tags: - submission - description: Get paginated submissions(100 per page) of user(logged-in if uid - is empty) across various platforms + description: |- + Get paginated submissions(100 per page) of user(logged-in if uid is empty) across various platforms +
operationId: SubmissionController.Get Submissions parameters: - in: path @@ -347,8 +371,9 @@ paths: get: tags: - submission - description: Get all submissions of a user(logged-in if uid is empty) across - various platforms + description: |- + Get all submissions of a user(logged-in if uid is empty) across various platforms +
operationId: SubmissionController.All submissions parameters: - in: path @@ -373,7 +398,9 @@ paths: put: tags: - user - description: update the logged in user + description: |- + update the logged in user +
operationId: UserController.Update parameters: - in: formData @@ -404,6 +431,10 @@ paths: name: handle.spoj description: New Spoj Handle type: string + - in: formData + name: handle.leetcode + description: New Leetcode Handle + type: string responses: "202": description: "" @@ -426,7 +457,9 @@ paths: get: tags: - user - description: Get user by uid. Returns logged in user if uid is empty + description: |- + Get user by uid. Returns logged in user if uid is empty +
operationId: UserController.Get parameters: - in: path @@ -453,7 +486,9 @@ paths: get: tags: - user - description: get all Users + description: |- + get all Users +
operationId: UserController.GetAll responses: "200": @@ -471,12 +506,19 @@ paths: get: tags: - user - description: checks if username is available - operationId: UserController.username available + description: |- + checks if email/username is available(email given pref, in case both params are present) +
+ operationId: UserController.Email/Username available parameters: + - in: query + name: email + description: E-mail ID + required: true + type: string - in: query name: username - description: Username + description: username required: true type: string responses: @@ -494,8 +536,9 @@ paths: post: tags: - user - description: Fetches user info from different websites and store them into the - database + description: |- + Fetches user info from different websites and store them into the database +
operationId: UserController.Fetch User Info parameters: - in: path @@ -519,8 +562,9 @@ paths: get: tags: - user - description: Returns info of user(logged in user if uid is empty) from different - websites + description: |- + Returns info of user(logged in user if uid is empty) from different websites +
operationId: UserController.Fetch All User Profiles And returns them parameters: - in: path @@ -545,7 +589,9 @@ paths: get: tags: - user - description: Filter users on basis of institute name + description: |- + Filter users on basis of institute name +
operationId: UserController.Filter parameters: - in: query @@ -571,7 +617,9 @@ paths: post: tags: - user - description: Logs user into the system + description: |- + Logs user into the system +
operationId: UserController.Login parameters: - in: formData @@ -595,7 +643,9 @@ paths: post: tags: - user - description: Logs out current logged in user session + description: |- + Logs out current logged in user session +
operationId: UserController.logout responses: "200": @@ -611,7 +661,9 @@ paths: post: tags: - user - description: Changes password of the user + description: |- + Changes password of the user +
operationId: UserController.Password Change parameters: - in: body @@ -638,7 +690,9 @@ paths: post: tags: - user - description: Sends an email to the user to reset the password + description: |- + Sends an email to the user to reset the password +
operationId: UserController.Password-Reset-Email parameters: - in: formData @@ -655,7 +709,9 @@ paths: post: tags: - user - description: Resets the password of the user + description: |- + Resets the password of the user +
operationId: UserController.Password-Reset-Form responses: "200": @@ -666,7 +722,9 @@ paths: put: tags: - user - description: update the profile picture of logged in user + description: |- + update the profile picture of logged in user +
operationId: UserController.Update Profile Pic parameters: - in: formData @@ -688,7 +746,9 @@ paths: get: tags: - user - description: Endpoint to search users + description: |- + Endpoint to search users +
operationId: UserController.Search parameters: - in: query @@ -700,6 +760,10 @@ paths: description: Search query required: true type: string + - in: query + name: path + description: Field to search in + type: string responses: "200": description: "" @@ -722,7 +786,9 @@ paths: post: tags: - user - description: create users + description: |- + create users +
operationId: UserController.CreateUser parameters: - in: formData @@ -765,6 +831,10 @@ paths: name: handle.spoj description: Spoj Handle type: string + - in: formData + name: handle.leetcode + description: Leetcode Handle + type: string responses: "201": description: '{int} types.User.Id' @@ -778,7 +848,9 @@ paths: get: tags: - user - description: verify user handles across different websites + description: |- + verify user handles across different websites +
operationId: UserController.Verify site handles parameters: - in: path @@ -804,7 +876,7 @@ paths: definitions: bson.ObjectId: title: ObjectId - type: object + type: string types.ActivityGraph: title: ActivityGraph type: array @@ -820,6 +892,8 @@ definitions: $ref: '#/definitions/types.ProfileInfo' hackerrankProfile: $ref: '#/definitions/types.ProfileInfo' + leetcodeProfile: + $ref: '#/definitions/types.ProfileInfo' spojProfile: $ref: '#/definitions/types.ProfileInfo' types.AllWorldRanks: @@ -884,6 +958,8 @@ definitions: type: string hackerrank: type: string + leetcode: + type: string spoj: type: string types.LastFetchedSubmission: @@ -899,6 +975,9 @@ definitions: Hackerrank: type: string format: datetime + Leetcode: + type: string + format: datetime Spoj: type: string format: datetime @@ -973,6 +1052,9 @@ definitions: hackerrank: type: integer format: int64 + leetcode: + type: integer + format: int64 spoj: type: integer format: int64 diff --git a/tests/default_test.go b/tests/default_test.go index c3b84d2..170305c 100644 --- a/tests/default_test.go +++ b/tests/default_test.go @@ -1,20 +1,24 @@ package test import ( + "fmt" + "github.com/globalsign/mgo/bson" "github.com/mdg-iitr/Codephile/conf" "github.com/mdg-iitr/Codephile/models/db" + "net/http" + "net/http/httptest" + "testing" + "github.com/astaxie/beego" _ "github.com/mdg-iitr/Codephile/conf" "github.com/mdg-iitr/Codephile/models" + _ "github.com/mdg-iitr/Codephile/models" "github.com/mdg-iitr/Codephile/models/types" _ "github.com/mdg-iitr/Codephile/routers" "github.com/mdg-iitr/Codephile/services/auth" . "github.com/smartystreets/goconvey/convey" - "net/http" - "net/http/httptest" - "testing" ) func init() { @@ -48,3 +52,24 @@ func TestGetAllUsers(t *testing.T) { }) }) } + +// A sample query to check if database is connected +func TestFind(t *testing.T) { + var user types.User + collection := db.NewUserCollectionSession() + defer collection.Close() + err := collection.Collection.Find(bson.M{"username": "nano_nish"}).Select(bson.M{"_id": 1, "username": 1, "email": 1, + "handle": 1, "lastfetched": 1, "profiles": 1, + "picture": 1, "fullname": 1, "institute": 1, "submissions": bson.M{"$slice": 5}}).One(&user) + // fmt.Println(err.Error()) + fmt.Print(user) + if err != nil { + // return nil, err + t.Error(err) + } +} + +// Test for Search +func TestSearch(t *testing.T) { + models.SearchUser("nishk", 10, "fullname") +} From 9093dbb85fa0375709a9f10438a550ccc028aa99 Mon Sep 17 00:00:00 2001 From: NanoNish Date: Fri, 23 Dec 2022 03:50:45 +0530 Subject: [PATCH 2/8] bfix: fixed search - added filter for path var --- controllers/search.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/controllers/search.go b/controllers/search.go index 208f806..08df5b9 100644 --- a/controllers/search.go +++ b/controllers/search.go @@ -35,8 +35,16 @@ func (u *UserController) Search() { c = 500 } path := u.GetString("path") - //TODO (nano): check for path to be one of the only pausible paths - if path == "" { + //Checking for possible fields, default is "username" + var possible_path []string + var good_path bool = false + possible_path = []string{"username", "fullname", "handle.codechef", "handle.codeforces", "handle.hackerearth", "handle.hackerrank", "handle.spoj"} + for _, itr := range possible_path { + if itr == path { + good_path = true + } + } + if good_path == false { path = "username" } results, err := models.SearchUser(query, c, path) From 38be732edfcf00da9975b6816d643c091e4e0eee Mon Sep 17 00:00:00 2001 From: NanoNish Date: Fri, 23 Dec 2022 04:00:14 +0530 Subject: [PATCH 3/8] rfac: made changes to pass Lint ci tests --- controllers/search.go | 2 +- tests/default_test.go | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/controllers/search.go b/controllers/search.go index 08df5b9..ad166fb 100644 --- a/controllers/search.go +++ b/controllers/search.go @@ -44,7 +44,7 @@ func (u *UserController) Search() { good_path = true } } - if good_path == false { + if !good_path { path = "username" } results, err := models.SearchUser(query, c, path) diff --git a/tests/default_test.go b/tests/default_test.go index 170305c..25a6047 100644 --- a/tests/default_test.go +++ b/tests/default_test.go @@ -71,5 +71,9 @@ func TestFind(t *testing.T) { // Test for Search func TestSearch(t *testing.T) { - models.SearchUser("nishk", 10, "fullname") + _, err := models.SearchUser("nishk", 10, "fullname") + if err != nil { + // return nil, err + t.Error(err) + } } From e1d400f5c7aa369c9ed9a0fd64e86444a390d16a Mon Sep 17 00:00:00 2001 From: NanoNish Date: Fri, 23 Dec 2022 04:04:38 +0530 Subject: [PATCH 4/8] rfac: removed TestSearch from tests for passing ci build test --- tests/default_test.go | 9 --------- 1 file changed, 9 deletions(-) diff --git a/tests/default_test.go b/tests/default_test.go index 25a6047..c4c274e 100644 --- a/tests/default_test.go +++ b/tests/default_test.go @@ -68,12 +68,3 @@ func TestFind(t *testing.T) { t.Error(err) } } - -// Test for Search -func TestSearch(t *testing.T) { - _, err := models.SearchUser("nishk", 10, "fullname") - if err != nil { - // return nil, err - t.Error(err) - } -} From af0dded9703a0f333f5d62ad1b7ed60aa1370788 Mon Sep 17 00:00:00 2001 From: NanoNish Date: Fri, 23 Dec 2022 04:07:38 +0530 Subject: [PATCH 5/8] rfac: removed TestFind from tests for passing ci build test --- tests/default_test.go | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/tests/default_test.go b/tests/default_test.go index c4c274e..95559a3 100644 --- a/tests/default_test.go +++ b/tests/default_test.go @@ -1,8 +1,6 @@ package test import ( - "fmt" - "github.com/globalsign/mgo/bson" "github.com/mdg-iitr/Codephile/conf" "github.com/mdg-iitr/Codephile/models/db" @@ -52,19 +50,3 @@ func TestGetAllUsers(t *testing.T) { }) }) } - -// A sample query to check if database is connected -func TestFind(t *testing.T) { - var user types.User - collection := db.NewUserCollectionSession() - defer collection.Close() - err := collection.Collection.Find(bson.M{"username": "nano_nish"}).Select(bson.M{"_id": 1, "username": 1, "email": 1, - "handle": 1, "lastfetched": 1, "profiles": 1, - "picture": 1, "fullname": 1, "institute": 1, "submissions": bson.M{"$slice": 5}}).One(&user) - // fmt.Println(err.Error()) - fmt.Print(user) - if err != nil { - // return nil, err - t.Error(err) - } -} From 8934f5df631a2a8950ad65713a76f1324394fe55 Mon Sep 17 00:00:00 2001 From: NanoNish Date: Sat, 24 Dec 2022 08:38:40 +0530 Subject: [PATCH 6/8] rfac: changed logic for checking valid paths - added IsPathValid to constants.go - removed logic from search.go in controllers --- conf/constants.go | 11 +++++++++++ controllers/search.go | 12 +++--------- routers/router.go | 7 ++++--- 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/conf/constants.go b/conf/constants.go index a9de25c..8f04c2d 100644 --- a/conf/constants.go +++ b/conf/constants.go @@ -58,3 +58,14 @@ const ( StatusMemoryLimitExceeded = "MLE" StatusPartial = "PTL" ) + +var ValidPaths = []string{"username", "fullname", "handle.codechef", "handle.codeforces", "handle.hackerearth", "handle.hackerrank", "handle.spoj"} + +func IsPathValid(s string) bool { + for _, path := range ValidPaths { + if path == s { + return true + } + } + return false +} diff --git a/controllers/search.go b/controllers/search.go index ad166fb..f9370f6 100644 --- a/controllers/search.go +++ b/controllers/search.go @@ -6,6 +6,7 @@ import ( "strconv" "github.com/getsentry/sentry-go" + . "github.com/mdg-iitr/Codephile/conf" . "github.com/mdg-iitr/Codephile/errors" "github.com/mdg-iitr/Codephile/models" ) @@ -36,16 +37,9 @@ func (u *UserController) Search() { } path := u.GetString("path") //Checking for possible fields, default is "username" - var possible_path []string - var good_path bool = false - possible_path = []string{"username", "fullname", "handle.codechef", "handle.codeforces", "handle.hackerearth", "handle.hackerrank", "handle.spoj"} - for _, itr := range possible_path { - if itr == path { - good_path = true - } - } - if !good_path { + if !IsPathValid(path) { path = "username" + return } results, err := models.SearchUser(query, c, path) if err != nil { diff --git a/routers/router.go b/routers/router.go index ccb914a..8980e60 100644 --- a/routers/router.go +++ b/routers/router.go @@ -9,13 +9,14 @@ package routers import ( + "net/http" + "os" + "path" + "github.com/astaxie/beego" "github.com/astaxie/beego/context" "github.com/mdg-iitr/Codephile/controllers" "github.com/mdg-iitr/Codephile/middleware" - "net/http" - "os" - "path" ) func init() { From 0970bff0fcff307a8266c226c8f11d6a0badc3a5 Mon Sep 17 00:00:00 2001 From: NanoNish Date: Sat, 24 Dec 2022 08:52:26 +0530 Subject: [PATCH 7/8] Revert "rfac: changed logic for checking valid paths" This reverts commit 8934f5df631a2a8950ad65713a76f1324394fe55. --- conf/constants.go | 11 ----------- controllers/search.go | 12 +++++++++--- routers/router.go | 7 +++---- 3 files changed, 12 insertions(+), 18 deletions(-) diff --git a/conf/constants.go b/conf/constants.go index 8f04c2d..a9de25c 100644 --- a/conf/constants.go +++ b/conf/constants.go @@ -58,14 +58,3 @@ const ( StatusMemoryLimitExceeded = "MLE" StatusPartial = "PTL" ) - -var ValidPaths = []string{"username", "fullname", "handle.codechef", "handle.codeforces", "handle.hackerearth", "handle.hackerrank", "handle.spoj"} - -func IsPathValid(s string) bool { - for _, path := range ValidPaths { - if path == s { - return true - } - } - return false -} diff --git a/controllers/search.go b/controllers/search.go index f9370f6..ad166fb 100644 --- a/controllers/search.go +++ b/controllers/search.go @@ -6,7 +6,6 @@ import ( "strconv" "github.com/getsentry/sentry-go" - . "github.com/mdg-iitr/Codephile/conf" . "github.com/mdg-iitr/Codephile/errors" "github.com/mdg-iitr/Codephile/models" ) @@ -37,9 +36,16 @@ func (u *UserController) Search() { } path := u.GetString("path") //Checking for possible fields, default is "username" - if !IsPathValid(path) { + var possible_path []string + var good_path bool = false + possible_path = []string{"username", "fullname", "handle.codechef", "handle.codeforces", "handle.hackerearth", "handle.hackerrank", "handle.spoj"} + for _, itr := range possible_path { + if itr == path { + good_path = true + } + } + if !good_path { path = "username" - return } results, err := models.SearchUser(query, c, path) if err != nil { diff --git a/routers/router.go b/routers/router.go index 8980e60..ccb914a 100644 --- a/routers/router.go +++ b/routers/router.go @@ -9,14 +9,13 @@ package routers import ( - "net/http" - "os" - "path" - "github.com/astaxie/beego" "github.com/astaxie/beego/context" "github.com/mdg-iitr/Codephile/controllers" "github.com/mdg-iitr/Codephile/middleware" + "net/http" + "os" + "path" ) func init() { From fbfd641d8154d3f679961badc8f8b371edee5464 Mon Sep 17 00:00:00 2001 From: NanoNish Date: Sat, 24 Dec 2022 09:04:15 +0530 Subject: [PATCH 8/8] rfac: changed logic for checking valid paths - added IsPathValid to constants.go - removed logic from search.go in controllers --- conf/constants.go | 11 +++++++++++ controllers/search.go | 11 ++--------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/conf/constants.go b/conf/constants.go index a9de25c..8f04c2d 100644 --- a/conf/constants.go +++ b/conf/constants.go @@ -58,3 +58,14 @@ const ( StatusMemoryLimitExceeded = "MLE" StatusPartial = "PTL" ) + +var ValidPaths = []string{"username", "fullname", "handle.codechef", "handle.codeforces", "handle.hackerearth", "handle.hackerrank", "handle.spoj"} + +func IsPathValid(s string) bool { + for _, path := range ValidPaths { + if path == s { + return true + } + } + return false +} diff --git a/controllers/search.go b/controllers/search.go index ad166fb..62c51e5 100644 --- a/controllers/search.go +++ b/controllers/search.go @@ -6,6 +6,7 @@ import ( "strconv" "github.com/getsentry/sentry-go" + . "github.com/mdg-iitr/Codephile/conf" . "github.com/mdg-iitr/Codephile/errors" "github.com/mdg-iitr/Codephile/models" ) @@ -36,15 +37,7 @@ func (u *UserController) Search() { } path := u.GetString("path") //Checking for possible fields, default is "username" - var possible_path []string - var good_path bool = false - possible_path = []string{"username", "fullname", "handle.codechef", "handle.codeforces", "handle.hackerearth", "handle.hackerrank", "handle.spoj"} - for _, itr := range possible_path { - if itr == path { - good_path = true - } - } - if !good_path { + if !IsPathValid(path) { path = "username" } results, err := models.SearchUser(query, c, path)