Files
api.pawol.nu/api/komante/controllers/komante.js
T

71 lines
1.8 KiB
JavaScript
Raw Normal View History

2021-06-22 21:45:40 +02:00
'use strict';
const {default: createStrapi} = require('strapi');
const {parseMultipartData, sanitizeEntity} = require('strapi-utils')
const findUser = async userId => {
const user = await strapi.query('user', 'users-permissions').findOne({id: userId})
return user
}
module.exports = {
async create(ctx) {
let entity
if (ctx.is('multipart')) {
let {data} = parseMultipartData(ctx)
data.sentAt = new Date()
data.published_at = null
entity = await createStrapi.services.komante.create(data)
} else {
let {body} = ctx.request
body.sentAt = new Date()
body.published_at = null
entity = await strapi.services.komante.create(body)
}
return sanitizeEntity(entity, {model: strapi.models.komante})
},
async find(ctx) {
let entities
if (ctx.query._q) {
entities = await strapi.services.komante.search(ctx.query, [])
for (const entity of entities) {
const user = await findUser(entity.user)
if (!user) {
throw strapi.errors.badRequest('Not found')
}
entity.username = user.username
}
} else {
entities = await strapi.services.komante.find(ctx.query, [])
for (const entity of entities) {
const user = await findUser(entity.user)
if (!user) {
throw strapi.errors.badRequest('Not found')
}
entity.username = user.username
}
}
return entities.map(entity => sanitizeEntity(entity, {model: strapi.models.komante}));
},
async findOne(ctx) {
const {id} = ctx.params
const entity = await strapi.services.komante.findOne({id}, [])
const user = await findUser(entity.user)
if (!user) {
throw strapi.errors.badRequest('Not found')
}
entity.username = user.username
return sanitizeEntity(entity, {model: strapi.models.komante})
},
}