Files
api.pawol.nu/src/api/artiste/controllers/artiste.js
T
2026-04-21 21:35:59 +04:00

58 lines
1.5 KiB
JavaScript

'use strict';
const { createCoreController } = require('@strapi/strapi').factories;
const slugify = require('slugify')
const getSlug = text => {
return slugify(text, {lower: true, remove: /[*#+~.()'"!:@]/g})
}
module.exports = createCoreController('api::artiste.artiste', ({strapi}) => ({
async create(ctx) {
const {body} = ctx.request
let {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.documents('plugin::users-permissions.user').findOne({
documentId: "__TODO__"
})
if (!user) {
throw new NotFoundError('Utilisateur introuvable.')
}
if (user.id !== data.user.id || user.username !== data.user.username || user.email !== data.user.email) {
ctx.badRequest('Informations non valides.')
}
const artiste = await strapi.db.query('api::artiste.artiste').findOne({
where: {slug: getSlug(data.alias)}
})
if (artiste) {
return artiste
} else {
const newArtiste = await strapi.documents('api::artiste.artiste').create({
data: {
...data
}
})
return newArtiste
}
}
}))