@@ -39,72 +39,84 @@ module.exports = {
3939 * Endpoint for liking suggestions.
4040 */
4141 async like ( ctx ) {
42- const { id } = ctx . params ;
43- const { user } = ctx . request . body ;
42+ try {
43+ const { id } = ctx . params ;
44+ const { user } = ctx . request . body ;
4445
45- let entity = await strapi . db . query ( 'api::suggestion.suggestion' ) . findOne ( {
46- where : { id : id } ,
47- } ) ;
46+ let entity = await strapi . db . query ( 'api::suggestion.suggestion' ) . findOne ( {
47+ where : { id : id } ,
48+ } ) ;
4849
49- if ( ! entity ) {
50- ctx . response . status = 404 ;
51- return ;
52- }
50+ if ( ! entity ) {
51+ ctx . response . status = 404 ;
52+ return ;
53+ }
5354
54- const likes = entity . likes || [ ] ;
55- if ( likes ?. includes ( user ) ) {
56- ctx . response . status = 400 ;
57- ctx . response . body = "already liked" ;
58- return ;
59- }
55+ const likes = entity . likes || [ ] ;
56+ if ( likes ?. includes ( user ) ) {
57+ ctx . response . status = 400 ;
58+ ctx . response . body = "already liked" ;
59+ return ;
60+ }
6061
61- const updatedLikes = [ ...likes , user ] ;
62+ const updatedLikes = [ ...likes , user ] ;
6263
63- entity = await strapi . db . query ( 'api::suggestion.suggestion' ) . update ( {
64- where : { id : id } ,
65- data : { likes : updatedLikes } ,
66- } ) ;
64+ entity = await strapi . db . query ( 'api::suggestion.suggestion' ) . update ( {
65+ where : { id : id } ,
66+ data : { likes : updatedLikes } ,
67+ } ) ;
6768
68- const model = strapi . getModel ( 'api::suggestion.suggestion' )
69- const sanitizeOutput = await sanitize . contentAPI . output ( entity , model )
69+ const model = strapi . getModel ( 'api::suggestion.suggestion' )
70+ const sanitizeOutput = await sanitize . contentAPI . output ( entity , model )
7071
71- return sanitizeOutput
72+ return sanitizeOutput
73+ } catch ( error ) {
74+ console . error ( error ) ;
75+ ctx . response . status = 500 ;
76+ ctx . response . body = { error : "Internal Server Error" , details : error . message } ;
77+ }
7278 } ,
7379 /**
7480 * Endpoint for unliking suggestions.
7581 */
7682 async unlike ( ctx ) {
77- const { id } = ctx . params ;
78- const { user } = ctx . request . body ;
83+ try {
84+ const { id } = ctx . params ;
85+ const { user } = ctx . request . body ;
7986
80- let entity = await strapi . db . query ( 'api::suggestion.suggestion' ) . findOne ( {
81- where : { id : id } ,
82- } ) ;
87+ let entity = await strapi . db . query ( 'api::suggestion.suggestion' ) . findOne ( {
88+ where : { id : id } ,
89+ } ) ;
8390
84- if ( ! entity ) {
85- ctx . response . status = 404 ;
86- return ;
87- }
91+ if ( ! entity ) {
92+ ctx . response . status = 404 ;
93+ return ;
94+ }
8895
89- const index = entity . likes ?. findIndex ( ( x ) => x === user ) ;
90- if ( index === undefined || index < 0 ) {
91- ctx . response . status = 400 ;
92- ctx . response . body = "not liked" ;
93- return ;
94- }
96+ const index = entity . likes ?. findIndex ( ( x ) => x === user ) ;
97+ if ( index === undefined || index < 0 ) {
98+ ctx . response . status = 400 ;
99+ ctx . response . body = "not liked" ;
100+ return ;
101+ }
95102
96- const updatedLikes = entity . likes || [ ] ;
97- updatedLikes . splice ( index , 1 ) ;
103+ const updatedLikes = entity . likes || [ ] ;
104+ updatedLikes . splice ( index , 1 ) ;
98105
99- entity = await strapi . db . query ( 'api::suggestion.suggestion' ) . update ( {
100- where : { id : id } ,
101- data : { likes : updatedLikes } ,
102- } ) ;
106+ entity = await strapi . db . query ( 'api::suggestion.suggestion' ) . update ( {
107+ where : { id : id } ,
108+ data : { likes : updatedLikes } ,
109+ } ) ;
103110
104- const model = strapi . getModel ( 'api::suggestion.suggestion' )
105- const sanitizeOutput = await sanitize . contentAPI . output ( entity , model )
111+ const model = strapi . getModel ( 'api::suggestion.suggestion' )
112+ const sanitizeOutput = await sanitize . contentAPI . output ( entity , model )
106113
107- return sanitizeOutput
114+ return sanitizeOutput
115+ } catch ( error ) {
116+ console . error ( error ) ;
117+ ctx . response . status = 500 ;
118+ ctx . response . body = { error : "Internal Server Error" , details : error . message } ;
119+ }
108120 } ,
109121 /**
110122 * Endpoint for creating new comments. This saves the comments in draft state,
0 commit comments