From 3786a803a69fced4f7a4c3859a932635cdc6383f Mon Sep 17 00:00:00 2001 From: j-weissen Date: Mon, 26 Sep 2022 12:32:27 +0200 Subject: [PATCH] Schema changed, findMany implemented --- .../api/note/content-types/note/schema.json | 8 +++-- .../strapi/src/api/note/controllers/note.js | 36 +++++++++++++++++-- 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/backend/strapi/src/api/note/content-types/note/schema.json b/backend/strapi/src/api/note/content-types/note/schema.json index 82d431e..356eed2 100644 --- a/backend/strapi/src/api/note/content-types/note/schema.json +++ b/backend/strapi/src/api/note/content-types/note/schema.json @@ -4,7 +4,8 @@ "info": { "singularName": "note", "pluralName": "notes", - "displayName": "note" + "displayName": "note", + "description": "" }, "options": { "draftAndPublish": true @@ -20,8 +21,9 @@ }, "owners": { "type": "relation", - "relation": "oneToMany", - "target": "admin::user" + "relation": "manyToMany", + "target": "plugin::users-permissions.user", + "inversedBy": "notes" }, "lastViewed": { "type": "datetime", diff --git a/backend/strapi/src/api/note/controllers/note.js b/backend/strapi/src/api/note/controllers/note.js index 0828a87..61197dc 100644 --- a/backend/strapi/src/api/note/controllers/note.js +++ b/backend/strapi/src/api/note/controllers/note.js @@ -4,6 +4,38 @@ * note controller */ -const { createCoreController } = require('@strapi/strapi').factories; +const {createCoreController} = require('@strapi/strapi').factories; -module.exports = createCoreController('api::note.note'); +module.exports = createCoreController('api::note.note', ({strapi}) => ({ + /** + * Gives all, to the user related, notes. + * @param ctx + * @returns {Promise} + */ + async find(ctx) { + const userId = ctx.state.user.id; + + const entries = await strapi.entityService.findMany('api::note.note', { + populate: ['owners'], + filters: { + owners: { + id: userId + } + }, + sort: 'lastViewed' + }); + return JSON.stringify(entries); + }, + /** + * Finds the note by id. Only returns the note when the user is assigned. + * @param ctx + * @returns {Promise} + */ + async findOne(ctx) { + console.log("findOne") + } +})); + +/** + * + */