| 
									
										
										
										
											2017-03-20 22:08:49 +01:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-16 17:05:02 +02:00
										 |  |  | const _ = require('lodash'); | 
					
						
							| 
									
										
										
										
											2020-02-19 10:54:44 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-01 17:19:49 +02:00
										 |  |  | const parseMultipartBody = require('../utils/parse-multipart'); | 
					
						
							| 
									
										
										
										
											2020-02-19 10:54:44 +01:00
										 |  |  | const { | 
					
						
							|  |  |  |   validateGenerateUIDInput, | 
					
						
							|  |  |  |   validateCheckUIDAvailabilityInput, | 
					
						
							|  |  |  |   validateUIDField, | 
					
						
							|  |  |  | } = require('./validation'); | 
					
						
							| 
									
										
										
										
											2019-08-01 08:52:35 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-20 22:08:49 +01:00
										 |  |  | module.exports = { | 
					
						
							| 
									
										
										
										
											2020-02-19 10:54:44 +01:00
										 |  |  |   async generateUID(ctx) { | 
					
						
							|  |  |  |     const { contentTypeUID, field, data } = await validateGenerateUIDInput(ctx.request.body); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     await validateUIDField(contentTypeUID, field); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const uidService = strapi.plugins['content-manager'].services.uid; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ctx.body = { | 
					
						
							|  |  |  |       data: await uidService.generateUIDField({ contentTypeUID, field, data }), | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   async checkUIDAvailability(ctx) { | 
					
						
							|  |  |  |     const { contentTypeUID, field, value } = await validateCheckUIDAvailabilityInput( | 
					
						
							|  |  |  |       ctx.request.body | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     await validateUIDField(contentTypeUID, field); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const uidService = strapi.plugins['content-manager'].services.uid; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const isAvailable = await uidService.checkUIDAvailability({ contentTypeUID, field, value }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ctx.body = { | 
					
						
							|  |  |  |       isAvailable, | 
					
						
							|  |  |  |       suggestion: !isAvailable | 
					
						
							|  |  |  |         ? await uidService.findUniqueUID({ contentTypeUID, field, value }) | 
					
						
							|  |  |  |         : null, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-19 09:58:38 +02:00
										 |  |  |   /** | 
					
						
							|  |  |  |    * Returns a list of entities of a content-type matching the query parameters | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   async find(ctx) { | 
					
						
							| 
									
										
										
										
											2020-03-06 19:16:23 +01:00
										 |  |  |     const { model } = ctx.params; | 
					
						
							| 
									
										
										
										
											2020-02-19 10:54:44 +01:00
										 |  |  |     const contentManagerService = strapi.plugins['content-manager'].services.contentmanager; | 
					
						
							| 
									
										
										
										
											2020-01-08 17:00:29 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-19 09:58:38 +02:00
										 |  |  |     let entities = []; | 
					
						
							| 
									
										
										
										
											2019-07-26 10:57:27 +02:00
										 |  |  |     if (_.has(ctx.request.query, '_q')) { | 
					
						
							| 
									
										
										
										
											2020-03-06 19:16:23 +01:00
										 |  |  |       entities = await contentManagerService.search({ model }, ctx.request.query); | 
					
						
							| 
									
										
										
										
											2019-07-19 09:58:38 +02:00
										 |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2020-03-06 19:16:23 +01:00
										 |  |  |       entities = await contentManagerService.fetchAll({ model }, ctx.request.query); | 
					
						
							| 
									
										
										
										
											2019-07-19 09:58:38 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-01-28 17:14:53 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-19 09:58:38 +02:00
										 |  |  |     ctx.body = entities; | 
					
						
							| 
									
										
										
										
											2017-03-20 22:08:49 +01:00
										 |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-19 09:58:38 +02:00
										 |  |  |   /** | 
					
						
							|  |  |  |    * Returns an entity of a content type by id | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   async findOne(ctx) { | 
					
						
							| 
									
										
										
										
											2020-03-06 19:16:23 +01:00
										 |  |  |     const { model, id } = ctx.params; | 
					
						
							| 
									
										
										
										
											2020-02-19 10:54:44 +01:00
										 |  |  |     const contentManagerService = strapi.plugins['content-manager'].services.contentmanager; | 
					
						
							| 
									
										
										
										
											2020-01-08 17:00:29 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-06 19:16:23 +01:00
										 |  |  |     const entry = await contentManagerService.fetch({ model, id }); | 
					
						
							| 
									
										
										
										
											2018-06-07 14:35:09 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-19 09:58:38 +02:00
										 |  |  |     // Entry not found
 | 
					
						
							|  |  |  |     if (!entry) { | 
					
						
							|  |  |  |       return ctx.notFound('Entry not found'); | 
					
						
							| 
									
										
										
										
											2018-06-07 14:35:09 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-19 09:58:38 +02:00
										 |  |  |     ctx.body = entry; | 
					
						
							| 
									
										
										
										
											2017-03-20 22:08:49 +01:00
										 |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-19 09:58:38 +02:00
										 |  |  |   /** | 
					
						
							|  |  |  |    * Returns a count of entities of a content type matching query parameters | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   async count(ctx) { | 
					
						
							| 
									
										
										
										
											2020-03-06 19:16:23 +01:00
										 |  |  |     const { model } = ctx.params; | 
					
						
							| 
									
										
										
										
											2020-02-19 10:54:44 +01:00
										 |  |  |     const contentManagerService = strapi.plugins['content-manager'].services.contentmanager; | 
					
						
							| 
									
										
										
										
											2020-01-08 17:00:29 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-19 09:58:38 +02:00
										 |  |  |     let count; | 
					
						
							| 
									
										
										
										
											2019-07-26 10:57:27 +02:00
										 |  |  |     if (_.has(ctx.request.query, '_q')) { | 
					
						
							| 
									
										
										
										
											2020-03-06 19:16:23 +01:00
										 |  |  |       count = await contentManagerService.countSearch({ model }, ctx.request.query); | 
					
						
							| 
									
										
										
										
											2019-07-19 09:58:38 +02:00
										 |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2020-03-06 19:16:23 +01:00
										 |  |  |       count = await contentManagerService.count({ model }, ctx.request.query); | 
					
						
							| 
									
										
										
										
											2019-07-19 09:58:38 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-04-11 11:34:59 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     ctx.body = { | 
					
						
							| 
									
										
										
										
											2019-07-19 09:58:38 +02:00
										 |  |  |       count: _.isNumber(count) ? count : _.toNumber(count), | 
					
						
							| 
									
										
										
										
											2017-04-11 11:34:59 +02:00
										 |  |  |     }; | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-19 09:58:38 +02:00
										 |  |  |   /** | 
					
						
							|  |  |  |    * Creates an entity of a content type | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   async create(ctx) { | 
					
						
							| 
									
										
										
										
											2020-02-19 10:54:44 +01:00
										 |  |  |     const contentManagerService = strapi.plugins['content-manager'].services.contentmanager; | 
					
						
							| 
									
										
										
										
											2020-01-08 17:00:29 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-01 08:52:35 +02:00
										 |  |  |     const { model } = ctx.params; | 
					
						
							| 
									
										
										
										
											2017-11-27 17:27:16 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-19 11:14:00 +01:00
										 |  |  |     try { | 
					
						
							|  |  |  |       if (ctx.is('multipart')) { | 
					
						
							|  |  |  |         const { data, files } = parseMultipartBody(ctx); | 
					
						
							| 
									
										
										
										
											2020-03-06 19:16:23 +01:00
										 |  |  |         ctx.body = await contentManagerService.create(data, { files, model }); | 
					
						
							| 
									
										
										
										
											2020-02-19 11:14:00 +01:00
										 |  |  |       } else { | 
					
						
							|  |  |  |         // Create an entry using `queries` system
 | 
					
						
							| 
									
										
										
										
											2020-03-06 19:16:23 +01:00
										 |  |  |         ctx.body = await contentManagerService.create(ctx.request.body, { model }); | 
					
						
							| 
									
										
										
										
											2020-02-19 11:14:00 +01:00
										 |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-25 20:02:29 +01:00
										 |  |  |       await strapi.telemetry.send('didCreateFirstContentTypeEntry', { model }); | 
					
						
							| 
									
										
										
										
											2020-02-19 11:14:00 +01:00
										 |  |  |     } catch (error) { | 
					
						
							|  |  |  |       strapi.log.error(error); | 
					
						
							|  |  |  |       ctx.badRequest(null, [ | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           messages: [{ id: error.message, message: error.message, field: error.field }], | 
					
						
							| 
									
										
										
										
											2020-03-03 16:12:19 +01:00
										 |  |  |           errors: _.get(error, 'data.errors'), | 
					
						
							| 
									
										
										
										
											2020-02-19 11:14:00 +01:00
										 |  |  |         }, | 
					
						
							|  |  |  |       ]); | 
					
						
							| 
									
										
										
										
											2017-06-17 17:01:50 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-07-19 09:58:38 +02:00
										 |  |  |   }, | 
					
						
							| 
									
										
										
										
											2017-03-20 22:08:49 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-19 09:58:38 +02:00
										 |  |  |   /** | 
					
						
							|  |  |  |    * Updates an entity of a content type | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   async update(ctx) { | 
					
						
							| 
									
										
										
										
											2020-01-08 11:12:41 +01:00
										 |  |  |     const { id, model } = ctx.params; | 
					
						
							| 
									
										
										
										
											2019-07-19 09:58:38 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-19 10:54:44 +01:00
										 |  |  |     const contentManagerService = strapi.plugins['content-manager'].services.contentmanager; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-19 11:14:00 +01:00
										 |  |  |     try { | 
					
						
							|  |  |  |       if (ctx.is('multipart')) { | 
					
						
							|  |  |  |         const { data, files } = parseMultipartBody(ctx); | 
					
						
							|  |  |  |         ctx.body = await contentManagerService.edit({ id }, data, { | 
					
						
							|  |  |  |           files, | 
					
						
							|  |  |  |           model, | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |       } else { | 
					
						
							|  |  |  |         // Return the last one which is the current model.
 | 
					
						
							|  |  |  |         ctx.body = await contentManagerService.edit({ id }, ctx.request.body, { | 
					
						
							|  |  |  |           model, | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } catch (error) { | 
					
						
							|  |  |  |       strapi.log.error(error); | 
					
						
							|  |  |  |       ctx.badRequest(null, [ | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           messages: [{ id: error.message, message: error.message, field: error.field }], | 
					
						
							| 
									
										
										
										
											2020-03-03 16:12:19 +01:00
										 |  |  |           errors: _.get(error, 'data.errors'), | 
					
						
							| 
									
										
										
										
											2020-02-19 11:14:00 +01:00
										 |  |  |         }, | 
					
						
							|  |  |  |       ]); | 
					
						
							| 
									
										
										
										
											2019-07-19 09:58:38 +02:00
										 |  |  |     } | 
					
						
							|  |  |  |   }, | 
					
						
							| 
									
										
										
										
											2017-05-05 11:40:52 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-19 17:24:27 +02:00
										 |  |  |   /** | 
					
						
							|  |  |  |    * Deletes one entity of a content type matching a query | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   async delete(ctx) { | 
					
						
							| 
									
										
										
										
											2020-03-06 19:16:23 +01:00
										 |  |  |     const { id, model } = ctx.params; | 
					
						
							| 
									
										
										
										
											2020-02-19 10:54:44 +01:00
										 |  |  |     const contentManagerService = strapi.plugins['content-manager'].services.contentmanager; | 
					
						
							| 
									
										
										
										
											2020-01-08 17:00:29 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-06 19:16:23 +01:00
										 |  |  |     ctx.body = await contentManagerService.delete({ id, model }); | 
					
						
							| 
									
										
										
										
											2018-07-05 17:57:30 +02:00
										 |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-19 17:24:27 +02:00
										 |  |  |   /** | 
					
						
							|  |  |  |    * Deletes multiple entities of a content type matching a query | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   async deleteMany(ctx) { | 
					
						
							| 
									
										
										
										
											2020-03-06 19:16:23 +01:00
										 |  |  |     const { model } = ctx.params; | 
					
						
							| 
									
										
										
										
											2020-02-19 10:54:44 +01:00
										 |  |  |     const contentManagerService = strapi.plugins['content-manager'].services.contentmanager; | 
					
						
							| 
									
										
										
										
											2020-01-08 17:00:29 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-06 19:16:23 +01:00
										 |  |  |     ctx.body = await contentManagerService.deleteMany({ model }, ctx.request.query); | 
					
						
							| 
									
										
										
										
											2019-07-19 09:58:38 +02:00
										 |  |  |   }, | 
					
						
							| 
									
										
										
										
											2017-03-20 22:08:49 +01:00
										 |  |  | }; |