use new routes

This commit is contained in:
SrGooglo 2022-12-09 12:27:56 +00:00
parent 09a53555a5
commit 741ce66df5
4 changed files with 149 additions and 119 deletions

View File

@ -16,11 +16,25 @@ export default class Post {
throw new Error("Post ID is required")
}
const request = Post.bridge.get.post(undefined, {
post_id,
const { data } = await app.api.customRequest("main", {
method: "GET",
url: `/posts/${post_id}`,
})
return request
return data
}
static async getPostComments({ post_id }) {
if (!post_id) {
throw new Error("Post ID is required")
}
const { data } = await app.api.customRequest("main", {
method: "GET",
url: `/posts/${post_id}/comments`,
})
return data
}
static async sendComment({ post_id, comment }) {
@ -30,7 +44,7 @@ export default class Post {
const request = await app.api.customRequest("main", {
method: "POST",
url: `/post/${post_id}/comment`,
url: `/posts/${post_id}/comment`,
data: {
message: comment,
},
@ -46,7 +60,7 @@ export default class Post {
const request = await app.api.customRequest("main", {
method: "DELETE",
url: `/post/${post_id}/comment/${comment_id}`,
url: `/posts/${post_id}/comment/${comment_id}`,
})
return request
@ -57,25 +71,16 @@ export default class Post {
throw new Error("Bridge is not available")
}
const request = Post.bridge.get.explorePosts(undefined, {
trim: trim ?? 0,
limit: limit ?? window.app.settings.get("feed_max_fetch"),
const { data } = await app.api.customRequest("main", {
method: "GET",
url: `/posts/explore`,
params: {
trim: trim ?? 0,
limit: limit ?? window.app.settings.get("feed_max_fetch"),
}
})
return request
}
static async getFeed({ trim, limit }) {
if (!Post.bridge) {
throw new Error("Bridge is not available")
}
const request = Post.bridge.get.feed(undefined, {
trim: trim ?? 0,
limit: limit ?? window.app.settings.get("feed_max_fetch"),
})
return request
return data
}
static async getSavedPosts({ trim, limit }) {
@ -83,12 +88,16 @@ export default class Post {
throw new Error("Bridge is not available")
}
const request = Post.bridge.get.savedPosts(undefined, {
trim: trim ?? 0,
limit: limit ?? window.app.settings.get("feed_max_fetch"),
const { data } = await app.api.customRequest("main", {
method: "GET",
url: `/posts/saved`,
params: {
trim: trim ?? 0,
limit: limit ?? window.app.settings.get("feed_max_fetch"),
}
})
return request
return data
}
static async getUserPosts({ user_id, trim, limit }) {
@ -101,12 +110,80 @@ export default class Post {
user_id = app.userData?._id
}
const request = Post.bridge.get.userPosts(undefined, {
user_id,
trim: trim ?? 0,
limit: limit ?? window.app.settings.get("feed_max_fetch"),
const { data } = await app.api.customRequest("main", {
method: "GET",
url: `/posts/user/${user_id}`,
params: {
trim: trim ?? 0,
limit: limit ?? window.app.settings.get("feed_max_fetch"),
}
})
return request
return data
}
static async toogleLike({ post_id }) {
if (!Post.bridge) {
throw new Error("Bridge is not available")
}
if (!post_id) {
throw new Error("Post ID is required")
}
const { data } = await app.api.customRequest("main", {
method: "POST",
url: `/posts/${post_id}/toogle_like`,
})
return data
}
static async toogleSave({ post_id }) {
if (!Post.bridge) {
throw new Error("Bridge is not available")
}
if (!post_id) {
throw new Error("Post ID is required")
}
const { data } = await app.api.customRequest("main", {
method: "POST",
url: `/posts/${post_id}/toogle_save`,
})
return data
}
static async create(payload) {
if (!Post.bridge) {
throw new Error("Bridge is not available")
}
const { data } = await app.api.customRequest("main", {
method: "POST",
url: `/posts/new`,
data: payload,
})
return data
}
static async deletePost({ post_id }) {
if (!Post.bridge) {
throw new Error("Bridge is not available")
}
if (!post_id) {
throw new Error("Post ID is required")
}
const { data } = await app.api.customRequest("main", {
method: "DELETE",
url: `/posts/${post_id}`,
})
return data
}
}

View File

@ -1,5 +1,4 @@
import { Controller } from "linebridge/dist/server"
import { User, Post, Comment } from "../../models"
import { Schematized } from "../../lib"
import getComments from "./methods/getComments"
@ -10,7 +9,7 @@ export default class CommentsController extends Controller {
static refName = "CommentsController"
get = {
"/post/:post_id/comments": {
"/posts/:post_id/comments": {
fn: async (req, res) => {
const { post_id } = req.params
@ -30,7 +29,7 @@ export default class CommentsController extends Controller {
}
post = {
"/post/:post_id/comment": {
"/posts/:post_id/comment": {
middlewares: ["withAuthentication"],
fn: Schematized({
required: ["message"],
@ -57,7 +56,7 @@ export default class CommentsController extends Controller {
}
delete = {
"/post/:post_id/comment/:comment_id": {
"/posts/:post_id/comment/:comment_id": {
middlewares: ["withAuthentication"],
fn: async (req, res) => {
const result = await deleteComment({

View File

@ -1,19 +1,23 @@
import { Controller } from "linebridge/dist/server"
import { Schematized } from "../../lib"
import getPosts from "./methods/getPosts"
export default class FeedController extends Controller {
static refName = "FeedController"
static useRoute = "/feed"
get = {
"/feed": {
middlewares: ["withOptionalAuthentication"],
fn: Schematized({
select: ["user_id"]
}, async (req, res) => {
"/posts": {
middlewares: ["withAuthentication"],
fn: async (req, res) => {
const for_user_id = req.user?._id.toString()
if (!for_user_id) {
return res.status(400).json({
error: "Invalid user id"
})
}
let feed = []
// fetch posts
@ -26,7 +30,7 @@ export default class FeedController extends Controller {
feed = feed.concat(posts)
return res.json(feed)
})
}
}
}
}

View File

@ -5,10 +5,10 @@ import { CreatePost, ToogleLike, GetPostData, DeletePost, ToogleSavePost } from
export default class PostsController extends Controller {
static refName = "PostsController"
//static useMiddlewares = ["withAuthentication"]
static useRoute = "/posts"
get = {
"/explore/posts": {
"/explore": {
middlewares: ["withOptionalAuthentication"],
fn: Schematized({
select: ["user_id"]
@ -23,7 +23,7 @@ export default class PostsController extends Controller {
return res.json(posts)
})
},
"/saved_posts": {
"/saved": {
middlewares: ["withOptionalAuthentication"],
fn: Schematized({
select: ["user_id"]
@ -38,30 +38,26 @@ export default class PostsController extends Controller {
return res.json(posts)
})
},
"/user_posts": {
"/user/:user_id": {
middlewares: ["withOptionalAuthentication"],
fn: Schematized({
required: ["user_id"],
select: ["user_id"]
}, async (req, res) => {
fn: async (req, res) => {
let posts = await GetPostData({
limit: req.query?.limit,
skip: req.query?.trim,
for_user_id: req.user?._id.toString(),
from_user_id: req.query?.user_id,
from_user_id: req.params.user_id,
})
console.log(req.query)
return res.json(posts)
})
}
},
"/post": {
"/:post_id": {
middlewares: ["withOptionalAuthentication"],
fn: Schematized({
select: ["post_id"],
required: ["post_id"]
}, async (req, res) => {
fn: async (req, res) => {
let post = await GetPostData({
post_id: req.query?.post_id,
post_id: req.params.post_id,
for_user_id: req.user?._id.toString(),
}).catch((error) => {
res.status(404).json({ error: error.message })
@ -72,16 +68,22 @@ export default class PostsController extends Controller {
if (!post) return
return res.json(post)
})
}
},
}
put = {
"/:post_id": {
middlewares: ["withAuthentication"],
fn: (req, res) => {
// TODO: Implement Post update
return res.status(501).json({ error: "Not implemented" })
}
}
}
post = {
"/post": {
"/new": {
middlewares: ["withAuthentication"],
fn: Schematized({
required: ["timestamp"],
@ -99,32 +101,7 @@ export default class PostsController extends Controller {
return res.json(post)
})
},
"/toogle_like": {
middlewares: ["withAuthentication"],
fn: Schematized({
required: ["post_id"],
select: ["post_id", "to"],
}, async (req, res) => {
const post = await ToogleLike({
user_id: req.user._id.toString(),
post_id: req.selection.post_id,
to: req.selection.to,
}).catch((err) => {
res.status(400).json({
error: err.message
})
return false
})
if (!post) return
return res.json({
success: true,
post
})
})
},
"/post/:post_id/toogle_like": {
"/:post_id/toogle_like": {
middlewares: ["withAuthentication"],
fn: Schematized({
select: ["to"],
@ -148,31 +125,7 @@ export default class PostsController extends Controller {
})
})
},
"/post/toogle_save": {
middlewares: ["withAuthentication"],
fn: Schematized({
required: ["post_id"],
select: ["post_id"],
}, async (req, res) => {
const post = await ToogleSavePost({
user_id: req.user._id.toString(),
post_id: req.selection.post_id,
}).catch((err) => {
res.status(400).json({
error: err.message
})
return false
})
if (!post) return
return res.json({
success: true,
post
})
})
},
"/post/:post_id/save": {
"/:post_id/toogle_save": {
middlewares: ["withAuthentication"],
fn: async (req, res) => {
const post = await ToogleSavePost({
@ -196,14 +149,11 @@ export default class PostsController extends Controller {
}
delete = {
"/post": {
"/:post_id": {
middlewares: ["withAuthentication"],
fn: Schematized({
required: ["post_id"],
select: ["post_id"],
}, async (req, res) => {
fn: async (req, res) => {
const post = await DeletePost({
post_id: req.selection.post_id,
post_id: req.params.post_id,
by_user_id: req.user._id.toString(),
}).catch((err) => {
res.status(400).json({
@ -219,7 +169,7 @@ export default class PostsController extends Controller {
success: true,
post
})
})
}
},
}
}