From dce2eeb15ba4149f077dbf92222f58a483beb8a6 Mon Sep 17 00:00:00 2001 From: Mattias van den Belt Date: Wed, 3 Feb 2021 11:53:16 +0100 Subject: [PATCH 1/2] Add isDraft to validateEntity in service examples --- developer-docs/v3.x/concepts/services.md | 32 +++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/developer-docs/v3.x/concepts/services.md b/developer-docs/v3.x/concepts/services.md index 9a01420165..e0b82d0c8b 100644 --- a/developer-docs/v3.x/concepts/services.md +++ b/developer-docs/v3.x/concepts/services.md @@ -19,6 +19,17 @@ You can read about `strapi.query` calls [here](./queries.md). In the following example your controller, service and model are named `restaurant`. ::: +#### Utils + +If you're extending the `create` of `update` service, first require the following utility function: + +```js +const { isDraft } = require('strapi-utils').contentTypes; +``` + +- `isDraft`: This function checks if the entry is a draft. + + #### Collection Type :::: tabs @@ -131,6 +142,8 @@ module.exports = { #### `create` ```js +const { isDraft } = require('strapi-utils').contentTypes; + module.exports = { /** * Promise to add record @@ -139,7 +152,13 @@ module.exports = { */ async create(data, { files } = {}) { - const validData = await strapi.entityValidator.validateEntity(strapi.models.restaurant, data); + const isDraft = isDraft(data, strapi.models.restaurant); + const validData = await strapi.entityValidator.validateEntity( + strapi.models.restaurant, + data, + { isDraft } + ); + const entry = await strapi.query('restaurant').create(validData); if (files) { @@ -163,6 +182,8 @@ module.exports = { #### `update` ```js +const { isDraft } = require('strapi-utils').contentTypes; + module.exports = { /** * Promise to edit record @@ -171,10 +192,15 @@ module.exports = { */ async update(params, data, { files } = {}) { + const existingEntry = await db.query('restaurant').findOne(params); + + const isDraft = isDraft(existingEntry, strapi.models.restaurant); const validData = await strapi.entityValidator.validateEntityUpdate( - strapi.models.restaurant, - data + strapi.models.restaurant, + data, + { isDraft } ); + const entry = await strapi.query('restaurant').update(params, validData); if (files) { From 4566f8fe45acfcf3a9b6c75bfe33fa427d5b150e Mon Sep 17 00:00:00 2001 From: Mattias van den Belt Date: Mon, 8 Feb 2021 14:19:44 +0100 Subject: [PATCH 2/2] Fix typo services.md --- developer-docs/latest/concepts/services.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/developer-docs/latest/concepts/services.md b/developer-docs/latest/concepts/services.md index 31bed3e15b..6eabbeb7e0 100644 --- a/developer-docs/latest/concepts/services.md +++ b/developer-docs/latest/concepts/services.md @@ -21,7 +21,7 @@ In the following example your controller, service and model are named `restauran #### Utils -If you're extending the `create` of `update` service, first require the following utility function: +If you're extending the `create` or `update` service, first require the following utility function: ```js const { isDraft } = require('strapi-utils').contentTypes;