frontend
This commit is contained in:
@@ -6,7 +6,7 @@ import { Post } from "../entity/Post";
|
||||
class PostController {
|
||||
async create(req: Request, res: Response): Promise<void> {
|
||||
const post = res.locals.post;
|
||||
AppDataSource.manager.save(post);
|
||||
await AppDataSource.manager.save(post);
|
||||
|
||||
res.status(200).send("Ok");
|
||||
}
|
||||
@@ -15,7 +15,7 @@ class PostController {
|
||||
const { postId } = req.params;
|
||||
|
||||
const post = res.locals.post;
|
||||
AppDataSource.manager.update(Post, { id: postId }, post);
|
||||
await AppDataSource.manager.update(Post, { id: postId }, post);
|
||||
|
||||
res.status(200).send("Ok");
|
||||
}
|
||||
@@ -23,10 +23,18 @@ class PostController {
|
||||
async delete(req: Request, res: Response): Promise<void> {
|
||||
const { postId } = req.params;
|
||||
|
||||
AppDataSource.manager.delete(Post, { id: postId });
|
||||
await AppDataSource.manager.delete(Post, { id: postId });
|
||||
|
||||
res.status(200).send("Ok");
|
||||
}
|
||||
|
||||
async get(req: Request, res: Response): Promise<void> {
|
||||
const { postId } = req.params;
|
||||
|
||||
const post = await AppDataSource.manager.findOneBy(Post, {id: Number.parseInt(postId)});
|
||||
|
||||
res.status(200).send(post);
|
||||
}
|
||||
}
|
||||
|
||||
export default new PostController();
|
||||
@@ -36,7 +36,15 @@ class UserController {
|
||||
async getPosts(req: Request, res: Response): Promise<void> {
|
||||
const { userId } = req.params;
|
||||
|
||||
const posts = await AppDataSource.manager.findBy(Post, { authorId: Number.parseInt(userId) });
|
||||
const posts = await AppDataSource.manager.find(Post,
|
||||
{
|
||||
where:
|
||||
{ authorId: Number.parseInt(userId) },
|
||||
order: {
|
||||
date: "DESC"
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
res.status(200).send(posts);
|
||||
}
|
||||
|
||||
@@ -8,43 +8,52 @@ import { AppDataSource } from "../data-source";
|
||||
|
||||
// Updates or creates a post and handles things like deleting old post's media
|
||||
const handlePostData = async (req: Request, res: Response, next: NextFunction): Promise<void> => {
|
||||
const token = req.cookies.jwt;
|
||||
const user = (jwt.decode(token) as JwtPayload)
|
||||
try {
|
||||
const token = req.cookies.jwt;
|
||||
const user = (jwt.decode(token) as JwtPayload)
|
||||
|
||||
const post = new Post();
|
||||
const post = new Post();
|
||||
|
||||
const { message } = req.body;
|
||||
if (req.method == "PUT") {
|
||||
// Delete old post data if it was media
|
||||
const postToUpdate = (await AppDataSource.manager.findOneBy(Post, { id: Number.parseInt(req.params.postId) }));
|
||||
const { message } = req.body;
|
||||
if (req.method == "PUT") {
|
||||
// Delete old post data if it was media
|
||||
const postToUpdate = (await AppDataSource.manager.findOneBy(Post, { id: Number.parseInt(req.params.postId) }));
|
||||
|
||||
if (postToUpdate.type == 1) {
|
||||
const filename = postToUpdate.message;
|
||||
fs.unlinkSync(`${process.env.UPLOAD_DESTINATION}/${filename}`);
|
||||
if (postToUpdate.type == 1) {
|
||||
const filename = postToUpdate.message;
|
||||
fs.unlinkSync(`${process.env.UPLOAD_DESTINATION}/${filename}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (req.file) {
|
||||
const extension = path.extname(req.file.originalname).toLowerCase()
|
||||
if ([".png", ".jpg", ".jpeg", ".webp", ".mp4", ".webm"].indexOf(extension) < 0) {
|
||||
res.status(400).send("Unknown mime type");
|
||||
return;
|
||||
|
||||
if (req.file) {
|
||||
const extension = path.extname(req.file.originalname).toLowerCase()
|
||||
if ([".png", ".jpg", ".jpeg", ".webp", ".mp4", ".webm"].indexOf(extension) < 0) {
|
||||
res.status(400).send("Unknown mime type");
|
||||
return;
|
||||
}
|
||||
const buffer = fs.readFileSync(req.file.path);
|
||||
const hash = crypto.createHash('md5');
|
||||
hash.update(buffer);
|
||||
const newFilename = `${hash.digest('hex')}${extension}`;
|
||||
fs.copyFileSync(`./${req.file.path}`, `${process.env.UPLOAD_DESTINATION}/${newFilename}`);
|
||||
fs.unlinkSync(`./${req.file.path}`);
|
||||
post.message = newFilename;
|
||||
post.type = 1;
|
||||
} else {
|
||||
if (!post.message) {
|
||||
res.status(400).send("Post message is not specified");
|
||||
return;
|
||||
}
|
||||
post.type = 0;
|
||||
post.message = message;
|
||||
}
|
||||
const buffer = fs.readFileSync(req.file.path);
|
||||
const hash = crypto.createHash('md5');
|
||||
hash.update(buffer);
|
||||
const newFilename = `${hash.digest('hex')}${extension}`;
|
||||
fs.copyFileSync(`./${req.file.path}`, `${process.env.UPLOAD_DESTINATION}/${newFilename}`);
|
||||
fs.unlinkSync(`./${req.file.path}`);
|
||||
post.message = newFilename;
|
||||
post.type = 1;
|
||||
} else {
|
||||
post.type = 0;
|
||||
post.message = message;
|
||||
if (req.method == "POST") post.date = new Date().toISOString();
|
||||
post.authorId = user.id;
|
||||
res.locals.post = post;
|
||||
next();
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
}
|
||||
if (req.method == "POST") post.date = new Date().toISOString();
|
||||
post.authorId = user.id;
|
||||
res.locals.post = post;
|
||||
next();
|
||||
}
|
||||
|
||||
export default { handlePostData };
|
||||
@@ -18,6 +18,7 @@ const upload = multer({
|
||||
|
||||
PostRouter.post('/create', auth.authenticate, upload.single("file"), utils.handlePostData, PostController.create);
|
||||
PostRouter.put('/update/:postId', auth.authorizeForPost, existance.postShouldExist, upload.single("file"), utils.handlePostData, PostController.update);
|
||||
PostRouter.delete('/delete/:postId', auth.authorizeForPost, existance.postShouldExist, PostController.delete);
|
||||
PostRouter.delete('/delete/:postId', existance.postShouldExist, auth.authorizeForPost, PostController.delete);
|
||||
PostRouter.get('/:postId', existance.postShouldExist, PostController.get);
|
||||
|
||||
export default PostRouter;
|
||||
Reference in New Issue
Block a user