Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: sql controller template fixes #200

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 11 additions & 14 deletions src/commands/generate/templates/controllers/sql.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ const primaryKey = getPrimaryKey(modelName);

class <%= entityClass %>Controller {

async list(req, resp) {
async list(req, resp, next) {
try {
let items = [];
const {
startPage, limit, offset, order
} = req.pagination;
let query = req.parsedQuery;

const repository = Utils.getEntityManager(modelName, resp);
const repository = Utils.getEntityManager(req, resp);
if (!repository) {
throw new ExtendedError({
code: 400,
Expand Down Expand Up @@ -74,10 +74,9 @@ class <%= entityClass %>Controller {
}
}

async get(req, resp) {
async get(req, resp, next) {
const id = req.params.id;
try {
const primaryKey = getPrimaryKey(modelName);
const repository = Utils.getEntityManager(req, resp);
if (!repository) {
throw new ExtendedError({ code: 400, message: 'error_model_not_found_for_this_url' });
Expand Down Expand Up @@ -110,11 +109,11 @@ class <%= entityClass %>Controller {
}
}

async post(req, resp) {
async post(req, resp, next) {
const data = Utils.injectUserId(req.body, req.user, ['createdBy']); // replace field by userId or any other relevant field
try {
await execHook(modelName, 'beforeApiCreate', { request: req, sequelizeQuery: data });
const repository = Utils.getEntityManager(entity, resp);
const repository = Utils.getEntityManager(req, resp);
if (!repository) {
throw new ExtendedError({ code: 400, message: 'error_model_not_found_for_this_url' });
}
Expand All @@ -141,13 +140,12 @@ class <%= entityClass %>Controller {
* @param {[type]} resp [description]
* @return {[type]} [description]
*/
async put(req, resp) {
async put(req, resp, next) {
const data = Utils.injectUserId(req.body, req.user, ['lastModifiedBy']); // replace field by userId or any other relevant field

const id = req.params.id;

try {
const primaryKey = getPrimaryKey(modelName);
const repository = Utils.getEntityManager(req, resp);
if (!repository) {
throw new ExtendedError({ code: 400, message: 'error_model_not_found_for_this_url' });
Expand Down Expand Up @@ -195,10 +193,9 @@ class <%= entityClass %>Controller {
* @param {[type]} resp [description]
* @return {[type]} [description]
*/
async delete (req, resp) {
async delete (req, resp, next) {
try {
const id = req.params.id;
const primaryKey = getPrimaryKey(modelName);
const repository = Utils.getEntityManager(req, resp);

if (!repository) {
Expand All @@ -213,7 +210,7 @@ class <%= entityClass %>Controller {
body: await repository
.destroy(sequelizeQuery)
};
result.body = result.body.get();
result.body = result.body.get();
await execHook(modelName, 'afterApiDelete', result, { request: req, response: resp });

return resp.status(200).json(result);
Expand All @@ -234,7 +231,7 @@ class <%= entityClass %>Controller {

Promise.resolve()
.then(() => {
repository = Utils.getEntityManager(modelName, resp);
repository = Utils.getEntityManager(req, resp);
if (!repository) {
throw new Error('table_model_not_found_error_O');
}
Expand Down Expand Up @@ -273,7 +270,7 @@ class <%= entityClass %>Controller {

getImportTemplate(req, resp) {

const repository = Utils.getEntityManager(modelName, resp);
const repository = Utils.getEntityManager(req, resp);
if (!repository) {
throw new ExtendedError({ code: 400, message: 'error_model_not_found_for_this_url' });
}
Expand Down Expand Up @@ -318,7 +315,7 @@ class <%= entityClass %>Controller {
}

import(req, resp) {
const repository = Utils.getEntityManager(modelName, resp);
const repository = Utils.getEntityManager(req, resp);
if (!repository) {
return;
}
Expand Down