Commit b8b7aa0d authored by rlacko's avatar rlacko 💬
Browse files

get many activity

parent cc24bf3d
Pipeline #4661 passed with stages
in 1 minute and 33 seconds
......@@ -5,13 +5,18 @@ const { Attendance } = require('../attendance/attendanceModel')
const { Comment } = require('../comment/commentModel')
const { omit, pick } = require('lodash')
exports.default = crudControllers(Activity, [
const defaultKeys = [
'_id',
'title',
'description',
'date',
'type',
])
'createdAt',
'attendance',
'comment',
]
exports.default = crudControllers(Activity, defaultKeys)
exports.default.createOne = async (req, res) => {
try {
......@@ -69,16 +74,7 @@ exports.default.createOne = async (req, res) => {
res
.status(200)
.json({
data: pick(objActivity, [
'_id',
'title',
'description',
'date',
'type',
'createdAt',
'attendance',
'comment',
]),
data: pick(objActivity, defaultKeys),
})
.end()
} catch (err) {
......@@ -90,7 +86,8 @@ exports.default.createOne = async (req, res) => {
}
return res.status(422).json({ messages })
}
return res.status(500).json({ message: err.message }).end()
console.error(err)
return res.status(500).json({ message: err.message })
}
}
......@@ -112,9 +109,10 @@ exports.default.getOne = async (req, res) => {
if (!activity)
return res.status(404).json({ messages: ['No such activity.'] })
return res.status(200).json({ data: activity }).end()
return res.status(200).json({ data: pick(activity, defaultKeys) })
} catch (err) {
return res.status(500).json({ message: err.message }).end()
console.error(err)
return res.status(500).json({ message: err.message })
}
}
......@@ -123,19 +121,27 @@ exports.default.getMany = async (req, res) => {
const activity = await Activity.find()
.populate({
path: 'comment',
populate: { path: 'creator', select: '-_id fullName nickName' },
select: '_id creator text createdAt',
})
.populate({
path: 'attendance',
select: '_id user state',
})
.select('-__v -_id')
.lean()
.exec()
if (!activity) {
res.status(404).json({ message: 'Activity not found!' }).end()
}
if (!activity)
return res.status(404).json({ message: 'Activity not found!' })
res.status(200).json({ data: activity }).end()
res.status(200).json({
data: activity.map(function pickKeys(doc) {
return pick(doc, defaultKeys)
}),
})
} catch (err) {
res.status(500).json({ message: err.message }).end()
console.error(err)
res.status(500).json({ message: err.message })
}
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment