Skip to content

Commit

Permalink
Merge pull request #100 from All-eatery/refactor/nest
Browse files Browse the repository at this point in the history
Refactor/nest
  • Loading branch information
go-tiger authored Sep 17, 2024
2 parents 475966a + a999548 commit 459b77d
Show file tree
Hide file tree
Showing 38 changed files with 1,098 additions and 1,122 deletions.
16 changes: 15 additions & 1 deletion .env.sample
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
ENV_NESTJS_PORT=

ENV_KAPI_URL=
ENV_FE_DOMAIN=
ENV_FE_LOGIN_REDIRECT_URL=

ENV_DB_URL=
ENV_DB_USER=
ENV_DB_PW=
Expand All @@ -15,5 +19,15 @@ ENV_GOOGLE_REDIRECT_URI=
ENV_GOOGLE_CLIENT_ID=
ENV_GOOGLE_CLIENT_SECRET=

ENV_NAVER_CLIENT_ID=
ENV_NAVER_REDIRECT_URI=
ENV_NAVER_CLIENT_SECRET=

ENV_EMAIL_NAME=
ENV_EMAIL_PASSWORD=
ENV_EMAIL_PASSWORD=

ENV_AWS_S3_BUCKET=
ENV_AWS_S3_REGION=
ENV_AWS_S3_ACCESS_KEY_ID=
ENV_AWS_S3_SECRET_ACCESS_KEY=
ENV_AWS_S3_URL=
1,907 changes: 925 additions & 982 deletions package-lock.json

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@
"create:place": "ts-node src/main/nest/script/batch/create-place.batch.ts"
},
"dependencies": {
"@aws-sdk/client-s3": "^3.609.0",
"@aws-sdk/client-s3": "^3.651.1",
"@nestjs-modules/mailer": "^2.0.2",
"@nestjs/common": "^10.0.0",
"@nestjs/config": "^3.2.2",
"@nestjs/core": "^10.0.0",
"@nestjs/core": "^10.4.3",
"@nestjs/jwt": "^10.2.0",
"@nestjs/passport": "^10.0.3",
"@nestjs/platform-express": "^10.0.0",
"@nestjs/swagger": "^7.3.1",
"@nestjs/swagger": "^7.4.2",
"@prisma/client": "^5.16.1",
"bcrypt": "^5.1.1",
"class-transformer": "^0.5.1",
Expand All @@ -38,11 +38,11 @@
"rxjs": "^7.8.1"
},
"devDependencies": {
"@nestjs/cli": "^10.0.0",
"@nestjs/cli": "^10.4.5",
"@nestjs/schematics": "^10.0.0",
"@nestjs/testing": "^10.0.0",
"@types/bcrypt": "^5.0.2",
"@types/express": "^4.17.17",
"@types/express": "^4.17.21",
"@types/jest": "^29.5.2",
"@types/multer": "^1.4.11",
"@types/node": "^20.3.1",
Expand Down
22 changes: 0 additions & 22 deletions src/main/nest/app/app.controller.spec.ts

This file was deleted.

7 changes: 5 additions & 2 deletions src/main/nest/app/app.controller.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service';
import { ApiOkResponse, ApiOperation } from '@nestjs/swagger';

@Controller()
export class AppController {
constructor(private readonly appService: AppService) {}

@Get()
getHello(): string {
return this.appService.getHello();
@ApiOperation({ summary: '서버 상태 체크' })
@ApiOkResponse({ description: 'health check' })
health(): string {
return this.appService.health();
}
}
2 changes: 2 additions & 0 deletions src/main/nest/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { UserModule } from '../domain/user/user.module';
import { MailModule } from '../global/mail/mail.module';
import { PostModule } from '../domain/post/post.module';
import { PlaceModule } from '../domain/place/place.module';
import { MenuModule } from '../domain/menu/menu.module';

@Module({
imports: [
Expand All @@ -18,6 +19,7 @@ import { PlaceModule } from '../domain/place/place.module';
UserModule,
PostModule,
PlaceModule,
MenuModule,
],
controllers: [AppController],
providers: [AppService],
Expand Down
4 changes: 2 additions & 2 deletions src/main/nest/app/app.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Injectable } from '@nestjs/common';

@Injectable()
export class AppService {
getHello(): string {
return 'Hello World!';
health(): string {
return 'health-check!';
}
}
8 changes: 4 additions & 4 deletions src/main/nest/domain/auth/auth.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { AuthController } from './controller/auth.controller';
import { AuthService } from './service/auth.service';
import { PassportModule } from '@nestjs/passport';
import { JwtModule } from '@nestjs/jwt';
import { LocalStrategy } from './strategy/local.strategy';
import { JwtStrategy } from './strategy/jwt.strategy';
import { LocalStrategy } from '../../global/strategy/local.strategy';
import { JwtStrategy } from '../../global/strategy/jwt.strategy';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { NaverStrategy } from './strategy/naver.strategy';
import { NaverAuthGuard } from './guard/naver-auth.guard';
import { NaverStrategy } from '../../global/strategy/naver.strategy';
import { NaverAuthGuard } from '../../global/guard/naver-auth.guard';

@Module({
imports: [
Expand Down
20 changes: 0 additions & 20 deletions src/main/nest/domain/auth/controller/auth.controller.spec.ts

This file was deleted.

20 changes: 10 additions & 10 deletions src/main/nest/domain/auth/controller/auth.controller.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { Controller, UseGuards, Body, Req, Get, Post } from '@nestjs/common';
import { ApiTags, ApiBody, ApiOperation, ApiResponse } from '@nestjs/swagger';
import { ApiTags, ApiBody, ApiOperation, ApiOkResponse, ApiCreatedResponse } from '@nestjs/swagger';
import { UsersSocialType } from '@prisma/client';
import { AuthService } from '../service/auth.service';
import { CreateUserDto } from '../dto/create-user.dto';
import { LoginUserDto } from '../dto/login-user.dto';
import { LocalAuthGuard } from '../guard/local-auth.guard';
import { NaverAuthGuard } from '../guard/naver-auth.guard';
import { AccessTokenResponseDto, CreateUserRequestDto, LoginUserRequestDto } from '../../../global/dto';
import { LocalAuthGuard } from '../../../global/guard/local-auth.guard';
import { NaverAuthGuard } from '../../../global/guard/naver-auth.guard';

@ApiTags('Auth')
@Controller('auth')
Expand All @@ -14,17 +13,17 @@ export class AuthController {

@Post('signup')
@ApiOperation({ summary: '로컬 회원 가입' })
@ApiBody({ type: CreateUserDto })
@ApiResponse({ status: 201, description: '회원 가입 성공' })
async createUser(@Body() dto: CreateUserDto) {
@ApiBody({ type: CreateUserRequestDto })
@ApiCreatedResponse({ description: '회원 가입 성공' })
async createUser(@Body() dto: CreateUserRequestDto) {
const socialType: UsersSocialType = 'LOCAL';
return await this.authService.createUser(dto, socialType);
}

@Post('login')
@ApiOperation({ summary: '로컬 로그인' })
@ApiBody({ type: LoginUserDto })
@ApiResponse({ status: 200, description: '로그인 성공' })
@ApiBody({ type: LoginUserRequestDto })
@ApiCreatedResponse({ description: '로그인 성공', type: AccessTokenResponseDto })
@UseGuards(LocalAuthGuard)
async login(@Req() req: any) {
return this.authService.login(req.user);
Expand All @@ -37,6 +36,7 @@ export class AuthController {

@Get('callback/naver')
@ApiOperation({ summary: '네이버 로그인 callback' })
@ApiOkResponse({ description: '지정된 redirect_url로 이동합니다.' })
@UseGuards(NaverAuthGuard)
async naverAuthCallback(@Req() req: any) {
return this.authService.login(req.user);
Expand Down
4 changes: 0 additions & 4 deletions src/main/nest/domain/auth/dto/update-auth.dto.ts

This file was deleted.

18 changes: 0 additions & 18 deletions src/main/nest/domain/auth/service/auth.service.spec.ts

This file was deleted.

6 changes: 4 additions & 2 deletions src/main/nest/domain/auth/service/auth.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ConflictException, ForbiddenException, Injectable, UnauthorizedException } from '@nestjs/common';
import { PrismaService } from '../../../global/prisma/prisma.service';
import { CreateUserDto } from '../dto/create-user.dto';
import { CreateUserRequestDto } from '../../../global/dto';
import * as bcrypt from 'bcrypt';
import { JwtService } from '@nestjs/jwt';
import { UsersSocialType } from '@prisma/client';
Expand All @@ -12,7 +12,7 @@ export class AuthService {
private readonly jwtService: JwtService,
) {}

async createUser(dto: CreateUserDto, socialType: UsersSocialType) {
async createUser(dto: CreateUserRequestDto, socialType: UsersSocialType) {
console.log('🚀 dto: ', dto);
const nicknameCheck = await this.prismaService.users.findFirst({ where: { nickname: dto.nickname } });
switch (socialType) {
Expand Down Expand Up @@ -44,6 +44,8 @@ export class AuthService {
const currentTime = new Date();
const hashPassword = await bcrypt.hash(dto.password, 11);

await this.prismaService.emailVerifications.delete({ where: { email: dto.email } });

return await this.prismaService.users.create({
data: {
email: dto.email,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
ApiOperation,
ApiTags,
} from '@nestjs/swagger';
import { CreateMenuRequestDto, CreateMenuResponseDto, FindMenuResponseDto } from '../../../../global/dto';
import { CreateMenuRequestDto, CreateMenuResponseDto, FindMenuResponseDto } from '../../../global/dto';

@ApiTags('Menu')
@Controller('menus')
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ConflictException, Injectable } from '@nestjs/common';
import { PrismaService } from '../../../../global/prisma/prisma.service';
import { CreateMenuRequestDto } from '../../../../global/dto';
import { PrismaService } from '../../../global/prisma/prisma.service';
import { CreateMenuRequestDto } from '../../../global/dto';

@Injectable()
export class MenuService {
Expand All @@ -24,16 +24,20 @@ export class MenuService {
async findMenu(placeId: number) {
const findManyMenu = await this.prismaService.placeMenus.findMany({ where: { placeId } });

for (let i = findManyMenu.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[findManyMenu[i], findManyMenu[j]] = [findManyMenu[j], findManyMenu[i]];
}

return findManyMenu
.map((menu) => ({
id: Number(menu.id),
menu: menu.menu,
}))
.slice(0, 10);
const allMenus = findManyMenu.map((menu) => ({
id: Number(menu.id),
menu: menu.menu,
}));

const randomMenus = (menus: any[]) => {
for (let i = menus.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[menus[i], menus[j]] = [menus[j], menus[i]];
}
return menus.slice(0, 10);
};
const recommendMenus = randomMenus([...allMenus]);

return { allMenus, recommendMenus };
}
}
2 changes: 0 additions & 2 deletions src/main/nest/domain/place/place.module.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { Module } from '@nestjs/common';
import { PlaceService } from './service/place.service';
import { PlaceController } from './controller/place.controller';
import { MenuModule } from './menu/menu.module';

@Module({
controllers: [PlaceController],
providers: [PlaceService],
imports: [MenuModule],
})
export class PlaceModule {}
12 changes: 11 additions & 1 deletion src/main/nest/domain/post/controller/post.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
} from '@nestjs/swagger';
import { PostService } from '../service/post.service';
import { CreatePostRequestDto, FindOnePostResponseDto, UpdatePostRequestDto } from '../../../global/dto';
import { GetUserId, ApiGuard, ApiCreatePost, ApiUpdatePost } from '../../../global/decorator';
import { GetUserId, ApiGuard, ApiCreatePost, ApiUpdatePost, ApiOptionGuard } from '../../../global/decorator';

@ApiTags('Post')
@Controller('posts')
Expand Down Expand Up @@ -55,6 +55,16 @@ export class PostController {
return await this.postService.findOnePost(id);
}

@Post(':id/count')
@ApiOptionGuard()
@ApiOperation({ summary: '유저 게시물 조회수 증가' })
@ApiOkResponse({ description: '작성자는 조회수가 증가하지 않습니다.' })
@ApiCreatedResponse({ description: '게시물 조회수 증가' })
@ApiNotFoundResponse({ description: '해당 게시물은 존재하지 않습니다.' })
async countPost(@Param('id', ParseIntPipe) id: number, @GetUserId() userId: number) {
return await this.postService.countPost(id, userId);
}

@Get(':id/check')
@ApiOperation({ summary: '유저 게시물 수정 체크' })
@ApiOkResponse({ description: '수정가능 합니다' })
Expand Down
10 changes: 10 additions & 0 deletions src/main/nest/domain/post/service/post.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export class PostService {
menuTag: true,
keywordTag: true,
likeCount: true,
viewCount: true,
createdAt: true,
users: {
select: {
Expand Down Expand Up @@ -116,6 +117,15 @@ export class PostService {
return result;
}

async countPost(id: number, userId: number) {
const findPost = await this.prismaService.posts.findFirst({ where: { id, deletedAt: null } });
if (!findPost) {
throw new NotFoundException('해당 게시물은 존재하지 않습니다.');
} else if (userId !== undefined && findPost.userId === BigInt(userId))
throw { statusCode: 200, message: '작성자는 조회수가 증가하지 않습니다.' };
return await this.prismaService.posts.update({ where: { id }, data: { viewCount: { increment: 1 } } });
}

async checkPost(id: number) {
const postData = await this.prismaService.posts.findFirst({
where: { id, deletedAt: null },
Expand Down
Loading

0 comments on commit 459b77d

Please sign in to comment.