123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- import { HttpResourceController } from "../http-resource-controller";
- import { Response } from "../http-petition";
- import Log from "../../libs/log";
- import Db from "../../data-storage/db";
- import { getAuthor } from "../../use-cases/author";
- import {
- getPost,
- allPosts,
- addPost,
- updatePost,
- deletePost
- } from "../../use-cases/post";
- import AuthorDb from "../../data-storage/db/author";
- import PostDb from "../../data-storage/db/post";
- export default class PostsController extends HttpResourceController {
- private _db: Db;
- private _log: Log;
- constructor() {
- super();
- this._db = new Db();
- this._log = Log.instance;
- }
- public async index(): Promise<Response> {
- try {
- const postDb = new PostDb(this._db);
- const posts = await allPosts.get(postDb).catch(err => {
- throw err;
- });
- return this.success({
- status: "OK",
- statusCode: 200,
- payload: posts
- });
- } catch (err) {
- this._log.logger.error("Ocurrió un error", err);
- return this.error({
- error: err.message,
- status: "NO_OK",
- statusCode: 422
- });
- }
- }
- public async store(): Promise<Response> {
- try {
- const authorDb = new AuthorDb(this._db);
- const author = await getAuthor
- .retrieve(this.request.payload.authorId, authorDb)
- .catch(err => {
- throw err;
- });
- const postDb = new PostDb(this._db);
- const post = await addPost
- .save({ ...this.request.payload, author }, postDb)
- .catch(err => {
- throw err;
- });
- return this.success({
- status: "OK",
- statusCode: 200,
- payload: post
- });
- } catch (err) {
- this._log.logger.error("An error has occurred:", err.message);
- return this.error({
- error: err.message,
- status: "NO_OK",
- statusCode: 422
- });
- }
- }
- public async show(): Promise<Response> {
- try {
- const postDb = new PostDb(this._db);
- const post = await getPost
- .byId(Number(this.request.params.id), postDb)
- .catch(err => {
- throw err;
- });
- const authorDb = new AuthorDb(this._db);
- const author = await getAuthor
- .retrieve(post.authorId, authorDb)
- .catch(err => {
- throw err;
- });
- post.author = author;
- return this.success({
- status: "OK",
- statusCode: 200,
- payload: post
- });
- } catch (err) {
- this._log.logger.error("An error has occurred", err);
- return this.error({
- error: err.message,
- status: "NO_OK",
- statusCode: 422
- });
- }
- }
- public async update(): Promise<Response> {
- try {
- const postDb = new PostDb(this._db);
- const post = await getPost
- .byId(Number(this.request.params.id), postDb)
- .catch(err => {
- throw err;
- });
- const authorDb = new AuthorDb(this._db);
- const author = await getAuthor
- .retrieve(this.request.payload.authorId, authorDb)
- .catch(err => {
- throw err;
- });
- post.id = this.request.params.id;
- post.author = author;
- await updatePost
- .save(post, { ...this.request.payload }, postDb)
- .catch(err => {
- throw err;
- });
- return this.success({
- status: "OK",
- statusCode: 200,
- payload: post
- });
- } catch (err) {
- this._log.logger.error("An error has occurred", err);
- return this.error({
- error: err.message,
- status: "NO_OK",
- statusCode: 422
- });
- }
- }
- public async destroy(): Promise<Response> {
- try {
- const postDb = new PostDb(this._db);
- const post = await getPost
- .byId(Number(this.request.params.id), postDb)
- .catch(err => {
- throw err;
- });
- await deletePost.save(post, postDb).catch(err => {
- throw err;
- });
- return this.success({
- status: "OK",
- statusCode: 200,
- payload: post.id
- });
- } catch (err) {
- this._log.logger.error("An error has occurred", err);
- return this.error({
- error: err.message,
- status: "NO_OK",
- statusCode: 422
- });
- }
- }
- }
|