Files
api.pawol.nu/src/api/artiste/controllers/artiste.js
T

49 lines
1.2 KiB
JavaScript
Raw Normal View History

2022-05-12 03:22:15 +04:00
'use strict';
2022-05-12 03:03:29 +04:00
const { createCoreController } = require('@strapi/strapi').factories;
2026-07-04 20:00:51 +04:00
const slugify = require('slugify');
2022-05-12 03:03:29 +04:00
2022-05-20 00:06:01 +04:00
const getSlug = text => {
2026-07-04 20:00:51 +04:00
return slugify(text, {lower: true, remove: /[*#+~.()'"!:@]/g});
};
2022-05-20 00:06:01 +04:00
module.exports = createCoreController('api::artiste.artiste', ({strapi}) => ({
async create(ctx) {
2026-07-04 20:00:51 +04:00
const {body} = ctx.request;
let {data} = body;
2022-05-20 00:06:01 +04:00
if (!data?.user?.documentId) {
2026-07-04 20:00:51 +04:00
return ctx.badRequest('Informations manquantes.');
}
2026-04-21 21:35:59 +04:00
const user = await strapi.documents('plugin::users-permissions.user').findOne({
2026-04-28 12:30:29 +04:00
documentId: body.data.user.documentId
2026-07-04 20:00:51 +04:00
});
2022-05-20 00:06:01 +04:00
if (!user) {
2026-07-04 20:00:51 +04:00
return ctx.notFound('Utilisateur introuvable.');
2022-05-20 00:06:01 +04:00
}
if (user.id !== data.user.id || user.username !== data.user.username || user.email !== data.user.email) {
2026-07-04 20:00:51 +04:00
return ctx.badRequest('Informations non valides.');
2022-05-20 00:06:01 +04:00
}
const artiste = await strapi.db.query('api::artiste.artiste').findOne({
where: {slug: getSlug(data.alias)}
2026-07-04 20:00:51 +04:00
});
2022-05-20 00:06:01 +04:00
if (artiste) {
2026-07-04 20:00:51 +04:00
return artiste;
2022-05-20 00:06:01 +04:00
} else {
2026-04-21 21:35:59 +04:00
const newArtiste = await strapi.documents('api::artiste.artiste').create({
2022-05-20 00:06:01 +04:00
data: {
alias: data.alias,
user: user.id
2022-05-20 00:06:01 +04:00
}
2026-07-04 20:00:51 +04:00
});
return newArtiste;
2022-05-20 00:06:01 +04:00
}
}
2026-07-04 20:00:51 +04:00
}));