Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
KSZK
DevTeam
kszkepzes
Backend
Commits
245edf82
Commit
245edf82
authored
Jul 30, 2020
by
rlacko
💬
Browse files
populate hell
parent
2f7d48e6
Pipeline
#4673
passed with stages
in 1 minute and 45 seconds
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
src/resources/activity/activityControllers.js
View file @
245edf82
const
{
crudControllers
}
=
require
(
'
../../utils/crud
'
)
const
{
Activity
}
=
require
(
'
./activityModel
'
)
const
{
User
}
=
require
(
'
../user/userModel
'
)
const
{
Attendance
}
=
require
(
'
../attendance/attendanceModel
'
)
const
{
Comment
}
=
require
(
'
../comment/commentModel
'
)
const
{
omit
,
pick
}
=
require
(
'
lodash
'
)
// Config
const
pickedKeys
=
[
'
_id
'
,
'
title
'
,
...
...
@@ -12,13 +12,44 @@ const pickedKeys = [
'
date
'
,
'
type
'
,
'
createdAt
'
,
'
updatedAt
'
,
'
attendance
'
,
'
comment
'
,
]
exports
.
default
=
crudControllers
(
Activity
,
pickedKeys
)
const
activitySelector
=
'
_id title description date type createdAt updatedAt attendance comment
'
const
commentSelector
=
'
_id creator _creator text createdAt
'
const
attendanceSelector
=
'
_id user state
'
const
creatorSelector
=
'
-_id fullName nickName schacc
'
// Overwrites the given field with the populated User
// field: populate on this
// byPopulate: user schacc field name
async
function
populateUsersOnField
(
fieldToPopulate
,
byPopulate
)
{
let
newField
=
[]
for
(
let
field
of
fieldToPopulate
)
{
const
populated
=
await
User
.
findOne
({
schacc
:
field
[
byPopulate
],
})
.
lean
()
.
exec
()
field
[
byPopulate
]
=
pick
(
populated
,
creatorSelector
.
split
(
'
'
))
newField
.
push
(
field
)
}
return
newField
}
async
function
populateCommentsandAttendances
(
activity
)
{
activity
.
comment
=
await
populateUsersOnField
(
activity
.
comment
,
'
creator
'
)
activity
.
attendance
=
await
populateUsersOnField
(
activity
.
attendance
,
'
user
'
)
return
activity
}
exports
.
default
.
createOne
=
async
(
req
,
res
)
=>
{
// Controller
module
.
exports
.
createOne
=
async
function
createOne
(
req
,
res
)
{
try
{
// Invalid Date provided
if
(
!
req
.
body
.
date
||
Date
.
parse
(
req
.
body
.
date
)
<
new
Date
().
getTime
())
...
...
@@ -59,22 +90,22 @@ exports.default.createOne = async (req, res) => {
await
activity
.
populate
({
path
:
'
comment
'
,
select
:
'
_id creator text createdAt
'
,
select
:
commentSelector
,
})
.
populate
({
path
:
'
attendance
'
,
select
:
'
_id user state
'
,
select
:
attendanceSelector
,
})
.
execPopulate
()
// conver to JS Object to overwrite .creator
let
objActivity
=
activity
.
toObject
()
objActivity
.
creator
=
objActivity
.
_creator
const
populatedActivity
=
await
populateCommentsandAttendances
(
activity
.
toObject
()
)
res
return
res
.
status
(
200
)
.
json
({
data
:
pick
(
obj
Activity
,
pickedKeys
),
data
:
pick
(
populated
Activity
,
pickedKeys
),
})
.
end
()
}
catch
(
err
)
{
...
...
@@ -91,32 +122,33 @@ exports.default.createOne = async (req, res) => {
}
}
exports
.
default
.
getOne
=
async
(
req
,
res
)
=>
{
module
.
exports
.
getOne
=
async
function
getOne
(
req
,
res
)
{
try
{
const
activity
=
await
Activity
.
findOne
({
_id
:
req
.
params
.
id
})
.
populate
({
path
:
'
comment
'
,
select
:
'
_id creator text createdAt
'
,
select
:
commentSelector
,
})
.
populate
({
path
:
'
attendance
'
,
select
:
'
_id user state
'
,
select
:
attendanceSelector
,
})
.
select
(
'
-__v
'
)
.
lean
()
.
exec
()
if
(
!
activity
)
return
res
.
status
(
404
).
json
({
messages
:
[
'
No such activity.
'
]
})
return
res
.
status
(
200
).
json
({
data
:
pick
(
activity
,
pickedKeys
)
})
const
populatedActivity
=
await
populateCommentsandAttendances
(
activity
)
return
res
.
status
(
200
).
json
({
data
:
pick
(
populatedActivity
,
pickedKeys
)
})
}
catch
(
err
)
{
console
.
error
(
err
)
return
res
.
status
(
500
).
json
({
message
:
err
.
message
})
}
}
exports
.
default
.
getMany
=
async
(
req
,
res
)
=>
{
module
.
exports
.
getMany
=
async
function
getMany
(
req
,
res
)
{
try
{
const
activity
=
await
Activity
.
find
()
.
populate
({
...
...
@@ -135,9 +167,12 @@ exports.default.getMany = async (req, res) => {
return
res
.
status
(
404
).
json
({
message
:
'
Activity not found!
'
})
res
.
status
(
200
).
json
({
data
:
activity
.
map
(
function
pickKeys
(
doc
)
{
return
pick
(
doc
,
pickedKeys
)
}),
data
:
await
Promise
.
all
(
activity
.
map
(
async
function
pickKeys
(
doc
)
{
const
populatedActivity
=
await
populateCommentsandAttendances
(
doc
)
return
pick
(
populatedActivity
,
pickedKeys
)
})
),
})
}
catch
(
err
)
{
console
.
error
(
err
)
...
...
@@ -145,7 +180,7 @@ exports.default.getMany = async (req, res) => {
}
}
exports
.
default
.
removeOne
=
async
(
req
,
res
)
=>
{
module
.
exports
.
removeOne
=
async
function
removeOne
(
req
,
res
)
{
try
{
const
activity
=
await
Activity
.
findByIdAndDelete
({
_id
:
req
.
params
.
id
,
...
...
@@ -181,7 +216,7 @@ exports.default.removeOne = async (req, res) => {
}
}
exports
.
default
.
updateOne
=
async
(
req
,
res
)
=>
{
module
.
exports
.
updateOne
=
async
function
updateOne
(
req
,
res
)
{
try
{
const
activity
=
await
Activity
.
findOneAndUpdate
(
{
_id
:
req
.
params
.
id
},
...
...
src/resources/activity/activityRouter.js
View file @
245edf82
...
...
@@ -7,14 +7,14 @@ const router = Router()
// /api/item
router
.
route
(
'
/
'
)
.
get
(
isLoggedIn
,
controllers
.
default
.
getMany
)
.
post
(
isLoggedIn
,
isMentor
,
controllers
.
default
.
createOne
)
.
get
(
isLoggedIn
,
controllers
.
getMany
)
.
post
(
isLoggedIn
,
isMentor
,
controllers
.
createOne
)
// /api/item/:id
router
.
route
(
'
/:id
'
)
.
get
(
isLoggedIn
,
controllers
.
default
.
getOne
)
.
put
(
isLoggedIn
,
isMentor
,
controllers
.
default
.
updateOne
)
.
delete
(
isLoggedIn
,
isMentor
,
controllers
.
default
.
removeOne
)
.
get
(
isLoggedIn
,
controllers
.
getOne
)
.
put
(
isLoggedIn
,
isMentor
,
controllers
.
updateOne
)
.
delete
(
isLoggedIn
,
isMentor
,
controllers
.
removeOne
)
exports
.
default
=
router
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment