Skip to content

Commit

Permalink
chore(resource): Updated with latest standards (#6)
Browse files Browse the repository at this point in the history
Generated services and DTOs now follow our current standards.
  • Loading branch information
clintonb authored May 9, 2024
1 parent 352ab37 commit c1adfba
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 64 deletions.
11 changes: 5 additions & 6 deletions src/lib/resource/files/ts/__name__.e2e.__specFileSuffix__.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,18 @@ 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) %>')
.set('Authorization', 'Bearer FAKE')
.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(
Expand Down
23 changes: 11 additions & 12 deletions src/lib/resource/files/ts/__name__.service.ts
Original file line number Diff line number Diff line change
@@ -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';<% } %>
Expand All @@ -16,49 +15,49 @@ 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',
},
});<% } else { %>
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)
);<% } else { %>
return `This action updates a #${id} <%= lowercased(singular(classify(name))) %>`;<% } %>
}

public async remove(user: BannerMember, id: string): Promise<void> {<% if (crud && type == 'rest') { %>
const <%= singular(camelize(name)) %> = await this.findOne(user, id);
public async remove({bannerID, id}: {bannerID: string; id: string}): Promise<void> {<% 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') { %>
Expand Down
6 changes: 5 additions & 1 deletion src/lib/resource/files/ts/dto/__name@singular__.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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?: Partial<Create<%= singular(classify(name)) %>Dto>) {
Object.assign(this, data);
}
}

export class Update<%= singular(classify(name)) %>Dto extends PartialType(Create<%= singular(classify(name)) %>Dto) {}
93 changes: 48 additions & 45 deletions src/lib/resource/resource.factory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -197,45 +196,45 @@ export class UsersService {
private readonly usersRepository: Repository<User>
) {}
public async create(user: BannerMember, dto: CreateUserDto): Promise<User> {
public async create({bannerID, dto}: {bannerID: string, dto: CreateUserDto}): Promise<User> {
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<User[]> {
public async findAll({bannerID}: {bannerID: string}): Promise<User[]> {
// 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<User> {
public async findOne({bannerID, id}: {bannerID: string; id: string}): Promise<User> {
// 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<User> {
const user = await this.findOne(user, id);
public async update({bannerID, id, dto}: {bannerID: string; id: string; dto: UpdateUserDto}): Promise<User> {
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<void> {
const user = await this.findOne(user, id);
public async remove({bannerID, id}: {bannerID: string; id: string}): Promise<void> {
const user = await this.findOne({bannerID, id});
await this.usersRepository.remove(user);
}
Expand Down Expand Up @@ -292,7 +291,11 @@ export class UserDto extends BaseEntityDto {
}
}
export class CreateUserDto {}
export class CreateUserDto {
public constructor(data?: Partial<CreateUserDto>) {
Object.assign(this, data);
}
}
export class UpdateUserDto extends PartialType(CreateUserDto) {}
`,
Expand Down Expand Up @@ -415,19 +418,18 @@ 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')
.set('Authorization', 'Bearer FAKE')
.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(
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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';
Expand All @@ -931,23 +931,23 @@ export class UsersService {
private readonly usersRepository: Repository<User>
) {}
public async create(user: BannerMember, dto: CreateUserDto): Promise<User> {
public async create({bannerID, dto}: {bannerID: string, dto: CreateUserDto}): Promise<User> {
return 'This action adds a new user';
}
public async findAll(user: BannerMember): Promise<User[]> {
public async findAll({bannerID}: {bannerID: string}): Promise<User[]> {
return \`This action returns all users\`;
}
public async findOne(user: BannerMember, id: string): Promise<User> {
public async findOne({bannerID, id}: {bannerID: string; id: string}): Promise<User> {
return \`This action returns a #\${id} user\`;
}
public async update(user: BannerMember, id: string, dto: UpdateUserDto): Promise<User> {
public async update({bannerID, id, dto}: {bannerID: string; id: string; dto: UpdateUserDto}): Promise<User> {
return \`This action updates a #\${id} user\`;
}
public async remove(user: BannerMember, id: string): Promise<void> {
public async remove({bannerID, id}: {bannerID: string; id: string}): Promise<void> {
return \`This action removes a #\${id} user\`;
}
}
Expand Down Expand Up @@ -995,7 +995,11 @@ export class UserDto extends BaseEntityDto {
}
}
export class CreateUserDto {}
export class CreateUserDto {
public constructor(data?: Partial<CreateUserDto>) {
Object.assign(this, data);
}
}
export class UpdateUserDto extends PartialType(CreateUserDto) {}
`,
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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';
Expand All @@ -1294,23 +1296,23 @@ export class UsersService {
private readonly usersRepository: Repository<User>
) {}
public async create(user: BannerMember, dto: CreateUserDto): Promise<User> {
public async create({bannerID, dto}: {bannerID: string, dto: CreateUserDto}): Promise<User> {
return 'This action adds a new user';
}
public async findAll(user: BannerMember): Promise<User[]> {
public async findAll({bannerID}: {bannerID: string}): Promise<User[]> {
return \`This action returns all users\`;
}
public async findOne(user: BannerMember, id: string): Promise<User> {
public async findOne({bannerID, id}: {bannerID: string; id: string}): Promise<User> {
return \`This action returns a #\${id} user\`;
}
public async update(user: BannerMember, id: string, dto: UpdateUserDto): Promise<User> {
public async update({bannerID, id, dto}: {bannerID: string; id: string; dto: UpdateUserDto}): Promise<User> {
return \`This action updates a #\${id} user\`;
}
public async remove(user: BannerMember, id: string): Promise<void> {
public async remove({bannerID, id}: {bannerID: string; id: string}): Promise<void> {
return \`This action removes a #\${id} user\`;
}
}
Expand Down Expand Up @@ -1356,7 +1358,11 @@ export class UserDto extends BaseEntityDto {
}
}
export class CreateUserDto {}
export class CreateUserDto {
public constructor(data?: Partial<CreateUserDto>) {
Object.assign(this, data);
}
}
export class UpdateUserDto extends PartialType(CreateUserDto) {}
`,
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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';
Expand All @@ -1652,19 +1656,19 @@ export class UsersService {
return 'This action adds a new user';
}
public async findAll(user: BannerMember): Promise<User[]> {
public async findAll({bannerID}: {bannerID: string}): Promise<User[]> {
return \`This action returns all users\`;
}
public async findOne(user: BannerMember, id: string): Promise<User> {
public async findOne({bannerID, id}: {bannerID: string; id: string}): Promise<User> {
return \`This action returns a #\${id} user\`;
}
public async update(user: BannerMember, id: string, updateUserInput: UpdateUserInput): Promise<User> {
public async update(updateUserInput: UpdateUserInput): Promise<User> {
return \`This action updates a #\${id} user\`;
}
public async remove(user: BannerMember, id: string): Promise<void> {
public async remove({bannerID, id}: {bannerID: string; id: string}): Promise<void> {
return \`This action removes a #\${id} user\`;
}
}
Expand Down Expand Up @@ -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';
Expand All @@ -1938,19 +1941,19 @@ export class UsersService {
return 'This action adds a new user';
}
public async findAll(user: BannerMember): Promise<User[]> {
public async findAll({bannerID}: {bannerID: string}): Promise<User[]> {
return \`This action returns all users\`;
}
public async findOne(user: BannerMember, id: string): Promise<User> {
public async findOne({bannerID, id}: {bannerID: string; id: string}): Promise<User> {
return \`This action returns a #\${id} user\`;
}
public async update(user: BannerMember, id: string, updateUserInput: UpdateUserInput): Promise<User> {
public async update(updateUserInput: UpdateUserInput): Promise<User> {
return \`This action updates a #\${id} user\`;
}
public async remove(user: BannerMember, id: string): Promise<void> {
public async remove({bannerID, id}: {bannerID: string; id: string}): Promise<void> {
return \`This action removes a #\${id} user\`;
}
}
Expand Down

0 comments on commit c1adfba

Please sign in to comment.