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 { 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 { 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 { 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 { 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 { 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 }); } } }