Skip to content

Commit 416f2a6

Browse files
committed
refactor
1 parent f1edab0 commit 416f2a6

File tree

8 files changed

+23
-84
lines changed

8 files changed

+23
-84
lines changed

config.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,4 @@
11
import { config as configDev } from './config/config.development';
22
import { config as configProd } from './config/config.production';
33

4-
let config;
5-
6-
if (process.env.NODE_ENV === 'production') {
7-
config = configProd;
8-
} else {
9-
config = configDev;
10-
}
11-
12-
export default config;
4+
export default process.env.NODE_ENV === 'production' ? configProd : configDev;

src/database/database.providers.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,7 @@ export const databaseProviders = [
77
{
88
provide: 'SEQUELIZE',
99
useFactory: async (configService: ConfigService) => {
10-
const { database, username, password, dialect, host, port, logging } = configService.sequelizeOrmConfig;
11-
const sequelize = new Sequelize(
12-
database,
13-
username,
14-
password,
15-
{
16-
dialect,
17-
host,
18-
port,
19-
logging
20-
}
21-
);
10+
const sequelize = new Sequelize(configService.sequelizeOrmConfig);
2211
sequelize.addModels([User, Post]);
2312
await sequelize.sync();
2413
return sequelize;

src/posts/posts.controller.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import { PostsService } from './posts.service';
2222
import { AuthGuard } from '@nestjs/passport';
2323
import { Post as PostEntity } from './post.entity';
2424
import { PostDto } from './dto/post.dto';
25-
import { Request } from 'express';
2625
import { UpdatePostDto } from './dto/update-post.dto';
2726

2827
@Controller('posts')

src/posts/posts.service.ts

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,45 +12,36 @@ export class PostsService {
1212
private readonly postsRepository: typeof Post,
1313
) {}
1414

15-
async findAll(): Promise<PostDto[]> {
15+
async findAll() {
1616
const posts = await this.postsRepository.findAll<Post>({
1717
include: [User],
1818
});
19-
return posts.map(post => {
20-
return new PostDto(post);
21-
});
19+
return posts.map(post => new PostDto(post));
2220
}
2321

24-
async findOne(id: number): Promise<PostDto> {
22+
async findOne(id: number) {
2523
const post = await this.postsRepository.findByPk<Post>(id, {
2624
include: [User],
2725
});
2826
if (!post) {
2927
throw new HttpException('No post found', HttpStatus.NOT_FOUND);
3028
}
31-
3229
return new PostDto(post);
3330
}
3431

35-
async create(userId: string, createPostDto: CreatePostDto): Promise<Post> {
32+
async create(userId: string, createPostDto: CreatePostDto) {
3633
const post = new Post();
3734
post.userId = userId;
3835
post.title = createPostDto.title;
3936
post.content = createPostDto.content;
40-
41-
try {
42-
return await post.save();
43-
} catch (err) {
44-
throw new HttpException(err, HttpStatus.INTERNAL_SERVER_ERROR);
45-
}
37+
return post.save();
4638
}
4739

48-
private async getUserPost(id: number, userId: string): Promise<Post> {
40+
private async getUserPost(id: number, userId: string) {
4941
const post = await this.postsRepository.findByPk<Post>(id);
5042
if (!post) {
5143
throw new HttpException('No post found', HttpStatus.NOT_FOUND);
5244
}
53-
5445
if (post.userId !== userId) {
5546
throw new HttpException(
5647
'You are unauthorized to manage this post',
@@ -61,24 +52,14 @@ export class PostsService {
6152
return post;
6253
}
6354

64-
async update(
65-
id: number,
66-
userId: string,
67-
updatePostDto: UpdatePostDto,
68-
): Promise<Post> {
55+
async update(id: number, userId: string, updatePostDto: UpdatePostDto) {
6956
const post = await this.getUserPost(id, userId);
70-
7157
post.title = updatePostDto.title || post.title;
7258
post.content = updatePostDto.content || post.content;
73-
74-
try {
75-
return await post.save();
76-
} catch (err) {
77-
throw new HttpException(err, HttpStatus.INTERNAL_SERVER_ERROR);
78-
}
59+
return post.save();
7960
}
8061

81-
async delete(id: number, userId: string): Promise<Post> {
62+
async delete(id: number, userId: string) {
8263
const post = await this.getUserPost(id, userId);
8364
await post.destroy();
8465
return post;
Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { Injectable } from '@nestjs/common';
2-
import { JwtConfig } from './interfaces/jwt-config.interface';
32
import config from '../../../config';
43

54
@Injectable()
@@ -8,9 +7,7 @@ export class ConfigService {
87
return config.database;
98
}
109

11-
get jwtConfig(): JwtConfig {
12-
return {
13-
privateKey: config.jwtPrivateKey,
14-
};
10+
get jwtConfig() {
11+
return { privateKey: config.jwtPrivateKey };
1512
}
1613
}

src/shared/config/interfaces/jwt-config.interface.ts

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/shared/config/interfaces/sequelize-orm-config.interface.ts

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/users/users.service.ts

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,32 +22,29 @@ export class UsersService {
2222
this.jwtPrivateKey = this.configService.jwtConfig.privateKey;
2323
}
2424

25-
async findAll(): Promise<UserDto[]> {
25+
async findAll() {
2626
const users = await this.usersRepository.findAll<User>();
27-
return users.map(user => {
28-
return new UserDto(user);
29-
});
27+
return users.map(user => new UserDto(user));
3028
}
3129

32-
async getUser(id: string): Promise<UserDto> {
30+
async getUser(id: string) {
3331
const user = await this.usersRepository.findByPk<User>(id);
3432
if (!user) {
3533
throw new HttpException(
3634
'User with given id not found',
3735
HttpStatus.NOT_FOUND,
3836
);
3937
}
40-
4138
return new UserDto(user);
4239
}
4340

44-
async getUserByEmail(email: string): Promise<User> {
41+
async getUserByEmail(email: string) {
4542
return await this.usersRepository.findOne<User>({
4643
where: { email },
4744
});
4845
}
4946

50-
async create(createUserDto: CreateUserDto): Promise<UserLoginResponseDto> {
47+
async create(createUserDto: CreateUserDto) {
5148
try {
5249
const user = new User();
5350
user.email = createUserDto.email.trim().toLowerCase();
@@ -76,9 +73,7 @@ export class UsersService {
7673
}
7774
}
7875

79-
async login(
80-
userLoginRequestDto: UserLoginRequestDto,
81-
): Promise<UserLoginResponseDto> {
76+
async login(userLoginRequestDto: UserLoginRequestDto) {
8277
const email = userLoginRequestDto.email;
8378
const password = userLoginRequestDto.password;
8479

@@ -102,7 +97,7 @@ export class UsersService {
10297
return new UserLoginResponseDto(user, token);
10398
}
10499

105-
async update(id: string, updateUserDto: UpdateUserDto): Promise<UserDto> {
100+
async update(id: string, updateUserDto: UpdateUserDto) {
106101
const user = await this.usersRepository.findByPk<User>(id);
107102
if (!user) {
108103
throw new HttpException('User not found.', HttpStatus.NOT_FOUND);
@@ -121,18 +116,17 @@ export class UsersService {
121116
}
122117
}
123118

124-
async delete(id: string): Promise<UserDto> {
119+
async delete(id: string) {
125120
const user = await this.usersRepository.findByPk<User>(id);
126121
await user.destroy();
127122
return new UserDto(user);
128123
}
129124

130-
async signToken(user: User): Promise<string> {
125+
async signToken(user: User) {
131126
const payload: JwtPayload = {
132127
email: user.email,
133128
};
134129

135-
const token = sign(payload, this.jwtPrivateKey, {});
136-
return token;
130+
return sign(payload, this.jwtPrivateKey, {});
137131
}
138132
}

0 commit comments

Comments
 (0)