contacts.js.html 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <title>Code coverage report for nodejs-homework-API/controllers/contacts.js</title>
  5. <meta charset="utf-8" />
  6. <link rel="stylesheet" href="../../prettify.css" />
  7. <link rel="stylesheet" href="../../base.css" />
  8. <link rel="shortcut icon" type="image/x-icon" href="../../favicon.png" />
  9. <meta name="viewport" content="width=device-width, initial-scale=1" />
  10. <style type='text/css'>
  11. .coverage-summary .sorter {
  12. background-image: url(../../sort-arrow-sprite.png);
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <div class='wrapper'>
  18. <div class='pad1'>
  19. <h1><a href="../../index.html">All files</a> / <a href="index.html">nodejs-homework-API/controllers</a> contacts.js</h1>
  20. <div class='clearfix'>
  21. <div class='fl pad1y space-right2'>
  22. <span class="strong">87.5% </span>
  23. <span class="quiet">Statements</span>
  24. <span class='fraction'>35/40</span>
  25. </div>
  26. <div class='fl pad1y space-right2'>
  27. <span class="strong">87.5% </span>
  28. <span class="quiet">Branches</span>
  29. <span class='fraction'>7/8</span>
  30. </div>
  31. <div class='fl pad1y space-right2'>
  32. <span class="strong">100% </span>
  33. <span class="quiet">Functions</span>
  34. <span class='fraction'>5/5</span>
  35. </div>
  36. <div class='fl pad1y space-right2'>
  37. <span class="strong">87.5% </span>
  38. <span class="quiet">Lines</span>
  39. <span class='fraction'>35/40</span>
  40. </div>
  41. </div>
  42. <p class="quiet">
  43. Press <em>n</em> or <em>j</em> to go to the next uncovered block, <em>b</em>, <em>p</em> or <em>k</em> for the previous block.
  44. </p>
  45. </div>
  46. <div class='status-line high'></div>
  47. <pre><table class="coverage">
  48. <tr><td class="line-count quiet"><a name='L1'></a><a href='#L1'>1</a>
  49. <a name='L2'></a><a href='#L2'>2</a>
  50. <a name='L3'></a><a href='#L3'>3</a>
  51. <a name='L4'></a><a href='#L4'>4</a>
  52. <a name='L5'></a><a href='#L5'>5</a>
  53. <a name='L6'></a><a href='#L6'>6</a>
  54. <a name='L7'></a><a href='#L7'>7</a>
  55. <a name='L8'></a><a href='#L8'>8</a>
  56. <a name='L9'></a><a href='#L9'>9</a>
  57. <a name='L10'></a><a href='#L10'>10</a>
  58. <a name='L11'></a><a href='#L11'>11</a>
  59. <a name='L12'></a><a href='#L12'>12</a>
  60. <a name='L13'></a><a href='#L13'>13</a>
  61. <a name='L14'></a><a href='#L14'>14</a>
  62. <a name='L15'></a><a href='#L15'>15</a>
  63. <a name='L16'></a><a href='#L16'>16</a>
  64. <a name='L17'></a><a href='#L17'>17</a>
  65. <a name='L18'></a><a href='#L18'>18</a>
  66. <a name='L19'></a><a href='#L19'>19</a>
  67. <a name='L20'></a><a href='#L20'>20</a>
  68. <a name='L21'></a><a href='#L21'>21</a>
  69. <a name='L22'></a><a href='#L22'>22</a>
  70. <a name='L23'></a><a href='#L23'>23</a>
  71. <a name='L24'></a><a href='#L24'>24</a>
  72. <a name='L25'></a><a href='#L25'>25</a>
  73. <a name='L26'></a><a href='#L26'>26</a>
  74. <a name='L27'></a><a href='#L27'>27</a>
  75. <a name='L28'></a><a href='#L28'>28</a>
  76. <a name='L29'></a><a href='#L29'>29</a>
  77. <a name='L30'></a><a href='#L30'>30</a>
  78. <a name='L31'></a><a href='#L31'>31</a>
  79. <a name='L32'></a><a href='#L32'>32</a>
  80. <a name='L33'></a><a href='#L33'>33</a>
  81. <a name='L34'></a><a href='#L34'>34</a>
  82. <a name='L35'></a><a href='#L35'>35</a>
  83. <a name='L36'></a><a href='#L36'>36</a>
  84. <a name='L37'></a><a href='#L37'>37</a>
  85. <a name='L38'></a><a href='#L38'>38</a>
  86. <a name='L39'></a><a href='#L39'>39</a>
  87. <a name='L40'></a><a href='#L40'>40</a>
  88. <a name='L41'></a><a href='#L41'>41</a>
  89. <a name='L42'></a><a href='#L42'>42</a>
  90. <a name='L43'></a><a href='#L43'>43</a>
  91. <a name='L44'></a><a href='#L44'>44</a>
  92. <a name='L45'></a><a href='#L45'>45</a>
  93. <a name='L46'></a><a href='#L46'>46</a>
  94. <a name='L47'></a><a href='#L47'>47</a>
  95. <a name='L48'></a><a href='#L48'>48</a>
  96. <a name='L49'></a><a href='#L49'>49</a>
  97. <a name='L50'></a><a href='#L50'>50</a>
  98. <a name='L51'></a><a href='#L51'>51</a>
  99. <a name='L52'></a><a href='#L52'>52</a>
  100. <a name='L53'></a><a href='#L53'>53</a>
  101. <a name='L54'></a><a href='#L54'>54</a>
  102. <a name='L55'></a><a href='#L55'>55</a>
  103. <a name='L56'></a><a href='#L56'>56</a>
  104. <a name='L57'></a><a href='#L57'>57</a>
  105. <a name='L58'></a><a href='#L58'>58</a>
  106. <a name='L59'></a><a href='#L59'>59</a>
  107. <a name='L60'></a><a href='#L60'>60</a>
  108. <a name='L61'></a><a href='#L61'>61</a>
  109. <a name='L62'></a><a href='#L62'>62</a>
  110. <a name='L63'></a><a href='#L63'>63</a>
  111. <a name='L64'></a><a href='#L64'>64</a>
  112. <a name='L65'></a><a href='#L65'>65</a>
  113. <a name='L66'></a><a href='#L66'>66</a>
  114. <a name='L67'></a><a href='#L67'>67</a>
  115. <a name='L68'></a><a href='#L68'>68</a>
  116. <a name='L69'></a><a href='#L69'>69</a>
  117. <a name='L70'></a><a href='#L70'>70</a>
  118. <a name='L71'></a><a href='#L71'>71</a>
  119. <a name='L72'></a><a href='#L72'>72</a>
  120. <a name='L73'></a><a href='#L73'>73</a>
  121. <a name='L74'></a><a href='#L74'>74</a>
  122. <a name='L75'></a><a href='#L75'>75</a>
  123. <a name='L76'></a><a href='#L76'>76</a>
  124. <a name='L77'></a><a href='#L77'>77</a>
  125. <a name='L78'></a><a href='#L78'>78</a>
  126. <a name='L79'></a><a href='#L79'>79</a>
  127. <a name='L80'></a><a href='#L80'>80</a>
  128. <a name='L81'></a><a href='#L81'>81</a>
  129. <a name='L82'></a><a href='#L82'>82</a>
  130. <a name='L83'></a><a href='#L83'>83</a>
  131. <a name='L84'></a><a href='#L84'>84</a>
  132. <a name='L85'></a><a href='#L85'>85</a>
  133. <a name='L86'></a><a href='#L86'>86</a>
  134. <a name='L87'></a><a href='#L87'>87</a>
  135. <a name='L88'></a><a href='#L88'>88</a>
  136. <a name='L89'></a><a href='#L89'>89</a>
  137. <a name='L90'></a><a href='#L90'>90</a>
  138. <a name='L91'></a><a href='#L91'>91</a>
  139. <a name='L92'></a><a href='#L92'>92</a>
  140. <a name='L93'></a><a href='#L93'>93</a>
  141. <a name='L94'></a><a href='#L94'>94</a>
  142. <a name='L95'></a><a href='#L95'>95</a>
  143. <a name='L96'></a><a href='#L96'>96</a>
  144. <a name='L97'></a><a href='#L97'>97</a>
  145. <a name='L98'></a><a href='#L98'>98</a>
  146. <a name='L99'></a><a href='#L99'>99</a>
  147. <a name='L100'></a><a href='#L100'>100</a>
  148. <a name='L101'></a><a href='#L101'>101</a>
  149. <a name='L102'></a><a href='#L102'>102</a>
  150. <a name='L103'></a><a href='#L103'>103</a>
  151. <a name='L104'></a><a href='#L104'>104</a>
  152. <a name='L105'></a><a href='#L105'>105</a>
  153. <a name='L106'></a><a href='#L106'>106</a>
  154. <a name='L107'></a><a href='#L107'>107</a>
  155. <a name='L108'></a><a href='#L108'>108</a>
  156. <a name='L109'></a><a href='#L109'>109</a>
  157. <a name='L110'></a><a href='#L110'>110</a>
  158. <a name='L111'></a><a href='#L111'>111</a>
  159. <a name='L112'></a><a href='#L112'>112</a>
  160. <a name='L113'></a><a href='#L113'>113</a>
  161. <a name='L114'></a><a href='#L114'>114</a>
  162. <a name='L115'></a><a href='#L115'>115</a>
  163. <a name='L116'></a><a href='#L116'>116</a>
  164. <a name='L117'></a><a href='#L117'>117</a>
  165. <a name='L118'></a><a href='#L118'>118</a>
  166. <a name='L119'></a><a href='#L119'>119</a>
  167. <a name='L120'></a><a href='#L120'>120</a>
  168. <a name='L121'></a><a href='#L121'>121</a></td><td class="line-coverage quiet"><span class="cline-any cline-yes">1x</span>
  169. <span class="cline-any cline-neutral">&nbsp;</span>
  170. <span class="cline-any cline-yes">1x</span>
  171. <span class="cline-any cline-yes">1x</span>
  172. <span class="cline-any cline-yes">1x</span>
  173. <span class="cline-any cline-yes">1x</span>
  174. <span class="cline-any cline-yes">1x</span>
  175. <span class="cline-any cline-neutral">&nbsp;</span>
  176. <span class="cline-any cline-neutral">&nbsp;</span>
  177. <span class="cline-any cline-neutral">&nbsp;</span>
  178. <span class="cline-any cline-neutral">&nbsp;</span>
  179. <span class="cline-any cline-neutral">&nbsp;</span>
  180. <span class="cline-any cline-neutral">&nbsp;</span>
  181. <span class="cline-any cline-neutral">&nbsp;</span>
  182. <span class="cline-any cline-no">&nbsp;</span>
  183. <span class="cline-any cline-neutral">&nbsp;</span>
  184. <span class="cline-any cline-neutral">&nbsp;</span>
  185. <span class="cline-any cline-neutral">&nbsp;</span>
  186. <span class="cline-any cline-yes">1x</span>
  187. <span class="cline-any cline-yes">2x</span>
  188. <span class="cline-any cline-yes">2x</span>
  189. <span class="cline-any cline-yes">2x</span>
  190. <span class="cline-any cline-yes">2x</span>
  191. <span class="cline-any cline-yes">1x</span>
  192. <span class="cline-any cline-neutral">&nbsp;</span>
  193. <span class="cline-any cline-neutral">&nbsp;</span>
  194. <span class="cline-any cline-neutral">&nbsp;</span>
  195. <span class="cline-any cline-neutral">&nbsp;</span>
  196. <span class="cline-any cline-neutral">&nbsp;</span>
  197. <span class="cline-any cline-neutral">&nbsp;</span>
  198. <span class="cline-any cline-neutral">&nbsp;</span>
  199. <span class="cline-any cline-yes">1x</span>
  200. <span class="cline-any cline-neutral">&nbsp;</span>
  201. <span class="cline-any cline-neutral">&nbsp;</span>
  202. <span class="cline-any cline-neutral">&nbsp;</span>
  203. <span class="cline-any cline-neutral">&nbsp;</span>
  204. <span class="cline-any cline-neutral">&nbsp;</span>
  205. <span class="cline-any cline-neutral">&nbsp;</span>
  206. <span class="cline-any cline-no">&nbsp;</span>
  207. <span class="cline-any cline-neutral">&nbsp;</span>
  208. <span class="cline-any cline-neutral">&nbsp;</span>
  209. <span class="cline-any cline-neutral">&nbsp;</span>
  210. <span class="cline-any cline-yes">1x</span>
  211. <span class="cline-any cline-yes">1x</span>
  212. <span class="cline-any cline-yes">1x</span>
  213. <span class="cline-any cline-yes">1x</span>
  214. <span class="cline-any cline-yes">1x</span>
  215. <span class="cline-any cline-yes">1x</span>
  216. <span class="cline-any cline-yes">1x</span>
  217. <span class="cline-any cline-neutral">&nbsp;</span>
  218. <span class="cline-any cline-neutral">&nbsp;</span>
  219. <span class="cline-any cline-neutral">&nbsp;</span>
  220. <span class="cline-any cline-neutral">&nbsp;</span>
  221. <span class="cline-any cline-neutral">&nbsp;</span>
  222. <span class="cline-any cline-neutral">&nbsp;</span>
  223. <span class="cline-any cline-neutral">&nbsp;</span>
  224. <span class="cline-any cline-neutral">&nbsp;</span>
  225. <span class="cline-any cline-no">&nbsp;</span>
  226. <span class="cline-any cline-neutral">&nbsp;</span>
  227. <span class="cline-any cline-neutral">&nbsp;</span>
  228. <span class="cline-any cline-neutral">&nbsp;</span>
  229. <span class="cline-any cline-yes">1x</span>
  230. <span class="cline-any cline-yes">2x</span>
  231. <span class="cline-any cline-yes">2x</span>
  232. <span class="cline-any cline-yes">2x</span>
  233. <span class="cline-any cline-yes">2x</span>
  234. <span class="cline-any cline-yes">1x</span>
  235. <span class="cline-any cline-neutral">&nbsp;</span>
  236. <span class="cline-any cline-neutral">&nbsp;</span>
  237. <span class="cline-any cline-neutral">&nbsp;</span>
  238. <span class="cline-any cline-neutral">&nbsp;</span>
  239. <span class="cline-any cline-neutral">&nbsp;</span>
  240. <span class="cline-any cline-neutral">&nbsp;</span>
  241. <span class="cline-any cline-neutral">&nbsp;</span>
  242. <span class="cline-any cline-yes">1x</span>
  243. <span class="cline-any cline-neutral">&nbsp;</span>
  244. <span class="cline-any cline-neutral">&nbsp;</span>
  245. <span class="cline-any cline-neutral">&nbsp;</span>
  246. <span class="cline-any cline-neutral">&nbsp;</span>
  247. <span class="cline-any cline-neutral">&nbsp;</span>
  248. <span class="cline-any cline-neutral">&nbsp;</span>
  249. <span class="cline-any cline-no">&nbsp;</span>
  250. <span class="cline-any cline-neutral">&nbsp;</span>
  251. <span class="cline-any cline-neutral">&nbsp;</span>
  252. <span class="cline-any cline-neutral">&nbsp;</span>
  253. <span class="cline-any cline-yes">1x</span>
  254. <span class="cline-any cline-yes">2x</span>
  255. <span class="cline-any cline-yes">2x</span>
  256. <span class="cline-any cline-yes">2x</span>
  257. <span class="cline-any cline-neutral">&nbsp;</span>
  258. <span class="cline-any cline-neutral">&nbsp;</span>
  259. <span class="cline-any cline-neutral">&nbsp;</span>
  260. <span class="cline-any cline-neutral">&nbsp;</span>
  261. <span class="cline-any cline-yes">2x</span>
  262. <span class="cline-any cline-yes">1x</span>
  263. <span class="cline-any cline-neutral">&nbsp;</span>
  264. <span class="cline-any cline-neutral">&nbsp;</span>
  265. <span class="cline-any cline-neutral">&nbsp;</span>
  266. <span class="cline-any cline-neutral">&nbsp;</span>
  267. <span class="cline-any cline-neutral">&nbsp;</span>
  268. <span class="cline-any cline-neutral">&nbsp;</span>
  269. <span class="cline-any cline-neutral">&nbsp;</span>
  270. <span class="cline-any cline-yes">1x</span>
  271. <span class="cline-any cline-neutral">&nbsp;</span>
  272. <span class="cline-any cline-neutral">&nbsp;</span>
  273. <span class="cline-any cline-neutral">&nbsp;</span>
  274. <span class="cline-any cline-neutral">&nbsp;</span>
  275. <span class="cline-any cline-neutral">&nbsp;</span>
  276. <span class="cline-any cline-neutral">&nbsp;</span>
  277. <span class="cline-any cline-no">&nbsp;</span>
  278. <span class="cline-any cline-neutral">&nbsp;</span>
  279. <span class="cline-any cline-neutral">&nbsp;</span>
  280. <span class="cline-any cline-neutral">&nbsp;</span>
  281. <span class="cline-any cline-yes">1x</span>
  282. <span class="cline-any cline-neutral">&nbsp;</span>
  283. <span class="cline-any cline-neutral">&nbsp;</span>
  284. <span class="cline-any cline-neutral">&nbsp;</span>
  285. <span class="cline-any cline-neutral">&nbsp;</span>
  286. <span class="cline-any cline-neutral">&nbsp;</span>
  287. <span class="cline-any cline-neutral">&nbsp;</span>
  288. <span class="cline-any cline-neutral">&nbsp;</span></td><td class="text"><pre class="prettyprint lang-js">const Contact = require("../model/contact");
  289. &nbsp;
  290. const listContacts = async (req, res, next) =&gt; {
  291. try {
  292. const userId = req.user.id;
  293. const contacts = await Contact.getList(userId, req.query);
  294. return res.json({
  295. status: "success",
  296. code: 200,
  297. data: {
  298. ...contacts,
  299. },
  300. });
  301. } catch (e) {
  302. <span class="cstat-no" title="statement not covered" > next(e);</span>
  303. }
  304. };
  305. &nbsp;
  306. const getContactById = async (req, res, next) =&gt; {
  307. try {
  308. const userId = req.user.id;
  309. const contact = await Contact.getById(req.params.contactId, userId);
  310. if (contact) {
  311. return res.json({
  312. status: "success",
  313. code: 200,
  314. data: {
  315. contact,
  316. },
  317. });
  318. } else {
  319. return res.status(404).json({
  320. status: "error",
  321. code: 404,
  322. data: "Not Found",
  323. });
  324. }
  325. } catch (e) {
  326. <span class="cstat-no" title="statement not covered" > next(e);</span>
  327. }
  328. };
  329. &nbsp;
  330. const addContact = async (req, res, next) =&gt; {
  331. try {
  332. const contact = req.body;
  333. const userId = req.user.id;
  334. <span class="missing-if-branch" title="else path not taken" >E</span>if (contact) {
  335. await Contact.add({ ...contact, owner: userId });
  336. return res.status(201).json({
  337. status: "success",
  338. code: 201,
  339. data: {
  340. contact,
  341. },
  342. });
  343. }
  344. } catch (e) {
  345. <span class="cstat-no" title="statement not covered" > next(e);</span>
  346. }
  347. };
  348. &nbsp;
  349. const removeContact = async (req, res, next) =&gt; {
  350. try {
  351. const userId = req.user.id;
  352. const contact = await Contact.remove(req.params.contactId, userId);
  353. if (contact) {
  354. return res.json({
  355. status: "success",
  356. code: 200,
  357. data: {
  358. contact,
  359. },
  360. });
  361. } else {
  362. return res.status(404).json({
  363. status: "error",
  364. code: 404,
  365. data: "Not Found",
  366. });
  367. }
  368. } catch (e) {
  369. <span class="cstat-no" title="statement not covered" > next(e);</span>
  370. }
  371. };
  372. &nbsp;
  373. const updateContact = async (req, res, next) =&gt; {
  374. try {
  375. const userId = req.user.id;
  376. const contact = await Contact.update(
  377. req.params.contactId,
  378. req.body,
  379. userId
  380. );
  381. if (contact) {
  382. return res.status(200).json({
  383. status: "success",
  384. code: 200,
  385. data: {
  386. contact,
  387. },
  388. });
  389. } else {
  390. return res.status(404).json({
  391. status: "error",
  392. code: 404,
  393. data: "Not Found",
  394. });
  395. }
  396. } catch (e) {
  397. <span class="cstat-no" title="statement not covered" > next(e);</span>
  398. }
  399. };
  400. &nbsp;
  401. module.exports = {
  402. listContacts,
  403. getContactById,
  404. addContact,
  405. removeContact,
  406. updateContact,
  407. };
  408. &nbsp;</pre></td></tr></table></pre>
  409. <div class='push'></div><!-- for sticky footer -->
  410. </div><!-- /wrapper -->
  411. <div class='footer quiet pad2 space-top1 center small'>
  412. Code coverage generated by
  413. <a href="https://istanbul.js.org/" target="_blank">istanbul</a>
  414. at Mon Mar 15 2021 13:23:50 GMT+0200 (GMT+02:00)
  415. </div>
  416. </div>
  417. <script src="../../prettify.js"></script>
  418. <script>
  419. window.onload = function () {
  420. prettyPrint();
  421. };
  422. </script>
  423. <script src="../../sorter.js"></script>
  424. <script src="../../block-navigation.js"></script>
  425. </body>
  426. </html>