I am creating my own controller to create an entry. It will make use of database transaction, to create the entry only when the existing count is smaller than a number. I want the field validation feature (checking if the required fields exist, data-type is correct, etc). I have put validateInput() but it does not work. Even I have ignored some required fields, it threw no error.
Below is my source code:
'use strict';
const quota = require("../../../utils/quota");
const {createCoreController} = require('@strapi/strapi').factories;
module.exports = createCoreController(
'api::campaign-202408-public.campaign-202408-public',
({strapi}) => ({
async create(ctx) {
try {
await this.validateInput(ctx.request.body.data, ctx);
const trx = await strapi.db.transaction();
try {
const count = await strapi.query('api::campaign-202408-public.campaign-202408-public').count({
populate: {
Form: {
filters: {
PopUpDate: ctx.request.body.data.PopUpDate,
PopUpTime: ctx.state.popup_time,
}
}
},
});
console.log('COUNT', count);
const sanitized_data = await this.sanitizeInput(ctx.request.body.data, ctx);
const result = await strapi.query('api::campaign-202408-public.campaign-202408-public').create(
{
data: sanitized_data,
transaction: trx,
}
);
await trx.commit();
const sanitized_result = await this.sanitizeOutput(result, ctx);
return this.transformResponse(sanitized_result);
} catch (err) {
await trx.rollback();
throw err;
}
} catch (err) {
return ctx.badRequest(err.message);
}
},
}),
);
Thanks.
This topic has been created from a Discord post (1269936062894182450) to give it more visibility.
It will be on Read-Only mode here.
Join the conversation on Discord