Advertisement
RaphCpp

Untitled

Mar 29th, 2022
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. import { commentService } from "./comment.service.js";
  2. import { ResourceNotFoundError } from "../common/repository-error.js";
  3.  
  4. class CommentController {
  5. findAll = (request, response) => {
  6. const items = commentService.findAll();
  7. response.header("x-total-count", `${items.length}`);
  8. response.json(items);
  9. };
  10.  
  11. find = (request, response, next) => {
  12. const id = request.params.id;
  13. const item = commentService
  14. .findById(id)
  15. .then(() => {
  16. response.json(item);
  17. })
  18. .catch((error) => {
  19. if (error instanceof ResourceNotFoundError) {
  20. next({ code: 404, details: `Resource with id=${id} does not exist` });
  21. } else {
  22. next(error);
  23. }
  24. });
  25. };
  26.  
  27. create = (request, response) => {
  28. const item = commentService.create(request.body);
  29. response.status(201);
  30. response.json(item);
  31. };
  32.  
  33. patch = (request, response, next) => {
  34. const id = request.params.id;
  35. const item = commentService
  36. .patch(id, request.body)
  37. .then(() => {
  38. response.json(item);
  39. })
  40. .catch((error) => {
  41. if (error instanceof ResourceNotFoundError) {
  42. next({ code: 404, details: `Resource with id=${id} does not exist` });
  43. } else {
  44. next(error);
  45. }
  46. });
  47. };
  48.  
  49. set = (request, response, next) => {
  50. const id = request.params.id;
  51. commentService
  52. .set(id)
  53. .then(() => {
  54. response.json(item);
  55. })
  56. .catch((error) => {
  57. if (error instanceof ResourceNotFoundError) {
  58. next({ code: 404, details: `Resource with id=${id} does not exist` });
  59. } else {
  60. next(error);
  61. }
  62. });
  63. };
  64.  
  65. delete = (request, response, next) => {
  66. const id = request.params.id;
  67. commentService
  68. .delete(id)
  69. .then(() => {
  70. response.status(204);
  71. response.json();
  72. })
  73. .catch((error) => {
  74. if (error instanceof ResourceNotFoundError) {
  75. next({ code: 404, details: `Resource with id=${id} does not exist` });
  76. } else {
  77. next(error);
  78. }
  79. });
  80. };
  81. }
  82.  
  83. export const commentController = new CommentController();
  84.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement