posts-controller.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. import { HttpResourceController } from "../http-resource-controller";
  2. import { Response } from "../http-petition";
  3. import Log from "../../libs/log";
  4. import Db from "../../data-storage/db";
  5. import { getAuthor } from "../../use-cases/author";
  6. import {
  7. getPost,
  8. allPosts,
  9. addPost,
  10. updatePost,
  11. deletePost
  12. } from "../../use-cases/post";
  13. import AuthorDb from "../../data-storage/db/author";
  14. import PostDb from "../../data-storage/db/post";
  15. export default class PostsController extends HttpResourceController {
  16. private _db: Db;
  17. private _log: Log;
  18. constructor() {
  19. super();
  20. this._db = new Db();
  21. this._log = Log.instance;
  22. }
  23. public async index(): Promise<Response> {
  24. try {
  25. const postDb = new PostDb(this._db);
  26. const posts = await allPosts.get(postDb).catch(err => {
  27. throw err;
  28. });
  29. return this.success({
  30. status: "OK",
  31. statusCode: 200,
  32. payload: posts
  33. });
  34. } catch (err) {
  35. this._log.logger.error("Ocurrió un error", err);
  36. return this.error({
  37. error: err.message,
  38. status: "NO_OK",
  39. statusCode: 422
  40. });
  41. }
  42. }
  43. public async store(): Promise<Response> {
  44. try {
  45. const authorDb = new AuthorDb(this._db);
  46. const author = await getAuthor
  47. .retrieve(this.request.payload.authorId, authorDb)
  48. .catch(err => {
  49. throw err;
  50. });
  51. const postDb = new PostDb(this._db);
  52. const post = await addPost
  53. .save({ ...this.request.payload, author }, postDb)
  54. .catch(err => {
  55. throw err;
  56. });
  57. return this.success({
  58. status: "OK",
  59. statusCode: 200,
  60. payload: post
  61. });
  62. } catch (err) {
  63. this._log.logger.error("An error has occurred:", err.message);
  64. return this.error({
  65. error: err.message,
  66. status: "NO_OK",
  67. statusCode: 422
  68. });
  69. }
  70. }
  71. public async show(): Promise<Response> {
  72. try {
  73. const postDb = new PostDb(this._db);
  74. const post = await getPost
  75. .byId(Number(this.request.params.id), postDb)
  76. .catch(err => {
  77. throw err;
  78. });
  79. const authorDb = new AuthorDb(this._db);
  80. const author = await getAuthor
  81. .retrieve(post.authorId, authorDb)
  82. .catch(err => {
  83. throw err;
  84. });
  85. post.author = author;
  86. return this.success({
  87. status: "OK",
  88. statusCode: 200,
  89. payload: post
  90. });
  91. } catch (err) {
  92. this._log.logger.error("An error has occurred", err);
  93. return this.error({
  94. error: err.message,
  95. status: "NO_OK",
  96. statusCode: 422
  97. });
  98. }
  99. }
  100. public async update(): Promise<Response> {
  101. try {
  102. const postDb = new PostDb(this._db);
  103. const post = await getPost
  104. .byId(Number(this.request.params.id), postDb)
  105. .catch(err => {
  106. throw err;
  107. });
  108. const authorDb = new AuthorDb(this._db);
  109. const author = await getAuthor
  110. .retrieve(this.request.payload.authorId, authorDb)
  111. .catch(err => {
  112. throw err;
  113. });
  114. post.id = this.request.params.id;
  115. post.author = author;
  116. await updatePost
  117. .save(post, { ...this.request.payload }, postDb)
  118. .catch(err => {
  119. throw err;
  120. });
  121. return this.success({
  122. status: "OK",
  123. statusCode: 200,
  124. payload: post
  125. });
  126. } catch (err) {
  127. this._log.logger.error("An error has occurred", err);
  128. return this.error({
  129. error: err.message,
  130. status: "NO_OK",
  131. statusCode: 422
  132. });
  133. }
  134. }
  135. public async destroy(): Promise<Response> {
  136. try {
  137. const postDb = new PostDb(this._db);
  138. const post = await getPost
  139. .byId(Number(this.request.params.id), postDb)
  140. .catch(err => {
  141. throw err;
  142. });
  143. await deletePost.save(post, postDb).catch(err => {
  144. throw err;
  145. });
  146. return this.success({
  147. status: "OK",
  148. statusCode: 200,
  149. payload: post.id
  150. });
  151. } catch (err) {
  152. this._log.logger.error("An error has occurred", err);
  153. return this.error({
  154. error: err.message,
  155. status: "NO_OK",
  156. statusCode: 422
  157. });
  158. }
  159. }
  160. }