CoastalCommitsPastes/server/src/routes/admin.ts

115 lines
2.1 KiB
TypeScript
Raw Normal View History

2022-03-29 00:19:33 -07:00
import isAdmin from "@lib/middleware/is-admin"
import { Post } from "@lib/models/Post"
import { User } from "@lib/models/User"
import { File } from "@lib/models/File"
import { Router } from "express"
export const admin = Router()
admin.use(isAdmin)
admin.get("/is-admin", async (req, res) => {
2022-03-29 00:19:33 -07:00
return res.json({
isAdmin: true
})
})
admin.get("/users", async (req, res, next) => {
2022-03-29 00:19:33 -07:00
try {
const users = await User.findAll({
attributes: {
exclude: ["password"],
include: ["id", "username", "createdAt", "updatedAt"]
},
include: [
{
model: Post,
as: "posts",
attributes: ["id"]
}
]
})
res.json(users)
} catch (e) {
next(e)
}
})
admin.get("/posts", async (req, res, next) => {
2022-03-29 00:19:33 -07:00
try {
const posts = await Post.findAll({
attributes: {
exclude: ["content"],
include: ["id", "title", "visibility", "createdAt"]
},
include: [
{
model: File,
as: "files",
attributes: ["id", "title", "createdAt", "html"]
},
{
model: User,
as: "users",
attributes: ["id", "username"]
}
]
})
res.json(posts)
} catch (e) {
next(e)
}
})
admin.get("/post/:id", async (req, res, next) => {
2022-03-29 00:19:33 -07:00
try {
const post = await Post.findByPk(req.params.id, {
attributes: {
exclude: ["content"],
include: ["id", "title", "visibility", "createdAt"]
},
include: [
{
model: File,
as: "files",
attributes: ["id", "title", "sha", "createdAt", "updatedAt", "html"]
},
{
model: User,
as: "users",
attributes: ["id", "username"]
}
]
})
if (!post) {
return res.status(404).json({
message: "Post not found"
})
}
2022-03-29 00:19:33 -07:00
res.json(post)
} catch (e) {
next(e)
}
})
admin.delete("/post/:id", async (req, res, next) => {
2022-03-29 00:19:33 -07:00
try {
const post = await Post.findByPk(req.params.id)
if (!post) {
return res.status(404).json({
message: "Post not found"
})
}
2022-03-29 00:19:33 -07:00
if (post.files?.length)
await Promise.all(post.files.map((file) => file.destroy()))
await post.destroy({ force: true })
res.json({
message: "Post deleted"
})
} catch (e) {
next(e)
}
})