Skip to content
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
115 changes: 55 additions & 60 deletions sources/pages/contributors/bookmarks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -116,18 +115,16 @@ 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 = ""

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()
Expand All @@ -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 {
Expand All @@ -199,4 +195,3 @@ func fetchBookmarkedProjectsList(pageid int64, userID primitive.ObjectID)(status

return status, msg, results
}

Loading