diff --git a/src/lib/resource/files/ts/__name__.e2e.__specFileSuffix__.ts b/src/lib/resource/files/ts/__name__.e2e.__specFileSuffix__.ts index 85a314e7e..a60e57398 100644 --- a/src/lib/resource/files/ts/__name__.e2e.__specFileSuffix__.ts +++ b/src/lib/resource/files/ts/__name__.e2e.__specFileSuffix__.ts @@ -78,7 +78,7 @@ describe('/v1/<%= dasherize(name) %>', () => { it('creates a new <%= singular(classify(name)) %>', async () => { // TODO Add fields - const body: Create<%= singular(classify(name)) %>Dto = {}; + const body: new Create<%= singular(classify(name)) %>Dto({}); const response = await request(app.getHttpServer()) .post('/v1/<%= dasherize(name) %>') @@ -86,11 +86,10 @@ describe('/v1/<%= dasherize(name) %>', () => { .send(instanceToPlain(body)) .expect(201); - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - const <%= singular(camelize(name)) %> = await <%= lowercased(name) %>Service.findOne( - user!, - response.body.id - ); + const <%= singular(camelize(name)) %> = await <%= lowercased(name) %>Service.findOne({ + bannerID: banner.id, + id: response.body.id, + }); // TODO Assert values stored in database expect(response.body).toEqual( diff --git a/src/lib/resource/files/ts/__name__.service.ts b/src/lib/resource/files/ts/__name__.service.ts index 01c8c10fc..a80a020a8 100644 --- a/src/lib/resource/files/ts/__name__.service.ts +++ b/src/lib/resource/files/ts/__name__.service.ts @@ -1,7 +1,6 @@ import { Injectable, Logger } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; -import { Repository } from 'typeorm'; -import { BannerMember } from '@vori/types/User';<% if (crud && type !== 'graphql-code-first' && type !== 'graphql-schema-first') { %> +import { Repository } from 'typeorm';<% if (crud && type !== 'graphql-code-first' && type !== 'graphql-schema-first') { %> import { Create<%= singular(classify(name)) %>Dto, Update<%= singular(classify(name)) %>Dto } from './dto/<%= singular(name) %>.dto';<% } else if (crud) { %> import { Create<%= singular(classify(name)) %>Input } from './dto/create-<%= singular(name) %>.input'; import { Update<%= singular(classify(name)) %>Input } from './dto/update-<%= singular(name) %>.input';<% } %> @@ -16,22 +15,22 @@ export class <%= classify(name) %>Service { private readonly <%= camelize(name) %>Repository: Repository<<%= classify(singular(name)) %>> ) {} <% if (crud) { %> - public async create(<% if (type !== 'graphql-code-first' && type !== 'graphql-schema-first') { %>user: BannerMember, dto: Create<%= singular(classify(name)) %>Dto<% } else { %>create<%= singular(classify(name)) %>Input: Create<%= singular(classify(name)) %>Input<% } %>): Promise<<%= classify(singular(name)) %>> {<% if (crud && type == 'rest') { %> + public async create(<% if (type !== 'graphql-code-first' && type !== 'graphql-schema-first') { %>{bannerID, dto}: {bannerID: string, dto: Create<%= singular(classify(name)) %>Dto}<% } else { %>create<%= singular(classify(name)) %>Input: Create<%= singular(classify(name)) %>Input<% } %>): Promise<<%= classify(singular(name)) %>> {<% if (crud && type == 'rest') { %> const <%= singular(camelize(name)) %> = this.dtoToModel(dto); // TODO Adjust if the data is not associated with a banner // NOTE: Set this only on creation. Banner IDs are not allowed to change. - <%= singular(camelize(name)) %>.bannerID = user.bannerID; + <%= singular(camelize(name)) %>.bannerID = bannerID; return this.<%= camelize(name) %>Repository.save(<%= singular(camelize(name)) %>);<% } else { %> return 'This action adds a new <%= lowercased(singular(classify(name))) %>';<% } %> } - public async findAll(user: BannerMember): Promise<<%= classify(singular(name)) %>[]> {<% if (crud && type == 'rest') { %> + public async findAll({bannerID}: {bannerID: string}): Promise<<%= classify(singular(name)) %>[]> {<% if (crud && type == 'rest') { %> // TODO Adjust the query if the data is not associated with a banner return this.<%= camelize(name) %>Repository.find({ comment: `controller='<%= classify(name) %>Service',action='findAll'`, - where: { bannerID: user.bannerID }, + where: { bannerID }, order: { createdAt: 'DESC', }, @@ -39,17 +38,17 @@ export class <%= classify(name) %>Service { return `This action returns all <%= lowercased(classify(name)) %>`;<% } %> } - public async findOne(user: BannerMember, id: string): Promise<<%= classify(singular(name)) %>> {<% if (crud && type == 'rest') { %> + public async findOne({bannerID, id}: {bannerID: string; id: string}): Promise<<%= classify(singular(name)) %>> {<% if (crud && type == 'rest') { %> // TODO Adjust the query if the data is not associated with a banner return this.<%= camelize(name) %>Repository.findOneOrFail({ comment: `controller='<%= classify(name) %>Service',action='findOne'`, - where: { id: id.toString(), bannerID: user.bannerID }, + where: { id: id.toString(), bannerID }, });<% } else { %> return `This action returns a #${id} <%= lowercased(singular(classify(name))) %>`;<% } %> } - public async update(user: BannerMember, id: string, <% if (type !== 'graphql-code-first' && type !== 'graphql-schema-first') { %>dto: Update<%= singular(classify(name)) %>Dto<% } else { %>update<%= singular(classify(name)) %>Input: Update<%= singular(classify(name)) %>Input<% } %>): Promise<<%= classify(singular(name)) %>> {<% if (crud && type == 'rest') { %> - const <%= singular(camelize(name)) %> = await this.findOne(user, id); + public async update(<% if (type !== 'graphql-code-first' && type !== 'graphql-schema-first') { %>{bannerID, id, dto}: {bannerID: string; id: string; dto: Update<%= singular(classify(name)) %>Dto}<% } else { %>update<%= singular(classify(name)) %>Input: Update<%= singular(classify(name)) %>Input<% } %>): Promise<<%= classify(singular(name)) %>> {<% if (crud && type == 'rest') { %> + const <%= singular(camelize(name)) %> = await this.findOne({bannerID, id}); const updates = this.dtoToModel(dto); return this.<%= camelize(name) %>Repository.save( this.<%= camelize(name) %>Repository.merge(<%= singular(camelize(name)) %>, updates) @@ -57,8 +56,8 @@ export class <%= classify(name) %>Service { return `This action updates a #${id} <%= lowercased(singular(classify(name))) %>`;<% } %> } - public async remove(user: BannerMember, id: string): Promise {<% if (crud && type == 'rest') { %> - const <%= singular(camelize(name)) %> = await this.findOne(user, id); + public async remove({bannerID, id}: {bannerID: string; id: string}): Promise {<% if (crud && type == 'rest') { %> + const <%= singular(camelize(name)) %> = await this.findOne({bannerID, id}); await this.<%= camelize(name) %>Repository.remove(<%= singular(camelize(name)) %>);<% } else { %> return `This action removes a #${id} <%= lowercased(singular(classify(name))) %>`;<% } %> }<% if (crud && type == 'rest') { %> diff --git a/src/lib/resource/files/ts/dto/__name@singular__.dto.ts b/src/lib/resource/files/ts/dto/__name@singular__.dto.ts index ec7643a42..a94e362c2 100644 --- a/src/lib/resource/files/ts/dto/__name@singular__.dto.ts +++ b/src/lib/resource/files/ts/dto/__name@singular__.dto.ts @@ -8,6 +8,10 @@ export class <%= singular(classify(name)) %>Dto extends BaseEntityDto { } } -export class Create<%= singular(classify(name)) %>Dto {} +export class Create<%= singular(classify(name)) %>Dto { + public constructor(data?: PartialDto>) { + Object.assign(this, data); + } +} export class Update<%= singular(classify(name)) %>Dto extends PartialType(Create<%= singular(classify(name)) %>Dto) {} diff --git a/src/lib/resource/resource.factory.test.ts b/src/lib/resource/resource.factory.test.ts index fbb09e58d..3573464cd 100644 --- a/src/lib/resource/resource.factory.test.ts +++ b/src/lib/resource/resource.factory.test.ts @@ -184,7 +184,6 @@ export class UsersController { .toEqual(`import { Injectable, Logger } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository } from 'typeorm'; -import { BannerMember } from '@vori/types/User'; import { CreateUserDto, UpdateUserDto } from './dto/user.dto'; import { User } from './entities/user.entity'; @@ -197,45 +196,45 @@ export class UsersService { private readonly usersRepository: Repository ) {} - public async create(user: BannerMember, dto: CreateUserDto): Promise { + public async create({bannerID, dto}: {bannerID: string, dto: CreateUserDto}): Promise { const user = this.dtoToModel(dto); // TODO Adjust if the data is not associated with a banner // NOTE: Set this only on creation. Banner IDs are not allowed to change. - user.bannerID = user.bannerID; + user.bannerID = bannerID; return this.usersRepository.save(user); } - public async findAll(user: BannerMember): Promise { + public async findAll({bannerID}: {bannerID: string}): Promise { // TODO Adjust the query if the data is not associated with a banner return this.usersRepository.find({ comment: \`controller='UsersService',action='findAll'\`, - where: { bannerID: user.bannerID }, + where: { bannerID }, order: { createdAt: 'DESC', }, }); } - public async findOne(user: BannerMember, id: string): Promise { + public async findOne({bannerID, id}: {bannerID: string; id: string}): Promise { // TODO Adjust the query if the data is not associated with a banner return this.usersRepository.findOneOrFail({ comment: \`controller='UsersService',action='findOne'\`, - where: { id: id.toString(), bannerID: user.bannerID }, + where: { id: id.toString(), bannerID }, }); } - public async update(user: BannerMember, id: string, dto: UpdateUserDto): Promise { - const user = await this.findOne(user, id); + public async update({bannerID, id, dto}: {bannerID: string; id: string; dto: UpdateUserDto}): Promise { + const user = await this.findOne({bannerID, id}); const updates = this.dtoToModel(dto); return this.usersRepository.save( this.usersRepository.merge(user, updates) ); } - public async remove(user: BannerMember, id: string): Promise { - const user = await this.findOne(user, id); + public async remove({bannerID, id}: {bannerID: string; id: string}): Promise { + const user = await this.findOne({bannerID, id}); await this.usersRepository.remove(user); } @@ -292,7 +291,11 @@ export class UserDto extends BaseEntityDto { } } -export class CreateUserDto {} +export class CreateUserDto { + public constructor(data?: Partial) { + Object.assign(this, data); + } +} export class UpdateUserDto extends PartialType(CreateUserDto) {} `, @@ -415,7 +418,7 @@ describe('/v1/users', () => { it('creates a new User', async () => { // TODO Add fields - const body: CreateUserDto = {}; + const body: new CreateUserDto({}); const response = await request(app.getHttpServer()) .post('/v1/users') @@ -423,11 +426,10 @@ describe('/v1/users', () => { .send(instanceToPlain(body)) .expect(201); - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - const user = await usersService.findOne( - user!, - response.body.id - ); + const user = await usersService.findOne({ + bannerID: banner.id, + id: response.body.id, + }); // TODO Assert values stored in database expect(response.body).toEqual( @@ -751,7 +753,6 @@ export class UsersController { .toEqual(`import { Injectable, Logger } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository } from 'typeorm'; -import { BannerMember } from '@vori/types/User'; import { User } from './entities/user.entity'; @Injectable() @@ -918,7 +919,6 @@ export class UsersController { .toEqual(`import { Injectable, Logger } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository } from 'typeorm'; -import { BannerMember } from '@vori/types/User'; import { CreateUserDto, UpdateUserDto } from './dto/user.dto'; import { User } from './entities/user.entity'; @@ -931,23 +931,23 @@ export class UsersService { private readonly usersRepository: Repository ) {} - public async create(user: BannerMember, dto: CreateUserDto): Promise { + public async create({bannerID, dto}: {bannerID: string, dto: CreateUserDto}): Promise { return 'This action adds a new user'; } - public async findAll(user: BannerMember): Promise { + public async findAll({bannerID}: {bannerID: string}): Promise { return \`This action returns all users\`; } - public async findOne(user: BannerMember, id: string): Promise { + public async findOne({bannerID, id}: {bannerID: string; id: string}): Promise { return \`This action returns a #\${id} user\`; } - public async update(user: BannerMember, id: string, dto: UpdateUserDto): Promise { + public async update({bannerID, id, dto}: {bannerID: string; id: string; dto: UpdateUserDto}): Promise { return \`This action updates a #\${id} user\`; } - public async remove(user: BannerMember, id: string): Promise { + public async remove({bannerID, id}: {bannerID: string; id: string}): Promise { return \`This action removes a #\${id} user\`; } } @@ -995,7 +995,11 @@ export class UserDto extends BaseEntityDto { } } -export class CreateUserDto {} +export class CreateUserDto { + public constructor(data?: Partial) { + Object.assign(this, data); + } +} export class UpdateUserDto extends PartialType(CreateUserDto) {} `, @@ -1115,7 +1119,6 @@ export class UsersController { .toEqual(`import { Injectable, Logger } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository } from 'typeorm'; -import { BannerMember } from '@vori/types/User'; import { User } from './entities/user.entity'; @Injectable() @@ -1281,7 +1284,6 @@ export class UsersGateway { .toEqual(`import { Injectable, Logger } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository } from 'typeorm'; -import { BannerMember } from '@vori/types/User'; import { CreateUserDto, UpdateUserDto } from './dto/user.dto'; import { User } from './entities/user.entity'; @@ -1294,23 +1296,23 @@ export class UsersService { private readonly usersRepository: Repository ) {} - public async create(user: BannerMember, dto: CreateUserDto): Promise { + public async create({bannerID, dto}: {bannerID: string, dto: CreateUserDto}): Promise { return 'This action adds a new user'; } - public async findAll(user: BannerMember): Promise { + public async findAll({bannerID}: {bannerID: string}): Promise { return \`This action returns all users\`; } - public async findOne(user: BannerMember, id: string): Promise { + public async findOne({bannerID, id}: {bannerID: string; id: string}): Promise { return \`This action returns a #\${id} user\`; } - public async update(user: BannerMember, id: string, dto: UpdateUserDto): Promise { + public async update({bannerID, id, dto}: {bannerID: string; id: string; dto: UpdateUserDto}): Promise { return \`This action updates a #\${id} user\`; } - public async remove(user: BannerMember, id: string): Promise { + public async remove({bannerID, id}: {bannerID: string; id: string}): Promise { return \`This action removes a #\${id} user\`; } } @@ -1356,7 +1358,11 @@ export class UserDto extends BaseEntityDto { } } -export class CreateUserDto {} +export class CreateUserDto { + public constructor(data?: Partial) { + Object.assign(this, data); + } +} export class UpdateUserDto extends PartialType(CreateUserDto) {} `, @@ -1468,7 +1474,6 @@ export class UsersGateway { .toEqual(`import { Injectable, Logger } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository } from 'typeorm'; -import { BannerMember } from '@vori/types/User'; import { User } from './entities/user.entity'; @Injectable() @@ -1634,7 +1639,6 @@ export class UsersResolver { .toEqual(`import { Injectable, Logger } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository } from 'typeorm'; -import { BannerMember } from '@vori/types/User'; import { CreateUserInput } from './dto/create-user.input'; import { UpdateUserInput } from './dto/update-user.input'; import { User } from './entities/user.entity'; @@ -1652,19 +1656,19 @@ export class UsersService { return 'This action adds a new user'; } - public async findAll(user: BannerMember): Promise { + public async findAll({bannerID}: {bannerID: string}): Promise { return \`This action returns all users\`; } - public async findOne(user: BannerMember, id: string): Promise { + public async findOne({bannerID, id}: {bannerID: string; id: string}): Promise { return \`This action returns a #\${id} user\`; } - public async update(user: BannerMember, id: string, updateUserInput: UpdateUserInput): Promise { + public async update(updateUserInput: UpdateUserInput): Promise { return \`This action updates a #\${id} user\`; } - public async remove(user: BannerMember, id: string): Promise { + public async remove({bannerID, id}: {bannerID: string; id: string}): Promise { return \`This action removes a #\${id} user\`; } } @@ -1920,7 +1924,6 @@ export class UsersResolver { .toEqual(`import { Injectable, Logger } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository } from 'typeorm'; -import { BannerMember } from '@vori/types/User'; import { CreateUserInput } from './dto/create-user.input'; import { UpdateUserInput } from './dto/update-user.input'; import { User } from './entities/user.entity'; @@ -1938,19 +1941,19 @@ export class UsersService { return 'This action adds a new user'; } - public async findAll(user: BannerMember): Promise { + public async findAll({bannerID}: {bannerID: string}): Promise { return \`This action returns all users\`; } - public async findOne(user: BannerMember, id: string): Promise { + public async findOne({bannerID, id}: {bannerID: string; id: string}): Promise { return \`This action returns a #\${id} user\`; } - public async update(user: BannerMember, id: string, updateUserInput: UpdateUserInput): Promise { + public async update(updateUserInput: UpdateUserInput): Promise { return \`This action updates a #\${id} user\`; } - public async remove(user: BannerMember, id: string): Promise { + public async remove({bannerID, id}: {bannerID: string; id: string}): Promise { return \`This action removes a #\${id} user\`; } }