All files / src/app article.js

87.5% Statements 28/32
100% Branches 6/6
83.33% Functions 5/6
87.5% Lines 28/32

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                  4x   4x       6x   6x 6x 6x     6x 6x               6x       7x   7x 3x     4x             9x   9x 2x     7x 7x 7x   7x   7x       10x   10x 3x     7x   7x       4x   4x                  
import Article from "@models/article.model.js";
import { CreateArticleOutputDto } from "@common/dto/article/create.dto.js";
import { ReadArticleOutputDto } from "@common/dto/article/read.dto.js";
import { UpdateArticleOutputDto } from "@common/dto/article/update.dto.js";
import { DeleteArticleOutputDto } from "@common/dto/article/delete.dto.js";
import { ListArticleOutputDto } from "@common/dto/article/list.dto.js";
import ArticleRepository from "@repositories/article.repository.js";
 
import { newLogger } from "@common/utils/logger.js";
const logger = newLogger("Service|Article");
 
const repository = new ArticleRepository();
 
export default {
    create: async ({ title, body, tags }) => {
        const article = await new Article();
 
        article.title = title;
        article.body = body;
        article.tags = tags;
 
        // TODO: user_id and timezone must be included after authentication is implemented
        try {
            await article.save();
        } catch (error) {
            logger.error("%s", error);
 
            //error.errors.properties.message
            return new CreateArticleOutputDto({}, false, JSON.stringify(error.errors));
        }
 
        return new CreateArticleOutputDto(article);
    },
 
    read: async ({ id }) => {
        const article = await new Article(id);
 
        if (!article._id) {
            return new ReadArticleOutputDto({}, false, "Article not found");
        }
 
        return new ReadArticleOutputDto(article, true, "Article found");
    },
 
    // Receives object with id, title, body and tags
    // If id exists: returns object with data (_id, title, body and tags) and a feedback message
    // If id doesn't exist: returns object with data (false) and a feedback message
    update: async ({ id, title, body, tags }) => {
        const article = await new Article(id);
 
        if (!article._id) {
            return new UpdateArticleOutputDto({}, false, "Article not found");
        }
 
        article.title = title;
        article.body = body;
        article.tags = tags;
 
        await article.save();
 
        return new UpdateArticleOutputDto(article, true, "Article updated successfully");
    },
 
    delete: async ({ id }) => {
        const article = await new Article(id);
 
        if (!article._id) {
            return new DeleteArticleOutputDto({}, false, "Article not found");
        }
 
        article.delete();
 
        return new DeleteArticleOutputDto({}, true, "Article deleted successfully");
    },
 
    list: async ({ title, tags }) => {
        const articles = await repository.filter(title, tags);
 
        return new ListArticleOutputDto(articles, true, "Articles fetched");
    },
 
    distinct: async (field) => {
        const result = await new Article();
 
        return result.distinct(field);
    },
};