Skip to content

Commit

Permalink
fix: model with id required
Browse files Browse the repository at this point in the history
  • Loading branch information
jannyHou committed Sep 16, 2019
1 parent 8f49a45 commit 7af132c
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 16 deletions.
6 changes: 6 additions & 0 deletions packages/cli/generators/controller/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,12 @@ module.exports = class ControllerGenerator extends ArtifactGenerator {
when: this.artifactInfo.idType === undefined,
default: 'number',
},
{
type: 'confirm',
name: 'idOmitted',
message: 'Is the id omitted when creating a new instance?',
default: true,
},
{
type: 'input',
name: 'httpPathName',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ export class <%= className %>Controller {
'application/json': {
schema: getModelSchemaRef(<%= modelName %>, {
title: 'New<%= modelName %>',
exclude: ['<%= id %>'],
<%if (idOmitted) {%>exclude: ['<%= id %>'],<% } %>
}),
},
},
})
<%= modelVariableName %>: Omit<<%= modelName %>, '<%= id %>'>,
<%= modelVariableName %>: <% if (!idOmitted) { -%><%= modelName %><% } else { -%>Omit<<%= modelName %>, '<%= id %>'><% } -%>,
): Promise<<%= modelName %>> {
return this.<%= repositoryNameCamel %>.create(<%= modelVariableName %>);
}
Expand Down
69 changes: 55 additions & 14 deletions packages/cli/test/integration/generators/controller.integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ describe('lb4 controller', () => {
restCLIInput,
);

it('creates REST CRUD template with valid input', async () => {
it('creates REST CRUD template with valid input - id omitted', async () => {
await testUtils
.executeGenerator(generator)
.inDir(SANDBOX_PATH, () =>
Expand All @@ -111,7 +111,23 @@ describe('lb4 controller', () => {
)
.withPrompts(restCLIInputComplete);

checkRestCrudContents();
checkRestCrudContents({idOmitted: true});
});

it('creates REST CRUD template with valid input', async () => {
await testUtils
.executeGenerator(generator)
.inDir(SANDBOX_PATH, () =>
testUtils.givenLBProject(SANDBOX_PATH, {
includeDummyModel: true,
includeDummyRepository: true,
}),
)
.withPrompts(
Object.assign({}, restCLIInputComplete, {idOmitted: false}),
);

checkRestCrudContents({idOmitted: false});
});

describe('HTTP REST path', () => {
Expand Down Expand Up @@ -228,13 +244,44 @@ function checkBasicContents() {
assert.fileContent(expectedFile, /constructor\(\) {}/);
}

function checkCreateContentsWithIdOmitted() {
const postCreateRegEx = [
/\@post\('\/product-reviews', {/,
/responses: {/,
/'200': {/,
/description: 'ProductReview model instance'/,
/content: {'application\/json': {schema: getModelSchemaRef\(ProductReview\)}},\s{1,}},\s{1,}},\s{1,}}\)/,
/async create\(\s+\@requestBody\({\s+content: {\s+'application\/json': {\s+schema: getModelSchemaRef\(ProductReview, {\s+title: 'NewProductReview',\s+exclude: \['productId'\],\s+}\),\s+},\s+},\s+}\)\s+productReview: Omit<ProductReview, 'productId'>,\s+\)/,
];
postCreateRegEx.forEach(regex => {
assert.fileContent(expectedFile, regex);
});
}

/**
* Check the contents for operation 'create' when id is not required.
*/
function checkCreateContents() {
const postCreateRegEx = [
/\@post\('\/product-reviews', {/,
/responses: {/,
/'200': {/,
/description: 'ProductReview model instance'/,
/content: {'application\/json': {schema: getModelSchemaRef\(ProductReview\)}},\s{1,}},\s{1,}},\s{1,}}\)/,
/async create\(\s+\@requestBody\({\s+content: {\s+'application\/json': {\s+schema: getModelSchemaRef\(ProductReview, {\s+title: 'NewProductReview',\s+}\),\s+},\s+},\s+}\)\s+productReview: ProductReview,\s+\)/,
];
postCreateRegEx.forEach(regex => {
assert.fileContent(expectedFile, regex);
});
}

/**
* Assertions against the template to determine if it contains the
* required signatures for a REST CRUD controller, specifically to ensure
* that decorators are grouped correctly (for their corresponding
* target functions)
*/
function checkRestCrudContents() {
function checkRestCrudContents(options) {
assert.fileContent(expectedFile, /class ProductReviewController/);

// Repository and injection
Expand All @@ -243,17 +290,11 @@ function checkRestCrudContents() {

// Assert that the decorators are present in the correct groupings!
// @post - create
const postCreateRegEx = [
/\@post\('\/product-reviews', {/,
/responses: {/,
/'200': {/,
/description: 'ProductReview model instance'/,
/content: {'application\/json': {schema: getModelSchemaRef\(ProductReview\)}},\s{1,}},\s{1,}},\s{1,}}\)/,
/async create\(\s+\@requestBody\({\s+content: {\s+'application\/json': {\s+schema: getModelSchemaRef\(ProductReview, {\s+title: 'NewProductReview',\s+exclude: \['productId'\],\s+}\),\s+},\s+},\s+}\)\s+productReview: Omit<ProductReview, 'productId'>,\s+\)/,
];
postCreateRegEx.forEach(regex => {
assert.fileContent(expectedFile, regex);
});
if (options && options.idOmitted) {
checkCreateContentsWithIdOmitted();
} else {
checkCreateContents();
}

// @get - count
const getCountRegEx = [
Expand Down

0 comments on commit 7af132c

Please sign in to comment.