mirror of
https://github.com/ragestudio/comty.git
synced 2025-06-10 02:54:15 +00:00
added BadgesController
This commit is contained in:
parent
4d4b9b3050
commit
83dc6bb2f0
106
packages/server/src/controllers/BadgesController/index.js
Normal file
106
packages/server/src/controllers/BadgesController/index.js
Normal file
@ -0,0 +1,106 @@
|
||||
import { Controller } from "linebridge/dist/server"
|
||||
import { Badge, User } from "../../models"
|
||||
import { Schematized } from "../../lib"
|
||||
|
||||
export default class BadgesController extends Controller {
|
||||
static refName = "BadgesController"
|
||||
|
||||
get = {
|
||||
"/badges": Schematized({
|
||||
select: ["_id", "name", "label"],
|
||||
}, async (req, res) => {
|
||||
let badges = []
|
||||
|
||||
if (req.selection._id) {
|
||||
badges = await Badge.find({
|
||||
_id: { $in: req.selection._id },
|
||||
})
|
||||
|
||||
badges = badges.map(badge => badge.toObject())
|
||||
} else {
|
||||
badges = await Badge.find(req.selection).catch((err) => {
|
||||
res.status(500).json({ error: err })
|
||||
return false
|
||||
})
|
||||
}
|
||||
|
||||
if (badges) {
|
||||
return res.json(badges)
|
||||
}
|
||||
}),
|
||||
}
|
||||
|
||||
post = {
|
||||
"/badge": {
|
||||
middlewares: ["withAuthentication"],
|
||||
fn: Schematized({
|
||||
required: ["name"],
|
||||
select: ["name", "label", "description", "icon", "color"],
|
||||
}, async (req, res) => {
|
||||
await Badge.findOne(req.selection).then((data) => {
|
||||
if (data) {
|
||||
return res.status(409).json({
|
||||
error: "This badge is already created",
|
||||
})
|
||||
}
|
||||
|
||||
let badge = new Badge({
|
||||
name: req.selection.name,
|
||||
label: req.selection.label,
|
||||
description: req.selection.description,
|
||||
icon: req.selection.icon,
|
||||
color: req.selection.color,
|
||||
})
|
||||
|
||||
badge.save()
|
||||
|
||||
return res.json(badge)
|
||||
})
|
||||
})
|
||||
},
|
||||
"/badge/:badge_id/giveToUser": {
|
||||
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 already have this badge" })
|
||||
}
|
||||
|
||||
user.badges.push(badge._id.toString())
|
||||
|
||||
user.save()
|
||||
|
||||
return res.json(user)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
put = {
|
||||
|
||||
}
|
||||
|
||||
delete = {
|
||||
|
||||
}
|
||||
}
|
@ -6,3 +6,4 @@ export { default as FilesController } from "./FilesController"
|
||||
export { default as PublicController } from "./PublicController"
|
||||
export { default as PostsController } from "./PostsController"
|
||||
export { default as StreamingController } from "./StreamingController"
|
||||
export { default as BadgesController } from "./BadgesController"
|
Loading…
x
Reference in New Issue
Block a user