mirror of
https://github.com/ragestudio/comty.git
synced 2025-06-10 02:54:15 +00:00
declare middlewares every endpoint
This commit is contained in:
parent
3d034e9ccf
commit
eb75941d52
@ -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,118 +142,159 @@ export default class PostsController extends Controller {
|
|||||||
}
|
}
|
||||||
|
|
||||||
get = {
|
get = {
|
||||||
"/feed": Schematized({
|
"/feed": {
|
||||||
select: ["user_id"]
|
fn: Schematized({
|
||||||
}, async (req, res) => {
|
select: ["user_id"]
|
||||||
const feedLimit = req.query?.limit ?? 20
|
}, async (req, res) => {
|
||||||
const feedTrimIndex = req.query?.trim ?? 0
|
const feedLimit = req.query?.limit ?? 20
|
||||||
|
const feedTrimIndex = req.query?.trim ?? 0
|
||||||
|
|
||||||
// make sure that sort by date descending
|
// make sure that sort by date descending
|
||||||
// trim index is used to get the last n posts
|
// trim index is used to get the last n posts
|
||||||
let posts = await Post.find(req.selection)
|
let posts = await Post.find(req.selection)
|
||||||
.sort({ created_at: -1 })
|
.sort({ created_at: -1 })
|
||||||
.skip(feedTrimIndex)
|
.skip(feedTrimIndex)
|
||||||
.limit(feedLimit)
|
.limit(feedLimit)
|
||||||
|
|
||||||
|
// fetch and add user data to each post
|
||||||
|
posts = posts.map(async (post, index) => {
|
||||||
|
const user = await User.findById(post.user_id)
|
||||||
|
|
||||||
|
if (feedTrimIndex > 0) {
|
||||||
|
index = Number(feedTrimIndex) + Number(index)
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
...post.toObject(),
|
||||||
|
user: user.toObject(),
|
||||||
|
key: index,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
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"
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
// fetch and add user data to each post
|
|
||||||
posts = posts.map(async (post, index) => {
|
|
||||||
const user = await User.findById(post.user_id)
|
const user = await User.findById(post.user_id)
|
||||||
|
|
||||||
if (feedTrimIndex > 0) {
|
return res.json({
|
||||||
index = Number(feedTrimIndex) + Number(index)
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
...post.toObject(),
|
...post.toObject(),
|
||||||
user: user.toObject(),
|
user: user.toObject(),
|
||||||
key: index,
|
})
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
}
|
||||||
posts = await Promise.all(posts)
|
|
||||||
|
|
||||||
return res.json(posts)
|
|
||||||
}),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
put = {
|
put = {
|
||||||
"/post": Schematized({
|
"/post": {
|
||||||
required: ["message"],
|
middlewares: ["withAuthentification"],
|
||||||
select: ["message", "additions"],
|
fn: Schematized({
|
||||||
}, async (req, res) => {
|
required: ["message"],
|
||||||
const post = await this.methods.createPost({
|
select: ["message", "additions"],
|
||||||
user_id: req.user.id,
|
}, async (req, res) => {
|
||||||
message: req.selection.message,
|
const post = await this.methods.createPost({
|
||||||
additions: req.selection.additions,
|
user_id: req.user.id,
|
||||||
})
|
message: req.selection.message,
|
||||||
|
additions: req.selection.additions,
|
||||||
return res.json(post)
|
|
||||||
}),
|
|
||||||
"/toogle_like": Schematized({
|
|
||||||
required: ["post_id"],
|
|
||||||
select: ["post_id"],
|
|
||||||
}, async (req, res) => {
|
|
||||||
const post = await this.methods.toogleLike({
|
|
||||||
user_id: req.user._id.toString(),
|
|
||||||
post_id: req.selection.post_id,
|
|
||||||
}).catch((err) => {
|
|
||||||
return false
|
|
||||||
})
|
|
||||||
|
|
||||||
if (!post) {
|
|
||||||
return res.json({
|
|
||||||
error: err.message,
|
|
||||||
success: false
|
|
||||||
})
|
})
|
||||||
}
|
|
||||||
|
|
||||||
return res.json({
|
return res.json(post)
|
||||||
success: true,
|
|
||||||
post
|
|
||||||
})
|
})
|
||||||
}),
|
},
|
||||||
"/like": Schematized({
|
"/toogle_like": {
|
||||||
required: ["post_id"],
|
middlewares: ["withAuthentification"],
|
||||||
select: ["post_id"],
|
fn: Schematized({
|
||||||
}, async (req, res) => {
|
required: ["post_id"],
|
||||||
const post = await this.methods.likePost({
|
select: ["post_id"],
|
||||||
user_id: req.user._id.toString(),
|
}, async (req, res) => {
|
||||||
post_id: req.selection.post_id,
|
const post = await this.methods.toogleLike({
|
||||||
}).catch((err) => {
|
user_id: req.user._id.toString(),
|
||||||
return false
|
post_id: req.selection.post_id,
|
||||||
})
|
}).catch((err) => {
|
||||||
|
return false
|
||||||
if (!post) {
|
|
||||||
return res.json({
|
|
||||||
success: false,
|
|
||||||
})
|
})
|
||||||
}
|
|
||||||
|
|
||||||
return res.json({
|
if (!post) {
|
||||||
success: true,
|
return res.json({
|
||||||
})
|
error: err.message,
|
||||||
}),
|
success: false
|
||||||
"/unlike": Schematized({
|
})
|
||||||
required: ["post_id"],
|
}
|
||||||
select: ["post_id"],
|
|
||||||
}, async (req, res) => {
|
|
||||||
const post = await this.methods.unlikePost({
|
|
||||||
user_id: req.user._id.toString(),
|
|
||||||
post_id: req.selection.post_id,
|
|
||||||
}).catch((err) => {
|
|
||||||
return false
|
|
||||||
})
|
|
||||||
|
|
||||||
if (!post) {
|
|
||||||
return res.json({
|
return res.json({
|
||||||
success: false,
|
success: true,
|
||||||
|
post
|
||||||
})
|
})
|
||||||
}
|
|
||||||
|
|
||||||
return res.json({
|
|
||||||
success: true,
|
|
||||||
})
|
})
|
||||||
}),
|
},
|
||||||
|
"/like": {
|
||||||
|
middlewares: ["withAuthentification"],
|
||||||
|
fn: Schematized({
|
||||||
|
required: ["post_id"],
|
||||||
|
select: ["post_id"],
|
||||||
|
}, async (req, res) => {
|
||||||
|
const post = await this.methods.likePost({
|
||||||
|
user_id: req.user._id.toString(),
|
||||||
|
post_id: req.selection.post_id,
|
||||||
|
}).catch((err) => {
|
||||||
|
return false
|
||||||
|
})
|
||||||
|
|
||||||
|
if (!post) {
|
||||||
|
return res.json({
|
||||||
|
success: false,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return res.json({
|
||||||
|
success: true,
|
||||||
|
})
|
||||||
|
})
|
||||||
|
},
|
||||||
|
"/unlike": {
|
||||||
|
middlewares: ["withAuthentification"],
|
||||||
|
fn: Schematized({
|
||||||
|
required: ["post_id"],
|
||||||
|
select: ["post_id"],
|
||||||
|
}, async (req, res) => {
|
||||||
|
const post = await this.methods.unlikePost({
|
||||||
|
user_id: req.user._id.toString(),
|
||||||
|
post_id: req.selection.post_id,
|
||||||
|
}).catch((err) => {
|
||||||
|
return false
|
||||||
|
})
|
||||||
|
|
||||||
|
if (!post) {
|
||||||
|
return res.json({
|
||||||
|
success: false,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return res.json({
|
||||||
|
success: true,
|
||||||
|
})
|
||||||
|
})
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
post = {
|
post = {
|
||||||
@ -303,24 +344,27 @@ export default class PostsController extends Controller {
|
|||||||
}
|
}
|
||||||
|
|
||||||
delete = {
|
delete = {
|
||||||
"/post": Schematized({
|
"/post": {
|
||||||
required: ["post_id"],
|
middlewares: ["withAuthentification"],
|
||||||
select: ["post_id"],
|
fn: Schematized({
|
||||||
}, async (req, res) => {
|
required: ["post_id"],
|
||||||
await this.methods.deletePost({
|
select: ["post_id"],
|
||||||
post_id: req.selection.post_id,
|
}, async (req, res) => {
|
||||||
user_id: req.user._id.toString(),
|
await this.methods.deletePost({
|
||||||
|
post_id: req.selection.post_id,
|
||||||
|
user_id: req.user._id.toString(),
|
||||||
|
})
|
||||||
|
.then(() => {
|
||||||
|
return res.json({
|
||||||
|
success: true,
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
return res.status(500).json({
|
||||||
|
message: err.message,
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
.then(() => {
|
},
|
||||||
return res.json({
|
|
||||||
success: true,
|
|
||||||
})
|
|
||||||
})
|
|
||||||
.catch((err) => {
|
|
||||||
return res.status(500).json({
|
|
||||||
message: err.message,
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user