@@ -14,13 +14,13 @@ import (
14
14
"techpro.club/sources/users"
15
15
)
16
16
17
- type FinalBookmarksOutputStruct struct {
18
- Projects []common.FeedStruct `json:"projects"`
19
- UserNameImage common.UsernameImageStruct `json:"usernameImage"`
20
- MyReactions []primitive.ObjectID `json:"myReactions"`
21
- NotificaitonsCount int64 `json:"notificationsCount"`
22
- NotificationsList []common.MainNotificationStruct `json:"nofiticationsList"`
23
- PageDetails common.PageDetails `json:"pageDetails"`
17
+ type FinalBookmarksOutputStruct struct {
18
+ Projects []common.FeedStruct `json:"projects"`
19
+ UserNameImage common.UsernameImageStruct `json:"usernameImage"`
20
+ MyReactions []primitive.ObjectID `json:"myReactions"`
21
+ NotificaitonsCount int64 `json:"notificationsCount"`
22
+ NotificationsList []common.MainNotificationStruct `json:"nofiticationsList"`
23
+ PageDetails common.PageDetails `json:"pageDetails"`
24
24
}
25
25
26
26
// Fetched bookmarked projects
@@ -32,14 +32,14 @@ func FetchBookmarks(w http.ResponseWriter, r *http.Request) {
32
32
33
33
// Session check
34
34
sessionOk , userID := users .ValidateDbSession (w , r )
35
- if ( ! sessionOk ) {
36
-
35
+ if ! sessionOk {
36
+
37
37
// Delete cookies
38
38
users .DeleteSessionCookie (w , r )
39
39
users .DeleteUserCookie (w , r )
40
40
41
41
http .Redirect (w , r , "/" , http .StatusSeeOther )
42
- }
42
+ }
43
43
44
44
var userNameImage common.UsernameImageStruct
45
45
@@ -49,16 +49,16 @@ func FetchBookmarks(w http.ResponseWriter, r *http.Request) {
49
49
// Fetch notificaitons
50
50
_ , _ , notificationsCount , notificationsList := pages .NotificationsCountAndTopFive (userID )
51
51
52
- if ( ! status ) {
52
+ if ! status {
53
53
log .Println (msg )
54
54
} else {
55
- userNameImage = common.UsernameImageStruct {userName ,image }
55
+ userNameImage = common.UsernameImageStruct {Username : userName , Image : image }
56
56
}
57
57
58
58
var functions = template.FuncMap {
59
- "objectIdToString" : pages .ObjectIDToString ,
60
- "containsObjectId" : pages .ContainsObjectID ,
61
- "timeElapsed" : pages .TimeElapsed ,
59
+ "objectIdToString" : pages .ObjectIDToString ,
60
+ "containsObjectId" : pages .ContainsObjectID ,
61
+ "timeElapsed" : pages .TimeElapsed ,
62
62
}
63
63
64
64
// constants for check
@@ -70,39 +70,38 @@ func FetchBookmarks(w http.ResponseWriter, r *http.Request) {
70
70
_ , _ , _ , reactions := pages .FetchMyBookmarksAndReactions (userID )
71
71
72
72
baseUrl := common .GetBaseurl () + common .CONST_APP_PORT
73
- pageDetails := common.PageDetails {BaseUrl : baseUrl , Title : "Bookmarks" }
73
+ pageDetails := common.PageDetails {BaseUrl : baseUrl , Title : "Bookmarks" }
74
74
75
75
output := FinalBookmarksOutputStruct {results , userNameImage , reactions , notificationsCount , notificationsList , pageDetails }
76
76
77
77
tmpl , err := template .New ("" ).Funcs (functions ).ParseFiles ("templates/app/common/base.gohtml" , "templates/app/common/contributormenu.gohtml" , "templates/app/contributors/bookmarks.gohtml" )
78
78
if err != nil {
79
79
fmt .Println (err .Error ())
80
- }else {
81
- tmpl .ExecuteTemplate (w , "base" , output )
80
+ } else {
81
+ tmpl .ExecuteTemplate (w , "base" , output )
82
82
}
83
83
}
84
84
85
85
// Fetch all bookmarked projects
86
- func filterBookmarkedProjects (pageid int64 , userID primitive.ObjectID )(status bool , msg string , results []primitive.ObjectID ){
87
-
86
+ func filterBookmarkedProjects (pageid int64 , userID primitive.ObjectID ) (status bool , msg string , results []primitive.ObjectID ) {
88
87
89
88
status = false
90
89
msg = ""
91
90
var out common.FetchUserProjectBookmarkStruct
92
91
93
- status , msg , client := common .Mongoconnect ()
92
+ status , msg , client := common .Mongoconnect ()
94
93
defer client .Disconnect (context .TODO ())
95
94
96
95
dbName := common .GetMoDb ()
97
96
fetchBookmarkedProjects := client .Database (dbName ).Collection (common .CONST_MO_BOOKMARKS )
98
97
99
98
// Fetch all bookmarked projects against a user
100
- bookmarkedProjectsResults , err := fetchBookmarkedProjects .Find (context .TODO (), bson.M {"userid" : userID })
99
+ bookmarkedProjectsResults , err := fetchBookmarkedProjects .Find (context .TODO (), bson.M {"userid" : userID })
101
100
102
- if err != nil {
101
+ if err != nil {
103
102
msg = err .Error ()
104
103
} else {
105
- for bookmarkedProjectsResults .Next (context .TODO ()){
104
+ for bookmarkedProjectsResults .Next (context .TODO ()) {
106
105
errDecode := bookmarkedProjectsResults .Decode (& out )
107
106
108
107
if errDecode != nil {
@@ -116,18 +115,16 @@ func filterBookmarkedProjects(pageid int64, userID primitive.ObjectID)(status bo
116
115
return status , msg , results
117
116
}
118
117
119
-
120
118
// Filter all active projects from the database according to users bookmarks
121
- func fetchBookmarkedProjectsList (pageid int64 , userID primitive.ObjectID )(status bool , msg string , results []common.FeedStruct ){
122
-
119
+ func fetchBookmarkedProjectsList (pageid int64 , userID primitive.ObjectID ) (status bool , msg string , results []common.FeedStruct ) {
123
120
124
121
status = false
125
122
msg = ""
126
123
127
124
var finalConditions []bson.M
128
125
resultsPerPage := int64 (20 )
129
126
130
- status , msg , client := common .Mongoconnect ()
127
+ status , msg , client := common .Mongoconnect ()
131
128
defer client .Disconnect (context .TODO ())
132
129
133
130
dbName := common .GetMoDb ()
@@ -137,54 +134,53 @@ func fetchBookmarkedProjectsList(pageid int64, userID primitive.ObjectID)(status
137
134
138
135
status , errMsg , projectIds := filterBookmarkedProjects (pageid , userID )
139
136
140
- if ! status {
137
+ if ! status {
141
138
msg = errMsg
142
139
} else {
143
- if len (projectIds ) <= 0 {
140
+ if len (projectIds ) <= 0 {
144
141
msg = "No projects found"
145
- results = []common.FeedStruct {}
142
+ results = []common.FeedStruct {}
146
143
} else {
147
- finalConditions = append (finalConditions , bson.M {"_id" : bson.M {"$in" : projectIds }})
148
-
149
- aggCondition := bson.M {"$match" : bson.M {"$and" : finalConditions }}
150
-
151
-
144
+ finalConditions = append (finalConditions , bson.M {"_id" : bson.M {"$in" : projectIds }})
145
+
146
+ aggCondition := bson.M {"$match" : bson.M {"$and" : finalConditions }}
147
+
152
148
// Filter joins
153
149
aggLookup := bson.M {"$lookup" : bson.M {
154
- "from" : common .CONST_MO_USERS , // the collection name
155
- "localField" : "userid" , // the field on the child struct
156
- "foreignField" : "_id" , // the field on the parent struct
157
- "as" : "userdetails" , // the field to populate into
150
+ "from" : common .CONST_MO_USERS , // the collection name
151
+ "localField" : "userid" , // the field on the child struct
152
+ "foreignField" : "_id" , // the field on the parent struct
153
+ "as" : "userdetails" , // the field to populate into
158
154
}}
159
-
155
+
160
156
// Set projections
161
- aggProjections := bson.M {"$project" : bson.M {
162
- "_id" : 1 , "projectname" : 1 ,
163
- "projectdescription" : 1 ,
164
- "repolink" : 1 ,
165
- "languages" : 1 ,
166
- "otherlanguages" : 1 ,
167
- "allied" : 1 ,
168
- "company" : 1 ,
169
- "companyname" : 1 ,
170
- "createddate" : 1 ,
171
- "public" : 1 ,
172
- "reactionscount" : 1 ,
173
- "userdetails" : bson.M { "_id" : 1 , "name" : 1 , "imagelink" : 1 },
157
+ aggProjections := bson.M {"$project" : bson.M {
158
+ "_id" : 1 , "projectname" : 1 ,
159
+ "projectdescription" : 1 ,
160
+ "repolink" : 1 ,
161
+ "languages" : 1 ,
162
+ "otherlanguages" : 1 ,
163
+ "allied" : 1 ,
164
+ "company" : 1 ,
165
+ "companyname" : 1 ,
166
+ "createddate" : 1 ,
167
+ "public" : 1 ,
168
+ "reactionscount" : 1 ,
169
+ "userdetails" : bson.M {"_id" : 1 , "name" : 1 , "imagelink" : 1 },
174
170
}}
175
-
171
+
176
172
aggSkip := bson.M {"$skip" : (pageid * resultsPerPage )}
177
173
aggLimit := bson.M {"$limit" : resultsPerPage }
178
-
174
+
179
175
projectsList , err := fetchProjects .Aggregate (context .TODO (), []bson.M {aggCondition , aggLookup , aggProjections , aggSkip , aggLimit })
180
-
176
+
181
177
if err != nil {
182
178
msg = err .Error ()
183
179
} else {
184
- for projectsList .Next (context .TODO ()){
180
+ for projectsList .Next (context .TODO ()) {
185
181
var elem common.FeedStruct
186
182
errDecode := projectsList .Decode (& elem )
187
-
183
+
188
184
if errDecode != nil {
189
185
msg = errDecode .Error ()
190
186
} else {
@@ -199,4 +195,3 @@ func fetchBookmarkedProjectsList(pageid int64, userID primitive.ObjectID)(status
199
195
200
196
return status , msg , results
201
197
}
202
-
0 commit comments