contacts.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. const Contact = require('../model/contact');
  2. const UserModel = require('../model/user');
  3. const listContacts = async (req, res, next) => {
  4. try {
  5. const userId = req.user.id;
  6. const contacts = await Contact.getList(userId, req.query);
  7. return res.json({
  8. status: 'success',
  9. code: 200,
  10. data: {
  11. ...contacts,
  12. },
  13. });
  14. } catch (e) {
  15. next(e);
  16. }
  17. };
  18. const getContactById = async (req, res, next) => {
  19. try {
  20. const id = req.params.id;
  21. const userId = req.user.id;
  22. const contact = await Contact.getById(id, userId);
  23. if (contact)
  24. return res.json({
  25. status: 'success',
  26. code: 200,
  27. data: {
  28. contact,
  29. },
  30. });
  31. return res.status(404).json({
  32. status: 'error',
  33. code: 404,
  34. data: 'Not Found',
  35. });
  36. } catch (e) {
  37. next(e);
  38. }
  39. };
  40. const addContact = async (req, res, next) => {
  41. try {
  42. const { name, lastName, number, country, avatarUrl, color } =
  43. await UserModel.findByNumber(req.body.number);
  44. if (avatarUrl) {
  45. const userId = req.user.id;
  46. const newContact = await Contact.add({
  47. name,
  48. lastName,
  49. number,
  50. country,
  51. avatarUrl,
  52. color,
  53. owner: userId,
  54. });
  55. return res.status(201).json({
  56. status: 'success',
  57. code: 201,
  58. data: newContact,
  59. });
  60. }
  61. return res.status(404).json({
  62. status: 'error',
  63. code: 404,
  64. data: 'Not Found',
  65. });
  66. } catch (e) {
  67. next(e);
  68. }
  69. };
  70. const removeContact = async (req, res, next) => {
  71. try {
  72. const id = req.params.id;
  73. const userId = req.user.id;
  74. const contact = await Contact.remove(id, userId);
  75. if (contact) {
  76. return res.json({
  77. status: 'success',
  78. code: 200,
  79. data: {
  80. contact,
  81. },
  82. });
  83. } else {
  84. return res.status(404).json({
  85. status: 'error',
  86. code: 404,
  87. data: 'Not Found',
  88. });
  89. }
  90. } catch (e) {
  91. next(e);
  92. }
  93. };
  94. const updateContact = async (req, res, next) => {
  95. try {
  96. const id = req.params.id;
  97. const userId = req.user.id;
  98. const contact = await Contact.update(id, userId, req.body);
  99. if (contact) {
  100. return res.status(200).json({
  101. status: 'success',
  102. code: 200,
  103. data: {
  104. contact,
  105. },
  106. });
  107. } else {
  108. return res.status(404).json({
  109. status: 'error',
  110. code: 404,
  111. data: 'Not Found',
  112. });
  113. }
  114. } catch (e) {
  115. next(e);
  116. }
  117. };
  118. module.exports = {
  119. listContacts,
  120. getContactById,
  121. addContact,
  122. removeContact,
  123. updateContact,
  124. };