'use strict'; const { createCoreController } = require('@strapi/strapi').factories; const { ApplicationError, NotFoundError } = require("@strapi/utils").errors module.exports = createCoreController('api::commentaire.commentaire', ({strapi}) => ({ async create(ctx) { const {body} = ctx.request let {data} = body if (!data?.user?.id || !data?.parole) { throw new ApplicationError('Informations manquantes.') } const user = await strapi.db.query('plugin::users-permissions.user').findOne({ where: {id: data.user.id} }) if (!user) { throw new NotFoundError('Utilisateur introuvable.') } if (user.id !== data.user.id || user.username !== data.user.username || user.email !== data.user.email) { throw new ApplicationError('Informations non valides.') } const parole = await strapi.db.query('api::parole.parole').findOne({ where: {id: data.parole} }) if (!parole) { throw new NotFoundError('Texte introuvable.') } const newCommentaire = await strapi.documents('api::commentaire.commentaire').create({ data: { contenu: data.contenu, datePublication: data.datePublication, user: user.id, parole: parole.id } }) await strapi.documents('api::parole.parole').update({ documentId: parole.documentId, data: { commentaires: {connect: [newCommentaire.id]} } }) return newCommentaire; } }))