52 lines
1.3 KiB
JavaScript
52 lines
1.3 KiB
JavaScript
'use strict';
|
|
|
|
const {default: createStrapi} = require('strapi');
|
|
const {parseMultipartData, sanitizeEntity} = require('strapi-utils')
|
|
|
|
/**
|
|
* Read the documentation (https://strapi.io/documentation/v3.x/concepts/controllers.html#core-controllers)
|
|
* to customize this controller
|
|
*/
|
|
|
|
module.exports = {
|
|
async create(ctx) {
|
|
let entity
|
|
if (ctx.is('multipart')) {
|
|
let {data} = parseMultipartData(ctx)
|
|
data.published_at = null
|
|
entity = await createStrapi.services.teks.create(data)
|
|
} else {
|
|
let {body} = ctx.request
|
|
body.published_at = null
|
|
entity = await strapi.services.teks.create(body)
|
|
}
|
|
|
|
return sanitizeEntity(entity, {model: strapi.models.teks})
|
|
},
|
|
|
|
async update(ctx) {
|
|
const {id} = ctx.params;
|
|
|
|
let entity
|
|
|
|
const teks = await strapi.query('teks').findOne({
|
|
id: ctx.params.id,
|
|
'user.id': ctx.state.user.id,
|
|
published_at: null
|
|
})
|
|
|
|
if (!teks) {
|
|
return ctx.unauthorized(`Vous ne pouvez pas mettre à jour cet élément.`)
|
|
}
|
|
|
|
if (ctx.is('multipart')) {
|
|
const {data} = parseMultipartData(ctx);
|
|
entity = await strapi.services.teks.update({id}, data)
|
|
} else {
|
|
entity = await strapi.services.teks.update({id}, ctx.request.body)
|
|
}
|
|
|
|
return sanitizeEntity(entity, {model: strapi.models.teks})
|
|
}
|
|
}
|