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 {
static refName = "PostsController"
static useMiddlewares = ["withAuthentication"]
//static useMiddlewares = ["withAuthentication"]
methods = {
createPost: async (payload) => {
@ -142,7 +142,8 @@ export default class PostsController extends Controller {
}
get = {
"/feed": Schematized({
"/feed": {
fn: Schematized({
select: ["user_id"]
}, async (req, res) => {
const feedLimit = req.query?.limit ?? 20
@ -173,11 +174,41 @@ export default class PostsController extends Controller {
posts = await Promise.all(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 = {
"/post": Schematized({
"/post": {
middlewares: ["withAuthentification"],
fn: Schematized({
required: ["message"],
select: ["message", "additions"],
}, async (req, res) => {
@ -188,8 +219,11 @@ export default class PostsController extends Controller {
})
return res.json(post)
}),
"/toogle_like": Schematized({
})
},
"/toogle_like": {
middlewares: ["withAuthentification"],
fn: Schematized({
required: ["post_id"],
select: ["post_id"],
}, async (req, res) => {
@ -211,8 +245,11 @@ export default class PostsController extends Controller {
success: true,
post
})
}),
"/like": Schematized({
})
},
"/like": {
middlewares: ["withAuthentification"],
fn: Schematized({
required: ["post_id"],
select: ["post_id"],
}, async (req, res) => {
@ -232,8 +269,11 @@ export default class PostsController extends Controller {
return res.json({
success: true,
})
}),
"/unlike": Schematized({
})
},
"/unlike": {
middlewares: ["withAuthentification"],
fn: Schematized({
required: ["post_id"],
select: ["post_id"],
}, async (req, res) => {
@ -253,7 +293,8 @@ export default class PostsController extends Controller {
return res.json({
success: true,
})
}),
})
},
}
post = {
@ -303,7 +344,9 @@ export default class PostsController extends Controller {
}
delete = {
"/post": Schematized({
"/post": {
middlewares: ["withAuthentification"],
fn: Schematized({
required: ["post_id"],
select: ["post_id"],
}, async (req, res) => {
@ -321,6 +364,7 @@ export default class PostsController extends Controller {
message: err.message,
})
})
}),
})
},
}
}