Create custom parole controller
This commit is contained in:
@@ -2,4 +2,111 @@
|
|||||||
|
|
||||||
const { createCoreController } = require('@strapi/strapi').factories;
|
const { createCoreController } = require('@strapi/strapi').factories;
|
||||||
|
|
||||||
module.exports = createCoreController('api::parole.parole')
|
module.exports = createCoreController('api::parole.parole', ({strapi}) => ({
|
||||||
|
async findOne(ctx) {
|
||||||
|
const {id} = ctx.params
|
||||||
|
const parole = await strapi.entityService.findOne('api::parole.parole', id, {
|
||||||
|
populate: ['artistes']
|
||||||
|
})
|
||||||
|
return parole
|
||||||
|
},
|
||||||
|
async update(ctx) {
|
||||||
|
const {body} = ctx.request
|
||||||
|
const {data} = body
|
||||||
|
|
||||||
|
const updatedParole = await strapi.entityService.update('api::parole.parole', data.id, {
|
||||||
|
data: {
|
||||||
|
...data
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
return updatedParole
|
||||||
|
},
|
||||||
|
async create(ctx) {
|
||||||
|
const translateLyrics = async parolesFR => {
|
||||||
|
const anglais = await strapi.service('api::parole.parole').translate('FR', 'EN', parolesFR)
|
||||||
|
const espagnol = await strapi.service('api::parole.parole').translate('FR', 'ES', parolesFR)
|
||||||
|
const allemand = await strapi.service('api::parole.parole').translate('FR', 'DE', parolesFR)
|
||||||
|
const italien = await strapi.service('api::parole.parole').translate('FR', 'IT', parolesFR)
|
||||||
|
|
||||||
|
return {
|
||||||
|
francais: parolesFR,
|
||||||
|
anglais: anglais + '\n\n (Translated by DeepL)',
|
||||||
|
espagnol: espagnol + '\n\n (Traducido por DeepL)',
|
||||||
|
allemand: allemand + '\n\n (Übersetzt von DeepL)',
|
||||||
|
italien: italien + '\n\n (Tradotto da DeepL)'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const {body} = ctx.request
|
||||||
|
const {data} = body
|
||||||
|
|
||||||
|
if (ctx.request && ctx.request.header && ctx.request.header.authorization) {
|
||||||
|
try {
|
||||||
|
const {id} = await strapi.plugins[
|
||||||
|
'users-permissions'
|
||||||
|
].services.jwt.getToken(ctx)
|
||||||
|
|
||||||
|
if (id !== data.user.id) {
|
||||||
|
throw new UnauthorizedError('Opération non autorisée')
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
throw new UnauthorizedError(ctx, err, 'Opération non autorisée')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const user = await strapi.entityService.findOne('plugin::users-permissions.user', body.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 ValidationError('Informations non valides.')
|
||||||
|
}
|
||||||
|
|
||||||
|
const artiste = await strapi.entityService.findOne('api::artiste.artiste', data.artistes[0])
|
||||||
|
|
||||||
|
if (!artiste) {
|
||||||
|
throw new NotFoundError('Artiste introuvable.')
|
||||||
|
}
|
||||||
|
|
||||||
|
const currentUserParole = await strapi.entityService.findMany('api::parole.parole', {
|
||||||
|
fields: ['id'],
|
||||||
|
filters: {
|
||||||
|
user: {
|
||||||
|
id: {
|
||||||
|
$eq: user.id
|
||||||
|
}
|
||||||
|
},
|
||||||
|
publishedAt: {
|
||||||
|
$eq: null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
if (user && user.canAutoTranslate && data.traductionAuto && data.traductions.francais && (!data.traductions.anglais || !data.traductions.espagnol || !data.traductions.allemand || !data.traductions.italien)) {
|
||||||
|
const translated = await translateLyrics(data.traductions.francais)
|
||||||
|
data.traductions = translated
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('data createCoreController', data)
|
||||||
|
const newParole = await strapi.entityService.create('api::parole.parole', {
|
||||||
|
data: {
|
||||||
|
...data
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
const parolesIds = currentUserParole.map(({id}) => id)
|
||||||
|
parolesIds.push(newParole.id)
|
||||||
|
|
||||||
|
await strapi.entityService.update('plugin::users-permissions.user', user.id, {
|
||||||
|
data: {
|
||||||
|
paroles: parolesIds
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
return newParole
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
|||||||
Reference in New Issue
Block a user