diff --git a/sources/pages/contributors/bookmarks.go b/sources/pages/contributors/bookmarks.go index 534fd1f..92654a6 100644 --- a/sources/pages/contributors/bookmarks.go +++ b/sources/pages/contributors/bookmarks.go @@ -14,13 +14,13 @@ import ( "techpro.club/sources/users" ) -type FinalBookmarksOutputStruct struct{ - Projects []common.FeedStruct `json:"projects"` - UserNameImage common.UsernameImageStruct `json:"usernameImage"` - MyReactions []primitive.ObjectID `json:"myReactions"` - NotificaitonsCount int64 `json:"notificationsCount"` - NotificationsList []common.MainNotificationStruct `json:"nofiticationsList"` - PageDetails common.PageDetails `json:"pageDetails"` +type FinalBookmarksOutputStruct struct { + Projects []common.FeedStruct `json:"projects"` + UserNameImage common.UsernameImageStruct `json:"usernameImage"` + MyReactions []primitive.ObjectID `json:"myReactions"` + NotificaitonsCount int64 `json:"notificationsCount"` + NotificationsList []common.MainNotificationStruct `json:"nofiticationsList"` + PageDetails common.PageDetails `json:"pageDetails"` } // Fetched bookmarked projects @@ -32,14 +32,14 @@ func FetchBookmarks(w http.ResponseWriter, r *http.Request) { // Session check sessionOk, userID := users.ValidateDbSession(w, r) - if(!sessionOk){ - + if !sessionOk { + // Delete cookies users.DeleteSessionCookie(w, r) users.DeleteUserCookie(w, r) http.Redirect(w, r, "/", http.StatusSeeOther) - } + } var userNameImage common.UsernameImageStruct @@ -49,16 +49,16 @@ func FetchBookmarks(w http.ResponseWriter, r *http.Request) { // Fetch notificaitons _, _, notificationsCount, notificationsList := pages.NotificationsCountAndTopFive(userID) - if(!status){ + if !status { log.Println(msg) } else { - userNameImage = common.UsernameImageStruct{userName,image} + userNameImage = common.UsernameImageStruct{Username: userName, Image: image} } var functions = template.FuncMap{ - "objectIdToString" : pages.ObjectIDToString, - "containsObjectId" : pages.ContainsObjectID, - "timeElapsed" : pages.TimeElapsed, + "objectIdToString": pages.ObjectIDToString, + "containsObjectId": pages.ContainsObjectID, + "timeElapsed": pages.TimeElapsed, } // constants for check @@ -70,39 +70,38 @@ func FetchBookmarks(w http.ResponseWriter, r *http.Request) { _, _, _, reactions := pages.FetchMyBookmarksAndReactions(userID) baseUrl := common.GetBaseurl() + common.CONST_APP_PORT - pageDetails := common.PageDetails{BaseUrl: baseUrl, Title : "Bookmarks"} + pageDetails := common.PageDetails{BaseUrl: baseUrl, Title: "Bookmarks"} output := FinalBookmarksOutputStruct{results, userNameImage, reactions, notificationsCount, notificationsList, pageDetails} tmpl, err := template.New("").Funcs(functions).ParseFiles("templates/app/common/base.gohtml", "templates/app/common/contributormenu.gohtml", "templates/app/contributors/bookmarks.gohtml") if err != nil { fmt.Println(err.Error()) - }else { - tmpl.ExecuteTemplate(w, "base", output) + } else { + tmpl.ExecuteTemplate(w, "base", output) } } // Fetch all bookmarked projects -func filterBookmarkedProjects(pageid int64, userID primitive.ObjectID)(status bool, msg string, results []primitive.ObjectID){ - +func filterBookmarkedProjects(pageid int64, userID primitive.ObjectID) (status bool, msg string, results []primitive.ObjectID) { status = false msg = "" var out common.FetchUserProjectBookmarkStruct - status, msg, client := common.Mongoconnect() + status, msg, client := common.Mongoconnect() defer client.Disconnect(context.TODO()) dbName := common.GetMoDb() fetchBookmarkedProjects := client.Database(dbName).Collection(common.CONST_MO_BOOKMARKS) // Fetch all bookmarked projects against a user - bookmarkedProjectsResults, err:= fetchBookmarkedProjects.Find(context.TODO(), bson.M{"userid": userID}) + bookmarkedProjectsResults, err := fetchBookmarkedProjects.Find(context.TODO(), bson.M{"userid": userID}) - if err != nil{ + if err != nil { msg = err.Error() } else { - for bookmarkedProjectsResults.Next(context.TODO()){ + for bookmarkedProjectsResults.Next(context.TODO()) { errDecode := bookmarkedProjectsResults.Decode(&out) if errDecode != nil { @@ -116,10 +115,8 @@ func filterBookmarkedProjects(pageid int64, userID primitive.ObjectID)(status bo return status, msg, results } - // Filter all active projects from the database according to users bookmarks -func fetchBookmarkedProjectsList(pageid int64, userID primitive.ObjectID)(status bool, msg string, results []common.FeedStruct){ - +func fetchBookmarkedProjectsList(pageid int64, userID primitive.ObjectID) (status bool, msg string, results []common.FeedStruct) { status = false msg = "" @@ -127,7 +124,7 @@ func fetchBookmarkedProjectsList(pageid int64, userID primitive.ObjectID)(status var finalConditions []bson.M resultsPerPage := int64(20) - status, msg, client := common.Mongoconnect() + status, msg, client := common.Mongoconnect() defer client.Disconnect(context.TODO()) dbName := common.GetMoDb() @@ -137,54 +134,53 @@ func fetchBookmarkedProjectsList(pageid int64, userID primitive.ObjectID)(status status, errMsg, projectIds := filterBookmarkedProjects(pageid, userID) - if !status{ + if !status { msg = errMsg } else { - if len(projectIds) <= 0{ + if len(projectIds) <= 0 { msg = "No projects found" - results = []common.FeedStruct{} + results = []common.FeedStruct{} } else { - finalConditions = append(finalConditions, bson.M{"_id" : bson.M{"$in": projectIds}}) - - aggCondition := bson.M{"$match": bson.M{"$and" : finalConditions}} - - + finalConditions = append(finalConditions, bson.M{"_id": bson.M{"$in": projectIds}}) + + aggCondition := bson.M{"$match": bson.M{"$and": finalConditions}} + // Filter joins aggLookup := bson.M{"$lookup": bson.M{ - "from": common.CONST_MO_USERS, // the collection name - "localField": "userid", // the field on the child struct - "foreignField": "_id", // the field on the parent struct - "as": "userdetails", // the field to populate into + "from": common.CONST_MO_USERS, // the collection name + "localField": "userid", // the field on the child struct + "foreignField": "_id", // the field on the parent struct + "as": "userdetails", // the field to populate into }} - + // Set projections - aggProjections := bson.M{"$project": bson.M{ - "_id": 1, "projectname" : 1, - "projectdescription" : 1, - "repolink": 1, - "languages": 1, - "otherlanguages": 1, - "allied": 1, - "company" : 1, - "companyname": 1, - "createddate": 1, - "public" : 1, - "reactionscount": 1, - "userdetails" : bson.M{ "_id" : 1, "name": 1, "imagelink" :1}, + aggProjections := bson.M{"$project": bson.M{ + "_id": 1, "projectname": 1, + "projectdescription": 1, + "repolink": 1, + "languages": 1, + "otherlanguages": 1, + "allied": 1, + "company": 1, + "companyname": 1, + "createddate": 1, + "public": 1, + "reactionscount": 1, + "userdetails": bson.M{"_id": 1, "name": 1, "imagelink": 1}, }} - + aggSkip := bson.M{"$skip": (pageid * resultsPerPage)} aggLimit := bson.M{"$limit": resultsPerPage} - + projectsList, err := fetchProjects.Aggregate(context.TODO(), []bson.M{aggCondition, aggLookup, aggProjections, aggSkip, aggLimit}) - + if err != nil { msg = err.Error() } else { - for projectsList.Next(context.TODO()){ + for projectsList.Next(context.TODO()) { var elem common.FeedStruct errDecode := projectsList.Decode(&elem) - + if errDecode != nil { msg = errDecode.Error() } else { @@ -199,4 +195,3 @@ func fetchBookmarkedProjectsList(pageid int64, userID primitive.ObjectID)(status return status, msg, results } - diff --git a/sources/pages/contributors/feeds.go b/sources/pages/contributors/feeds.go index 6630941..c3bcf4b 100644 --- a/sources/pages/contributors/feeds.go +++ b/sources/pages/contributors/feeds.go @@ -13,26 +13,27 @@ import ( "techpro.club/sources/pages" "techpro.club/sources/users" ) -type FinalFeedsOutputStruct struct{ - Projects []common.FeedStruct `json:"projects"` - UserNameImage common.UsernameImageStruct `json:"usernameImage"` - MyBookmarks []primitive.ObjectID `json:"myBookmarks"` - MyReactions []primitive.ObjectID `json:"myReactions"` - NotificaitonsCount int64 `json:"notificationsCount"` - NotificationsList []common.MainNotificationStruct `json:"nofiticationsList"` - PageDetails common.PageDetails `json:"pageDetails"` + +type FinalFeedsOutputStruct struct { + Projects []common.FeedStruct `json:"projects"` + UserNameImage common.UsernameImageStruct `json:"usernameImage"` + MyBookmarks []primitive.ObjectID `json:"myBookmarks"` + MyReactions []primitive.ObjectID `json:"myReactions"` + NotificaitonsCount int64 `json:"notificationsCount"` + NotificationsList []common.MainNotificationStruct `json:"nofiticationsList"` + PageDetails common.PageDetails `json:"pageDetails"` } -func Feeds(w http.ResponseWriter, r *http.Request){ +func Feeds(w http.ResponseWriter, r *http.Request) { if r.URL.Path != "/contributors/feeds" { - pages.ErrorHandler(w, r, http.StatusNotFound) - return - } + pages.ErrorHandler(w, r, http.StatusNotFound) + return + } // Session check sessionOk, userID := users.ValidateDbSession(w, r) - if(!sessionOk){ - + if !sessionOk { + // Delete cookies users.DeleteSessionCookie(w, r) users.DeleteUserCookie(w, r) @@ -48,18 +49,18 @@ func Feeds(w http.ResponseWriter, r *http.Request){ // Fetch notificaitons _, _, notificationsCount, notificationsList := pages.NotificationsCountAndTopFive(userID) - if(!status){ + if !status { log.Println(msg) } else { - userNameImage = common.UsernameImageStruct{userName,image} + userNameImage = common.UsernameImageStruct{Username: userName, Image: image} } var functions = template.FuncMap{ - "objectIdToString" : pages.ObjectIDToString, - "containsObjectId" : pages.ContainsObjectID, - "timeElapsed" : pages.TimeElapsed, + "objectIdToString": pages.ObjectIDToString, + "containsObjectId": pages.ContainsObjectID, + "timeElapsed": pages.TimeElapsed, } - + // TEST CONDITIONS // This has to come from the actual frontend pageid := int64(0) @@ -69,9 +70,8 @@ func Feeds(w http.ResponseWriter, r *http.Request){ _, _, results := filterActiveProjects(pageid, tags, keyword) _, _, bookmarks, reactions := pages.FetchMyBookmarksAndReactions(userID) - baseUrl := common.GetBaseurl() + common.CONST_APP_PORT - pageDetails := common.PageDetails{BaseUrl: baseUrl, Title : "Feeds"} + pageDetails := common.PageDetails{BaseUrl: baseUrl, Title: "Feeds"} output := FinalFeedsOutputStruct{results, userNameImage, bookmarks, reactions, notificationsCount, notificationsList, pageDetails} @@ -79,16 +79,14 @@ func Feeds(w http.ResponseWriter, r *http.Request){ if err != nil { fmt.Println(err.Error()) - }else { - tmpl.ExecuteTemplate(w, "base", output) + } else { + tmpl.ExecuteTemplate(w, "base", output) } } - // Filter all active projects from the database according to filters -func filterActiveProjects(pageid int64, tags []string, keyword string)(status bool, msg string, results []common.FeedStruct){ - +func filterActiveProjects(pageid int64, tags []string, keyword string) (status bool, msg string, results []common.FeedStruct) { status = false msg = "" @@ -97,7 +95,7 @@ func filterActiveProjects(pageid int64, tags []string, keyword string)(status bo var finalConditions []bson.M resultsPerPage := int64(20) - status, msg, client := common.Mongoconnect() + status, msg, client := common.Mongoconnect() defer client.Disconnect(context.TODO()) dbName := common.GetMoDb() @@ -106,7 +104,7 @@ func filterActiveProjects(pageid int64, tags []string, keyword string)(status bo finalConditions = append(finalConditions, bson.M{"isactive": bson.M{"$eq": common.CONST_ACTIVE}}) if len(keyword) > 0 { - finalConditions = append(finalConditions, bson.M{"projectname" : bson.M{"$regex": keyword}}) + finalConditions = append(finalConditions, bson.M{"projectname": bson.M{"$regex": keyword}}) } // Filter where conditions @@ -115,46 +113,44 @@ func filterActiveProjects(pageid int64, tags []string, keyword string)(status bo orConditions = append(orConditions, bson.M{"otherlanguages": bson.M{"$in": tags}}) orConditions = append(orConditions, bson.M{"allied": bson.M{"$in": tags}}) - finalConditions = append(finalConditions, bson.M{"$or" : orConditions}) + finalConditions = append(finalConditions, bson.M{"$or": orConditions}) } - - - aggCondition := bson.M{"$match": bson.M{"$and" : finalConditions}} + aggCondition := bson.M{"$match": bson.M{"$and": finalConditions}} // Filter joins aggLookup := bson.M{"$lookup": bson.M{ - "from": common.CONST_MO_USERS, // the collection name - "localField": "userid", // the field on the child struct - "foreignField": "_id", // the field on the parent struct - "as": "userdetails", // the field to populate into + "from": common.CONST_MO_USERS, // the collection name + "localField": "userid", // the field on the child struct + "foreignField": "_id", // the field on the parent struct + "as": "userdetails", // the field to populate into }} // Set projections - aggProjections := bson.M{"$project": bson.M{ - "_id": 1, "projectname" : 1, - "projectdescription" : 1, - "repolink": 1, - "languages": 1, - "otherlanguages": 1, - "allied": 1, - "company" : 1, - "companyname": 1, - "createddate": 1, - "reactionscount": 1, - "public" : 1, - "userdetails" : bson.M{ "_id" : 1, "name": 1, "imagelink" :1}, + aggProjections := bson.M{"$project": bson.M{ + "_id": 1, "projectname": 1, + "projectdescription": 1, + "repolink": 1, + "languages": 1, + "otherlanguages": 1, + "allied": 1, + "company": 1, + "companyname": 1, + "createddate": 1, + "reactionscount": 1, + "public": 1, + "userdetails": bson.M{"_id": 1, "name": 1, "imagelink": 1}, }} aggSkip := bson.M{"$skip": (pageid * resultsPerPage)} - aggLimit := bson.M{"$limit": resultsPerPage} + aggLimit := bson.M{"$limit": resultsPerPage} projectsList, err := fetchProjects.Aggregate(context.TODO(), []bson.M{aggCondition, aggLookup, aggProjections, aggSkip, aggLimit}) if err != nil { msg = err.Error() } else { - for projectsList.Next(context.TODO()){ + for projectsList.Next(context.TODO()) { var elem common.FeedStruct errDecode := projectsList.Decode(&elem) diff --git a/sources/pages/contributors/preferences.go b/sources/pages/contributors/preferences.go index 8e0ac7f..643d237 100644 --- a/sources/pages/contributors/preferences.go +++ b/sources/pages/contributors/preferences.go @@ -15,28 +15,29 @@ import ( "techpro.club/sources/pages" "techpro.club/sources/users" ) -type FinalPreferencesOutStruct struct{ - ProgrammingLanguages map[string]string `json:"programmingLanguages"` - AlliedServices map[string]string `json:"alliedServices"` - ProjectType map[string]string `json:"projectType"` - Contributors map[string]string `json:"contributors"` + +type FinalPreferencesOutStruct struct { + ProgrammingLanguages map[string]string `json:"programmingLanguages"` + AlliedServices map[string]string `json:"alliedServices"` + ProjectType map[string]string `json:"projectType"` + Contributors map[string]string `json:"contributors"` ContributorPreferences common.SaveContributorPreferencesStruct `json:"contributorPreferences"` - UserNameImage common.UsernameImageStruct `json:"userNameImage"` - NotificaitonsCount int64 `json:"notificationsCount"` - NotificationsList []common.MainNotificationStruct `json:"nofiticationsList"` - PageDetails common.PageDetails `json:"pageDetails"` + UserNameImage common.UsernameImageStruct `json:"userNameImage"` + NotificaitonsCount int64 `json:"notificationsCount"` + NotificationsList []common.MainNotificationStruct `json:"nofiticationsList"` + PageDetails common.PageDetails `json:"pageDetails"` } -func Preferences(w http.ResponseWriter, r *http.Request){ - +func Preferences(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/contributors/preferences" { - pages.ErrorHandler(w, r, http.StatusNotFound) - return - } + pages.ErrorHandler(w, r, http.StatusNotFound) + return + } // Session check sessionOk, userID := users.ValidateDbSession(w, r) - if(!sessionOk){ + if !sessionOk { // Delete cookies users.DeleteSessionCookie(w, r) @@ -46,8 +47,8 @@ func Preferences(w http.ResponseWriter, r *http.Request){ } var functions = template.FuncMap{ - "contains" : pages.Contains, - "sliceToCsv" : pages.SliceToCsv, + "contains": pages.Contains, + "sliceToCsv": pages.SliceToCsv, } var userNameImage common.UsernameImageStruct @@ -58,16 +59,16 @@ func Preferences(w http.ResponseWriter, r *http.Request){ // Fetch notificaitons _, _, notificationsCount, notificationsList := pages.NotificationsCountAndTopFive(userID) - if(!status){ + if !status { log.Println(msg) } else { - userNameImage = common.UsernameImageStruct{userName,image} + userNameImage = common.UsernameImageStruct{Username: userName, Image: image} } baseUrl := common.GetBaseurl() + common.CONST_APP_PORT - pageDetails := common.PageDetails{BaseUrl: baseUrl, Title : "Project Preferences"} + pageDetails := common.PageDetails{BaseUrl: baseUrl, Title: "Project Preferences"} - if r.Method == "GET"{ + if r.Method == "GET" { _, _, preferences := fetchPreferences(userID) finalPreferencesOutStruct := FinalPreferencesOutStruct{ @@ -77,7 +78,7 @@ func Preferences(w http.ResponseWriter, r *http.Request){ common.Contributors, preferences, userNameImage, - notificationsCount, + notificationsCount, notificationsList, pageDetails, } @@ -85,13 +86,13 @@ func Preferences(w http.ResponseWriter, r *http.Request){ tmpl, err := template.New("").Funcs(functions).ParseFiles("templates/app/common/base.gohtml", "templates/app/common/contributormenu.gohtml", "templates/app/contributors/preferences.gohtml") if err != nil { fmt.Println(err.Error()) - }else { - tmpl.ExecuteTemplate(w, "base", finalPreferencesOutStruct) + } else { + tmpl.ExecuteTemplate(w, "base", finalPreferencesOutStruct) } } else { - + status, msg := savePreferences(w, r, userID) - if !status{ + if !status { fmt.Println(msg) } else { http.Redirect(w, r, "/contributors/thankyou", http.StatusOK) @@ -100,7 +101,7 @@ func Preferences(w http.ResponseWriter, r *http.Request){ } // Return contributor preferences, if already saved -func fetchPreferences(userID primitive.ObjectID) (status bool, msg string, preferences common.SaveContributorPreferencesStruct){ +func fetchPreferences(userID primitive.ObjectID) (status bool, msg string, preferences common.SaveContributorPreferencesStruct) { status = false msg = "" @@ -109,13 +110,13 @@ func fetchPreferences(userID primitive.ObjectID) (status bool, msg string, prefe dbName := common.GetMoDb() fetchPreferences := client.Database(dbName).Collection(common.CONST_MO_CONTRIBUTOR_PREFERENCES) - results, err := fetchPreferences.Find(context.TODO(), bson.M{"userid": userID}, options.Find().SetProjection(bson.M{"_id": 0})) + results, err := fetchPreferences.Find(context.TODO(), bson.M{"userid": userID}, options.Find().SetProjection(bson.M{"_id": 0})) if err != nil { msg = err.Error() status = false } else { - + for results.Next(context.TODO()) { errDecode := results.Decode(&preferences) @@ -124,7 +125,7 @@ func fetchPreferences(userID primitive.ObjectID) (status bool, msg string, prefe msg = errDecode.Error() status = false } else { - msg= "Success" + msg = "Success" status = true } } @@ -134,11 +135,10 @@ func fetchPreferences(userID primitive.ObjectID) (status bool, msg string, prefe } // Save preferences for contributor -func savePreferences(w http.ResponseWriter, r *http.Request, userID primitive.ObjectID) (status bool, msg string){ +func savePreferences(w http.ResponseWriter, r *http.Request, userID primitive.ObjectID) (status bool, msg string) { status = false msg = "" - errParse := r.ParseForm() if errParse != nil { msg = errParse.Error() @@ -149,14 +149,22 @@ func savePreferences(w http.ResponseWriter, r *http.Request, userID primitive.Ob notificationFrequency := r.Form.Get("emailFrequency") projectType := r.Form["pType"] contributorCount := r.Form.Get("contributorCount") - paidJob := r.Form.Get("paidJob") + paidJob := r.Form.Get("paidJob") relocation := r.Form.Get("relocation") qualification := r.Form.Get("qualification") - otherLanguagesSplit := strings.Split(otherLanguages, ",") - result := common.SaveContributorPreferencesStruct{userID, languages, otherLanguagesSplit, allied, projectType, notificationFrequency, contributorCount, paidJob, relocation, qualification} + result := common.SaveContributorPreferencesStruct{UserID: userID, + Languages: languages, + OtherLanguages: otherLanguagesSplit, + Allied: allied, + ProjectType: projectType, + NotificationFrequency: notificationFrequency, + ContributorCount: contributorCount, + PaidJob: paidJob, + Relocation: relocation, + Qualification: qualification} _, _, client := common.Mongoconnect() defer client.Disconnect(context.TODO()) @@ -175,4 +183,4 @@ func savePreferences(w http.ResponseWriter, r *http.Request, userID primitive.Ob } return status, msg -} \ No newline at end of file +} diff --git a/sources/pages/contributors/preferencessaved.go b/sources/pages/contributors/preferencessaved.go index f61b2ea..e3cc2a3 100644 --- a/sources/pages/contributors/preferencessaved.go +++ b/sources/pages/contributors/preferencessaved.go @@ -11,24 +11,24 @@ import ( "techpro.club/sources/users" ) -type FinalPreferenceSavedOutputStruct struct{ - UserNameImage common.UsernameImageStruct `json:"usernameImage"` - NotificaitonsCount int64 `json:"notificationsCount"` - NotificationsList []common.MainNotificationStruct `json:"nofiticationsList"` - PageDetails common.PageDetails `json:"pageDetails"` +type FinalPreferenceSavedOutputStruct struct { + UserNameImage common.UsernameImageStruct `json:"usernameImage"` + NotificaitonsCount int64 `json:"notificationsCount"` + NotificationsList []common.MainNotificationStruct `json:"nofiticationsList"` + PageDetails common.PageDetails `json:"pageDetails"` } -func PreferencesSaved(w http.ResponseWriter, r *http.Request){ - +func PreferencesSaved(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/contributors/thankyou" { - pages.ErrorHandler(w, r, http.StatusNotFound) - return - } + pages.ErrorHandler(w, r, http.StatusNotFound) + return + } // Session check sessionOk, userID := users.ValidateDbSession(w, r) - if(!sessionOk){ - + if !sessionOk { + // Delete cookies users.DeleteSessionCookie(w, r) users.DeleteUserCookie(w, r) @@ -44,22 +44,22 @@ func PreferencesSaved(w http.ResponseWriter, r *http.Request){ // Fetch notificaitons _, _, notificationsCount, notificationsList := pages.NotificationsCountAndTopFive(userID) - if(!status){ + if !status { log.Println(msg) } else { - userNameImage = common.UsernameImageStruct{userName,image} + userNameImage = common.UsernameImageStruct{Username: userName, Image: image} } baseUrl := common.GetBaseurl() + common.CONST_APP_PORT - pageDetails := common.PageDetails{BaseUrl: baseUrl, Title : "Thank you"} + pageDetails := common.PageDetails{BaseUrl: baseUrl, Title: "Thank you"} output := FinalPreferenceSavedOutputStruct{userNameImage, notificationsCount, notificationsList, pageDetails} tmpl, err := template.New("").ParseFiles("templates/app/common/base.gohtml", "templates/app/common/contributormenu.gohtml", "templates/app/contributors/preferencessaved.gohtml") if err != nil { fmt.Println(err.Error()) - }else { - tmpl.ExecuteTemplate(w, "base", output) + } else { + tmpl.ExecuteTemplate(w, "base", output) } - + } diff --git a/sources/pages/contributors/reactions.go b/sources/pages/contributors/reactions.go index 1bf4ca7..e4cd065 100644 --- a/sources/pages/contributors/reactions.go +++ b/sources/pages/contributors/reactions.go @@ -14,13 +14,13 @@ import ( "techpro.club/sources/users" ) -type FinalReactionsOutputStruct struct{ - Projects []common.FeedStruct `json:"projects"` - UserNameImage common.UsernameImageStruct `json:"usernameImage"` - MyBookmarks []primitive.ObjectID `json:"myBookmarks"` - NotificaitonsCount int64 `json:"notificationsCount"` - NotificationsList []common.MainNotificationStruct `json:"nofiticationsList"` - PageDetails common.PageDetails `json:"pageDetails"` +type FinalReactionsOutputStruct struct { + Projects []common.FeedStruct `json:"projects"` + UserNameImage common.UsernameImageStruct `json:"usernameImage"` + MyBookmarks []primitive.ObjectID `json:"myBookmarks"` + NotificaitonsCount int64 `json:"notificationsCount"` + NotificationsList []common.MainNotificationStruct `json:"nofiticationsList"` + PageDetails common.PageDetails `json:"pageDetails"` } // Fetched reacted projects @@ -32,14 +32,14 @@ func FetchReactions(w http.ResponseWriter, r *http.Request) { // Session check sessionOk, userID := users.ValidateDbSession(w, r) - if(!sessionOk){ - + if !sessionOk { + // Delete cookies users.DeleteSessionCookie(w, r) users.DeleteUserCookie(w, r) http.Redirect(w, r, "/", http.StatusSeeOther) - } + } var userNameImage common.UsernameImageStruct @@ -49,16 +49,16 @@ func FetchReactions(w http.ResponseWriter, r *http.Request) { // Fetch notificaitons _, _, notificationsCount, notificationsList := pages.NotificationsCountAndTopFive(userID) - if(!status){ + if !status { log.Println(msg) } else { - userNameImage = common.UsernameImageStruct{userName,image} + userNameImage = common.UsernameImageStruct{Username: userName, Image: image} } var functions = template.FuncMap{ - "objectIdToString" : pages.ObjectIDToString, - "containsObjectId" : pages.ContainsObjectID, - "timeElapsed" : pages.TimeElapsed, + "objectIdToString": pages.ObjectIDToString, + "containsObjectId": pages.ContainsObjectID, + "timeElapsed": pages.TimeElapsed, } // constants for check @@ -68,41 +68,40 @@ func FetchReactions(w http.ResponseWriter, r *http.Request) { // Also fetch project details where the user reacted _, _, results := fetchReactedProjectsList(int64(pageid), userID) _, _, bookmarks, _ := pages.FetchMyBookmarksAndReactions(userID) - + baseUrl := common.GetBaseurl() + common.CONST_APP_PORT - pageDetails := common.PageDetails{BaseUrl: baseUrl, Title : "Reactions"} + pageDetails := common.PageDetails{BaseUrl: baseUrl, Title: "Reactions"} output := FinalReactionsOutputStruct{results, userNameImage, bookmarks, notificationsCount, notificationsList, pageDetails} tmpl, err := template.New("").Funcs(functions).ParseFiles("templates/app/common/base.gohtml", "templates/app/common/contributormenu.gohtml", "templates/app/contributors/reactions.gohtml") if err != nil { fmt.Println(err.Error()) - }else { - tmpl.ExecuteTemplate(w, "base", output) + } else { + tmpl.ExecuteTemplate(w, "base", output) } } // Fetch all reacted projects -func filterReactedProjects(pageid int64, userID primitive.ObjectID)(status bool, msg string, results []primitive.ObjectID){ - +func filterReactedProjects(pageid int64, userID primitive.ObjectID) (status bool, msg string, results []primitive.ObjectID) { status = false msg = "" var out common.FetchUserProjectReactionStruct - status, msg, client := common.Mongoconnect() + status, msg, client := common.Mongoconnect() defer client.Disconnect(context.TODO()) dbName := common.GetMoDb() fetchReactedProjects := client.Database(dbName).Collection(common.CONST_MO_USER_PROJECT_REACTIONS) // Fetch all reacted projects against a user - reactedProjectsResults, err:= fetchReactedProjects.Find(context.TODO(), bson.M{"userid": userID}) + reactedProjectsResults, err := fetchReactedProjects.Find(context.TODO(), bson.M{"userid": userID}) - if err != nil{ + if err != nil { msg = err.Error() } else { - for reactedProjectsResults.Next(context.TODO()){ + for reactedProjectsResults.Next(context.TODO()) { errDecode := reactedProjectsResults.Decode(&out) if errDecode != nil { @@ -116,10 +115,8 @@ func filterReactedProjects(pageid int64, userID primitive.ObjectID)(status bool, return status, msg, results } - // Filter all active projects from the database according to users reactions -func fetchReactedProjectsList(pageid int64, userID primitive.ObjectID)(status bool, msg string, results []common.FeedStruct){ - +func fetchReactedProjectsList(pageid int64, userID primitive.ObjectID) (status bool, msg string, results []common.FeedStruct) { status = false msg = "" @@ -127,7 +124,7 @@ func fetchReactedProjectsList(pageid int64, userID primitive.ObjectID)(status bo var finalConditions []bson.M resultsPerPage := int64(20) - status, msg, client := common.Mongoconnect() + status, msg, client := common.Mongoconnect() defer client.Disconnect(context.TODO()) dbName := common.GetMoDb() @@ -137,54 +134,53 @@ func fetchReactedProjectsList(pageid int64, userID primitive.ObjectID)(status bo status, errMsg, projectIds := filterReactedProjects(pageid, userID) - if !status{ + if !status { msg = errMsg } else { - if len(projectIds) <= 0{ + if len(projectIds) <= 0 { msg = "No projects found" - results = []common.FeedStruct{} + results = []common.FeedStruct{} } else { - finalConditions = append(finalConditions, bson.M{"_id" : bson.M{"$in": projectIds}}) - - aggCondition := bson.M{"$match": bson.M{"$and" : finalConditions}} - - + finalConditions = append(finalConditions, bson.M{"_id": bson.M{"$in": projectIds}}) + + aggCondition := bson.M{"$match": bson.M{"$and": finalConditions}} + // Filter joins aggLookup := bson.M{"$lookup": bson.M{ - "from": common.CONST_MO_USERS, // the collection name - "localField": "userid", // the field on the child struct - "foreignField": "_id", // the field on the parent struct - "as": "userdetails", // the field to populate into + "from": common.CONST_MO_USERS, // the collection name + "localField": "userid", // the field on the child struct + "foreignField": "_id", // the field on the parent struct + "as": "userdetails", // the field to populate into }} - + // Set projections - aggProjections := bson.M{"$project": bson.M{ - "_id": 1, "projectname" : 1, - "projectdescription" : 1, - "repolink": 1, - "languages": 1, - "otherlanguages": 1, - "allied": 1, - "company" : 1, - "companyname": 1, - "createddate": 1, - "public" : 1, - "reactionscount": 1, - "userdetails" : bson.M{ "_id" : 1, "name": 1, "imagelink" :1}, + aggProjections := bson.M{"$project": bson.M{ + "_id": 1, "projectname": 1, + "projectdescription": 1, + "repolink": 1, + "languages": 1, + "otherlanguages": 1, + "allied": 1, + "company": 1, + "companyname": 1, + "createddate": 1, + "public": 1, + "reactionscount": 1, + "userdetails": bson.M{"_id": 1, "name": 1, "imagelink": 1}, }} - + aggSkip := bson.M{"$skip": (pageid * resultsPerPage)} aggLimit := bson.M{"$limit": resultsPerPage} - + projectsList, err := fetchProjects.Aggregate(context.TODO(), []bson.M{aggCondition, aggLookup, aggProjections, aggSkip, aggLimit}) - + if err != nil { msg = err.Error() } else { - for projectsList.Next(context.TODO()){ + for projectsList.Next(context.TODO()) { var elem common.FeedStruct errDecode := projectsList.Decode(&elem) - + if errDecode != nil { msg = errDecode.Error() } else { @@ -199,4 +195,3 @@ func fetchReactedProjectsList(pageid int64, userID primitive.ObjectID)(status bo return status, msg, results } - diff --git a/sources/pages/contributors/videofeeds.go b/sources/pages/contributors/videofeeds.go index f02047c..5f830d3 100644 --- a/sources/pages/contributors/videofeeds.go +++ b/sources/pages/contributors/videofeeds.go @@ -14,14 +14,14 @@ import ( func VideoFeeds(w http.ResponseWriter, r *http.Request) { if r.URL.Path != "/contributors/videofeeds" { - pages.ErrorHandler(w, r, http.StatusNotFound) - return - } + pages.ErrorHandler(w, r, http.StatusNotFound) + return + } // Session check sessionOk, userID := users.ValidateDbSession(w, r) - if(!sessionOk){ - + if !sessionOk { + // Delete cookies users.DeleteSessionCookie(w, r) users.DeleteUserCookie(w, r) @@ -37,31 +37,30 @@ func VideoFeeds(w http.ResponseWriter, r *http.Request) { // Fetch notificaitons _, _, notificationsCount, notificationsList := pages.NotificationsCountAndTopFive(userID) - if(!status){ + if !status { log.Println(msg) } else { - userNameImage = common.UsernameImageStruct{userName,image} + userNameImage = common.UsernameImageStruct{Username: userName, Image: image} } - - if r.Method == "GET"{ + if r.Method == "GET" { baseUrl := common.GetBaseurl() + common.CONST_APP_PORT - pageDetails := common.PageDetails{BaseUrl: baseUrl, Title : "Video Feeds"} + pageDetails := common.PageDetails{BaseUrl: baseUrl, Title: "Video Feeds"} output := videos.FinalVideoListOutStruct{ - userNameImage, - notificationsCount, - notificationsList, - pageDetails, + UserNameImage: userNameImage, + NotificaitonsCount: notificationsCount, + NotificationsList: notificationsList, + PageDetails: pageDetails, } - tmpl, err := template.New("").ParseFiles("templates/app/common/base.gohtml", "templates/app/common/contributormenu.gohtml", "templates/app/contributors/videofeeds.gohtml") + tmpl, err := template.New("").ParseFiles("templates/app/common/base.gohtml", "templates/app/common/contributormenu.gohtml", "templates/app/contributors/videofeeds.gohtml") if err != nil { fmt.Println(err.Error()) - }else { - tmpl.ExecuteTemplate(w, "base", output) + } else { + tmpl.ExecuteTemplate(w, "base", output) } } -} \ No newline at end of file +} diff --git a/sources/users/savecontributor_test.go b/sources/users/savecontributor_test.go index 4328374..c024823 100644 --- a/sources/users/savecontributor_test.go +++ b/sources/users/savecontributor_test.go @@ -7,11 +7,11 @@ import ( // Test SaveUser func TestSaveUser(t *testing.T) { - var testEmail, testName, testLogin, testLocation, testImageLink, testRepoUrl, testSource, testUserType string + var testEmail, testName, testLogin, testLocation, testImageLink, testRepoUrl, testSource, testUserType, testSession string var testW http.ResponseWriter = http.ResponseWriter(nil) var testR *http.Request = new(http.Request) - status, msg, _ := SaveUser(testW, testR, testEmail, testName, testLocation, testImageLink, testRepoUrl, testSource, testUserType, testLogin) + status, msg, _ := SaveUser(testW, testR, testEmail, testName, testLocation, testImageLink, testRepoUrl, testSource, testUserType, testLogin, testSession) if !status { t.Errorf(msg)