All files / src/http article.controller.js

74.5% Statements 38/51
58.33% Branches 7/12
71.42% Functions 5/7
74.5% Lines 38/51

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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114                        3x 5x 5x 3x   3x       3x   2x       3x 5x 5x 5x   5x 2x     3x   2x       3x 6x 6x 4x   4x 1x     3x   3x       3x 5x 5x 5x   5x 2x     3x   2x       3x 3x 3x 3x   3x           3x                                           3x                
import { CreateArticleInputDto } from "@common/dto/article/create.dto.js";
import { ReadArticleInputDto } from "@common/dto/article/read.dto.js";
import { UpdateArticleInputDto } from "@common/dto/article/update.dto.js";
import { DeleteArticleInputDto } from "@common/dto/article/delete.dto.js";
import { ListArticleInputDto } from "@common/dto/article/list.dto.js";
import { HttpException } from "@common/exceptions/appExceptions.js";
 
import BaseController from "@controllers/controller.js";
import service from "@services/article.js";
import { routes } from "reversical";
 
class ArticleController extends BaseController {
    create = async (req, res) => {
        try {
            const dto = new CreateArticleInputDto(req.body);
            const result = await service.create(dto);
 
            Iif (!result.success) {
                throw new HttpException(400, result.info);
            }
 
            return res.redirect(routes.articleRead({ id: result.data._id.toString() }));
        } catch (error) {
            return this.reportBadData(error, req, res).redirect(routes.articleNew());
        }
    };
 
    read = async (req, res) => {
        try {
            const dto = new ReadArticleInputDto({ id: req.params.id });
            const result = await service.read(dto);
 
            if (!result.success) {
                throw new HttpException(404, result.info);
            }
 
            return res.status(200).render("article/index", result.data);
        } catch (error) {
            return this.reportBadData(error, req, res);
        }
    };
 
    update = async (req, res) => {
        try {
            const dto = new UpdateArticleInputDto({ id: req.params.id, ...req.body });
            const result = await service.update(dto);
 
            if (!result.success) {
                throw new HttpException(404, result.info);
            }
 
            return res.redirect(routes.articleRead({ id: result.data._id.toString() }));
        } catch (error) {
            return this.reportBadData(error, req, res).redirect(routes.articleEdit({ id: req.params.id }));
        }
    };
 
    delete = async (req, res) => {
        try {
            const dto = new DeleteArticleInputDto({ id: req.params.id });
            const result = await service.delete(dto);
 
            if (!result.success) {
                throw new HttpException(404, result.info);
            }
 
            return res.status(200).render("article/index", { title: result.info });
        } catch (error) {
            return this.reportBadData(error, req, res).redirect(routes.articleList());
        }
    };
 
    list = async (req, res) => {
        try {
            const dto = new ListArticleInputDto(req.query);
            const result = await service.list(dto);
 
            return res.status(200).render("article/list", { body: result.data, query: req.query });
        } catch (error) {
            return this.reportBadData(error, req, res);
        }
    };
 
    write = async (req, res) => {
        try {
            const dto = new ReadArticleInputDto({ id: req.params.id });
 
            if (typeof dto.id != "undefined") {
                // Update
                const result = await service.read(dto);
 
                if (!result.success) {
                    throw new HttpException(404, result.info);
                }
 
                return res.status(200).render("article/write", result.data);
            } else {
                // New
                return res.status(200).render("article/write");
            }
        } catch (error) {
            return this.reportBadData(error, req, res);
        }
    };
 
    tags = async (req, res) => {
        const result = await service.distinct("tags");
 
        return res.status(200).render("article/tags", { result });
    };
}
 
export default ArticleController;