Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

♻️ refactor: admin, statistic 리팩토링 #269

Merged
merged 7 commits into from
Dec 5, 2024
24 changes: 11 additions & 13 deletions server/src/admin/admin.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,20 @@ import { Request, Response } from 'express';
import { AdminService } from './admin.service';
import { RegisterAdminDto } from './dto/register-admin.dto';
import { ApiTags } from '@nestjs/swagger';
import {
ApiCheckAdminSessionId,
ApiLogout,
ApiPostLoginAdmin,
ApiPostRegisterAdmin,
} from './admin.api-docs';
import { ApiResponse } from '../common/response/common.response';
import { LoginAdminDto } from './dto/login-admin.dto';
import { CookieAuthGuard } from '../common/guard/auth.guard';
import { ApiLoginAdmin } from './api-docs/loginAdmin.api-docs';
import { ApiReadSessionIdAdmin } from './api-docs/readSessionIdAdmin.api-docs';
import { ApiLogoutAdmin } from './api-docs/logoutAdmin.api-docs';
import { ApiCreateAdmin } from './api-docs/createAdmin.api-docs';

@ApiTags('Admin')
@Controller('admin')
export class AdminController {
constructor(private readonly adminService: AdminService) {}

@ApiPostLoginAdmin()
@ApiLoginAdmin()
@Post('/login')
@HttpCode(HttpStatus.OK)
@UsePipes(ValidationPipe)
Expand All @@ -45,7 +43,7 @@ export class AdminController {
);
}

@ApiLogout()
@ApiLogoutAdmin()
@UseGuards(CookieAuthGuard)
@HttpCode(HttpStatus.OK)
@Post('/logout')
Expand All @@ -60,23 +58,23 @@ export class AdminController {
);
}

@ApiPostRegisterAdmin()
@ApiCreateAdmin()
@UseGuards(CookieAuthGuard)
@Post('/register')
@UsePipes(ValidationPipe)
async registerAdmin(@Body() registerAdminDto: RegisterAdminDto) {
await this.adminService.registerAdmin(registerAdminDto);
async createAdmin(@Body() registerAdminDto: RegisterAdminDto) {
await this.adminService.createAdmin(registerAdminDto);
return ApiResponse.responseWithNoContent(
'성공적으로 관리자 계정이 생성되었습니다.',
);
}

@ApiCheckAdminSessionId()
@ApiReadSessionIdAdmin()
@Get('/sessionId')
@HttpCode(HttpStatus.OK)
@UseGuards(CookieAuthGuard)
@UsePipes(new ValidationPipe({ transform: true }))
async checkAdminSessionId() {
async readSessionIdAdmin() {
return ApiResponse.responseWithNoContent('정상적인 sessionId 입니다.');
}
}
2 changes: 1 addition & 1 deletion server/src/admin/admin.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export class AdminRepository extends Repository<Admin> {
super(Admin, dataSource.createEntityManager());
}

async registerAdmin(registerAdminDto: RegisterAdminDto) {
async createAdmin(registerAdminDto: RegisterAdminDto) {
const { loginId, password } = registerAdminDto;
const admin = this.create({
loginId,
Expand Down
4 changes: 2 additions & 2 deletions server/src/admin/admin.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export class AdminService {
response.clearCookie('sessionId');
}

async registerAdmin(registerAdminDto: RegisterAdminDto) {
async createAdmin(registerAdminDto: RegisterAdminDto) {
let { loginId, password } = registerAdminDto;

const existingAdmin = await this.adminRepository.findOne({
Expand All @@ -106,6 +106,6 @@ export class AdminService {
const saltRounds = 10;
password = await bcrypt.hash(password, saltRounds);

return this.adminRepository.registerAdmin({ loginId, password });
await this.adminRepository.createAdmin({ loginId, password });
}
}
47 changes: 47 additions & 0 deletions server/src/admin/api-docs/createAdmin.api-docs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { applyDecorators } from '@nestjs/common';
import {
ApiBadRequestResponse,
ApiConflictResponse,
ApiCreatedResponse,
ApiOperation,
ApiUnauthorizedResponse,
} from '@nestjs/swagger';

export function ApiCreateAdmin() {
return applyDecorators(
ApiOperation({
summary: `관리자 회원 가입 API`,
}),
ApiCreatedResponse({
description: 'Created',
schema: {
properties: {
message: {
type: 'string',
},
},
},
example: {
message: '성공적으로 관리자 계정이 생성되었습니다.',
},
}),
ApiBadRequestResponse({
description: 'Bad Request',
example: {
message: '오류 메세지',
},
}),
ApiConflictResponse({
description: 'Conflict',
example: {
message: '이미 존재하는 계정입니다.',
},
}),
ApiUnauthorizedResponse({
description: 'Unauthorized',
example: {
message: '인증되지 않은 요청입니다.',
},
}),
);
}
40 changes: 40 additions & 0 deletions server/src/admin/api-docs/loginAdmin.api-docs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { applyDecorators } from '@nestjs/common';
import {
ApiBadRequestResponse,
ApiOkResponse,
ApiOperation,
ApiUnauthorizedResponse,
} from '@nestjs/swagger';

export function ApiLoginAdmin() {
return applyDecorators(
ApiOperation({
summary: `관리자 로그인 API`,
}),
ApiOkResponse({
description: 'Ok',
schema: {
properties: {
message: {
type: 'string',
},
},
},
example: {
message: '로그인이 성공적으로 처리되었습니다.',
},
}),
ApiBadRequestResponse({
description: 'Bad Request',
example: {
message: '오류 메세지',
},
}),
ApiUnauthorizedResponse({
description: 'Unauthorized',
example: {
message: '아이디 혹은 비밀번호가 잘못되었습니다.',
},
}),
);
}
33 changes: 33 additions & 0 deletions server/src/admin/api-docs/logoutAdmin.api-docs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import {
ApiOkResponse,
ApiOperation,
ApiUnauthorizedResponse,
} from '@nestjs/swagger';
import { applyDecorators } from '@nestjs/common';

export function ApiLogoutAdmin() {
return applyDecorators(
ApiOperation({
summary: '관리자 로그아웃 API',
}),
ApiOkResponse({
description: 'Ok',
schema: {
properties: {
message: {
type: 'string',
},
},
},
example: {
message: '로그아웃이 성공적으로 처리되었습니다.',
},
}),
ApiUnauthorizedResponse({
description: 'Unauthorized',
example: {
message: '인증되지 않은 요청입니다.',
},
}),
);
}
60 changes: 60 additions & 0 deletions server/src/admin/api-docs/readSessionIdAdmin.api-docs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { applyDecorators } from '@nestjs/common';
import {
ApiOkResponse,
ApiOperation,
ApiUnauthorizedResponse,
} from '@nestjs/swagger';

export function ApiReadSessionIdAdmin() {
return applyDecorators(
ApiOperation({
summary: `관리자 페이지 출력을 위한 sessionId 확인 API`,
}),
ApiOkResponse({
description: 'Ok',
schema: {
properties: {
message: {
type: 'string',
},
},
},
example: {
message: '정상적인 sessionId입니다.',
},
}),
ApiUnauthorizedResponse({
description: 'Unauthorized',
example: {
message: '인증되지 않은 요청입니다.',
},
}),
);
}

export function ApiLogout() {
return applyDecorators(
ApiOperation({
summary: '관리자 로그아웃 API',
}),
ApiOkResponse({
description: 'Ok',
schema: {
properties: {
message: {
type: 'string',
},
},
},
example: {
message: '로그아웃이 성공적으로 처리되었습니다.',
},
}),
ApiUnauthorizedResponse({
description: 'Unauthorized',
example: {
message: '인증되지 않은 요청입니다.',
},
}),
);
}
10 changes: 10 additions & 0 deletions server/src/feed/feed.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,14 @@ export class FeedRepository extends Repository<Feed> {
return '(MATCH(feed.title) AGAINST (:find IN NATURAL LANGUAGE MODE) OR MATCH(rss_accept.name) AGAINST (:find IN NATURAL LANGUAGE MODE))';
}
}

async findAllStatisticsOrderByViewCount(limit: number) {
return this.find({
select: ['id', 'title', 'viewCount'],
order: {
viewCount: 'DESC',
},
take: limit,
});
}
}
42 changes: 42 additions & 0 deletions server/src/statistic/api-docs/readPlatformStatistic.api-docs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { applyDecorators } from '@nestjs/common';
import { ApiOkResponse, ApiOperation } from '@nestjs/swagger';

export function ApiReadPlatformStatistic() {
return applyDecorators(
ApiOperation({
summary: '블로그 플랫폼 통계 조회 API',
}),
ApiOkResponse({
description: 'Ok',
schema: {
properties: {
message: {
type: 'string',
},
data: {
type: 'array',
items: {
properties: {
platform: {
type: 'string',
},
count: {
type: 'number',
},
},
},
},
},
},
example: {
message: '블로그 플랫폼 통계 조회 완료',
data: [
{
platform: 'test',
count: 30,
},
],
},
}),
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ import {
ApiOkResponse,
ApiOperation,
ApiQuery,
ApiUnauthorizedResponse,
} from '@nestjs/swagger';


export function ApiStatistic(category: 'today' | 'all') {
const type = category === 'all' ? '전체' : '금일';
return applyDecorators(
Expand Down Expand Up @@ -66,43 +64,3 @@ export function ApiStatistic(category: 'today' | 'all') {
}),
);
}

export function ApiPlatformStatistic() {
return applyDecorators(
ApiOperation({
summary: '블로그 플랫폼 통계 조회 API',
}),
ApiOkResponse({
description: 'Ok',
schema: {
properties: {
message: {
type: 'string',
},
data: {
type: 'array',
items: {
properties: {
platform: {
type: 'string',
},
count: {
type: 'number',
},
},
},
},
},
},
example: {
message: '블로그 플랫폼 통계 조회 완료',
data: [
{
platform: 'test',
count: 30,
},
],
},
}),
);
}
Loading
Loading