All files / src/controllers posts_controller.ts

82.47% Statements 80/97
31.25% Branches 5/16
100% Functions 5/5
82.47% Lines 80/97

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 971x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x   1x 1x 1x 1x     1x 1x 1x 1x 1x 1x   1x 1x 1x 1x     1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x     1x     1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x     1x
import { Request, Response } from 'express';
import * as postsService from '../services/posts_service';
import { handleError } from '../utils/handle_error';
import {CustomRequest} from "types/customRequest";
import {PostData} from "types/post_types";
import {postExists} from "../services/posts_service";
 
export const addPost = async (req: CustomRequest, res: Response): Promise<void> => {
    try {
        const postData: PostData = {
            title: req.body.title,
            content: req.body.content,
            owner: req.user.id
        };
 
        const savedPost: PostData = await postsService.addPost(postData);
 
        res.status(201).json(savedPost);
    } catch (err) {
        handleError(err, res);
    }
};
 
export const getPosts = async (req: Request, res: Response): Promise<void> => {
    try {
        let posts;
        if (req.query.sender) {
            posts = await postsService.getPosts(req.query.sender as string);
        } else {
            posts = await postsService.getPosts();
        }
 
        if (posts.length === 0) {
            res.status(204).json({ message: 'No posts found' });
        } else {
            res.json(posts);
        }
    } catch (err) {
        handleError(err, res);
    }
};
 
export const getPostById = async (req: Request, res: Response): Promise<void> => {
    try {
        const post = await postsService.getPostById(req.params.post_id);
        if (!post) {
            res.status(404).json({ message: 'Post not found' });
        } else {
            res.json(post);
        }
    } catch (err) {
        handleError(err, res);
    }
};
 
export const updatePost = async (req: CustomRequest, res: Response): Promise<void> => {
    try {
        const owner = req.user.id;
        const post_id = req.params.post_id;
        const postData: PostData = {
            title: req.body.title,
            content: req.body.content,
            owner
        };
 
        if (await postsService.postExists(post_id)) {
 
            if (await postsService.isPostOwnedByUser(post_id, owner)) {
 
                const updatedPost = await postsService.updatePost(post_id, postData);
                if (!updatedPost) {
                    res.status(404).json({message: 'Post not found'});
                } else {
                    res.json(updatedPost);
                }
            }
        } else { // If someone not the owner or try to update post that doesnt exists
            res.status(403).json({message: 'Forbidden'});
        }
    } catch (err) {
        handleError(err, res);
    }
};
 
 
export const deletePostById = async (req: Request, res: Response): Promise<void> => {
    try {
        const post = await postsService.getPostById(req.params.post_id);
        if (!post) {
            res.status(404).json({ message: 'Post not found' });
        } else {
            res.json({ message: 'Post deleted successfully' });
        }
    } catch (err) {
        handleError(err, res);
    }
};