mirror of
https://github.com/ragestudio/comty.git
synced 2025-06-10 02:54:15 +00:00
implement delete and put methods
This commit is contained in:
parent
54867d7eb1
commit
7ce1677b30
@ -97,10 +97,86 @@ export default class BadgesController extends Controller {
|
||||
}
|
||||
|
||||
put = {
|
||||
"/badge/:badge_id": {
|
||||
middlewares: ["withAuthentication", "onlyAdmin"],
|
||||
fn: Schematized({
|
||||
select: ["name", "label", "description", "icon", "color"],
|
||||
}, async (req, res) => {
|
||||
const badge = await Badge.findById(req.params.badge_id).catch((err) => {
|
||||
res.status(500).json({ error: err })
|
||||
return false
|
||||
})
|
||||
|
||||
if (!badge) {
|
||||
return res.status(404).json({ error: "No badge founded" })
|
||||
}
|
||||
|
||||
badge.name = req.selection.name || badge.name
|
||||
badge.label = req.selection.label || badge.label
|
||||
badge.description = req.selection.description || badge.description
|
||||
badge.icon = req.selection.icon || badge.icon
|
||||
badge.color = req.selection.color || badge.color
|
||||
|
||||
badge.save()
|
||||
|
||||
return res.json(badge)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
delete = {
|
||||
"/badge/:badge_id": {
|
||||
middlewares: ["withAuthentication", "onlyAdmin"],
|
||||
fn: async (req, res) => {
|
||||
const badge = await Badge.findById(req.params.badge_id).catch((err) => {
|
||||
res.status(500).json({ error: err })
|
||||
return false
|
||||
})
|
||||
|
||||
if (!badge) {
|
||||
return res.status(404).json({ error: "No badge founded" })
|
||||
}
|
||||
|
||||
badge.remove()
|
||||
|
||||
return res.json(badge)
|
||||
}
|
||||
},
|
||||
"/badge/:badge_id/removeFromUser": {
|
||||
middlewares: ["withAuthentication", "onlyAdmin"],
|
||||
fn: Schematized({
|
||||
required: ["user_id"],
|
||||
select: ["user_id"],
|
||||
}, async (req, res) => {
|
||||
const badge = await Badge.findById(req.params.badge_id).catch((err) => {
|
||||
res.status(500).json({ error: err })
|
||||
return false
|
||||
})
|
||||
|
||||
if (!badge) {
|
||||
return res.status(404).json({ error: "No badge founded" })
|
||||
}
|
||||
|
||||
const user = await User.findById(req.selection.user_id).catch((err) => {
|
||||
res.status(500).json({ error: err })
|
||||
return false
|
||||
})
|
||||
|
||||
if (!user) {
|
||||
return res.status(404).json({ error: "No user founded" })
|
||||
}
|
||||
|
||||
// check if user already have this badge
|
||||
if (!user.badges.includes(badge._id)) {
|
||||
return res.status(409).json({ error: "User don't have this badge" })
|
||||
}
|
||||
|
||||
user.badges = user.badges.filter(b => b !== badge._id.toString())
|
||||
|
||||
user.save()
|
||||
|
||||
return res.json(user)
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user