declare middlewares every endpoint

This commit is contained in:
srgooglo 2022-09-04 03:28:01 +02:00
parent 3d034e9ccf
commit eb75941d52

View File

@ -4,7 +4,7 @@ import { Post, User } from "../../models"
export default class PostsController extends Controller { export default class PostsController extends Controller {
static refName = "PostsController" static refName = "PostsController"
static useMiddlewares = ["withAuthentication"] //static useMiddlewares = ["withAuthentication"]
methods = { methods = {
createPost: async (payload) => { createPost: async (payload) => {
@ -142,7 +142,8 @@ export default class PostsController extends Controller {
} }
get = { get = {
"/feed": Schematized({ "/feed": {
fn: Schematized({
select: ["user_id"] select: ["user_id"]
}, async (req, res) => { }, async (req, res) => {
const feedLimit = req.query?.limit ?? 20 const feedLimit = req.query?.limit ?? 20
@ -173,11 +174,41 @@ export default class PostsController extends Controller {
posts = await Promise.all(posts) posts = await Promise.all(posts)
return res.json(posts) return res.json(posts)
}), })
},
"/post": {
fn: Schematized({
select: ["post_id"],
required: ["post_id"]
}, async (req, res) => {
if (typeof req.selection.post_id !== "string") {
return res.status(400).json({
error: "post_id must be a string"
})
}
const post = await Post.findById(req.selection.post_id).catch(() => null)
if (!post) {
return res.status(404).json({
error: "Post not found"
})
}
const user = await User.findById(post.user_id)
return res.json({
...post.toObject(),
user: user.toObject(),
})
})
}
} }
put = { put = {
"/post": Schematized({ "/post": {
middlewares: ["withAuthentification"],
fn: Schematized({
required: ["message"], required: ["message"],
select: ["message", "additions"], select: ["message", "additions"],
}, async (req, res) => { }, async (req, res) => {
@ -188,8 +219,11 @@ export default class PostsController extends Controller {
}) })
return res.json(post) return res.json(post)
}), })
"/toogle_like": Schematized({ },
"/toogle_like": {
middlewares: ["withAuthentification"],
fn: Schematized({
required: ["post_id"], required: ["post_id"],
select: ["post_id"], select: ["post_id"],
}, async (req, res) => { }, async (req, res) => {
@ -211,8 +245,11 @@ export default class PostsController extends Controller {
success: true, success: true,
post post
}) })
}), })
"/like": Schematized({ },
"/like": {
middlewares: ["withAuthentification"],
fn: Schematized({
required: ["post_id"], required: ["post_id"],
select: ["post_id"], select: ["post_id"],
}, async (req, res) => { }, async (req, res) => {
@ -232,8 +269,11 @@ export default class PostsController extends Controller {
return res.json({ return res.json({
success: true, success: true,
}) })
}), })
"/unlike": Schematized({ },
"/unlike": {
middlewares: ["withAuthentification"],
fn: Schematized({
required: ["post_id"], required: ["post_id"],
select: ["post_id"], select: ["post_id"],
}, async (req, res) => { }, async (req, res) => {
@ -253,7 +293,8 @@ export default class PostsController extends Controller {
return res.json({ return res.json({
success: true, success: true,
}) })
}), })
},
} }
post = { post = {
@ -303,7 +344,9 @@ export default class PostsController extends Controller {
} }
delete = { delete = {
"/post": Schematized({ "/post": {
middlewares: ["withAuthentification"],
fn: Schematized({
required: ["post_id"], required: ["post_id"],
select: ["post_id"], select: ["post_id"],
}, async (req, res) => { }, async (req, res) => {
@ -321,6 +364,7 @@ export default class PostsController extends Controller {
message: err.message, message: err.message,
}) })
}) })
}), })
},
} }
} }