2022-05-12 03:22:15 +04:00
|
|
|
'use strict';
|
|
|
|
|
|
2022-05-12 03:03:19 +04:00
|
|
|
const { createCoreController } = require('@strapi/strapi').factories;
|
2026-07-04 10:04:00 +04:00
|
|
|
const { ApplicationError, NotFoundError } = require("@strapi/utils").errors
|
2022-05-12 03:03:19 +04:00
|
|
|
|
2022-05-20 00:06:15 +04:00
|
|
|
module.exports = createCoreController('api::commentaire.commentaire', ({strapi}) => ({
|
|
|
|
|
async create(ctx) {
|
|
|
|
|
const {body} = ctx.request
|
|
|
|
|
let {data} = body
|
|
|
|
|
|
2026-07-04 11:38:02 +04:00
|
|
|
if (!data?.user?.id || !data?.parole) {
|
|
|
|
|
throw new ApplicationError('Informations manquantes.')
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-04 09:43:28 +04:00
|
|
|
const user = await strapi.db.query('plugin::users-permissions.user').findOne({
|
|
|
|
|
where: {id: data.user.id}
|
2026-04-21 21:35:59 +04:00
|
|
|
})
|
2022-05-20 00:06:15 +04:00
|
|
|
|
|
|
|
|
if (!user) {
|
|
|
|
|
throw new NotFoundError('Utilisateur introuvable.')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (user.id !== data.user.id || user.username !== data.user.username || user.email !== data.user.email) {
|
2023-04-02 12:25:52 +04:00
|
|
|
throw new ApplicationError('Informations non valides.')
|
2022-05-20 00:06:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
data.user = user.id
|
|
|
|
|
|
2026-07-04 09:43:28 +04:00
|
|
|
const parole = await strapi.db.query('api::parole.parole').findOne({
|
|
|
|
|
where: {id: data.parole}
|
2022-05-20 00:06:15 +04:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if (!parole) {
|
|
|
|
|
throw new NotFoundError('Texte introuvable.')
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-21 21:35:59 +04:00
|
|
|
const newCommentaire = await strapi.documents('api::commentaire.commentaire').create({
|
2022-05-20 00:06:15 +04:00
|
|
|
data: {
|
|
|
|
|
...data
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
2026-04-21 21:35:59 +04:00
|
|
|
await strapi.documents('api::parole.parole').update({
|
2026-07-04 09:43:28 +04:00
|
|
|
documentId: parole.documentId,
|
2026-04-21 21:35:59 +04:00
|
|
|
|
2022-05-20 00:06:15 +04:00
|
|
|
data: {
|
|
|
|
|
commentaires: [newCommentaire.id]
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return newCommentaire;
|
|
|
|
|
}
|
|
|
|
|
}))
|