Skip to content

Commit

Permalink
🔀 merge dev to be_v3/lent+return #114
Browse files Browse the repository at this point in the history
merge dev to be_v3/lent+return #114
  • Loading branch information
sichoi42 committed Nov 6, 2022
2 parents cb92b36 + 9f90692 commit b3c6f9f
Show file tree
Hide file tree
Showing 32 changed files with 1,905 additions and 15 deletions.
1 change: 1 addition & 0 deletions backend/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ async function bootstrap() {
.setTitle('Cabi Admin v2 API')
.setDescription('Cabi Admin v2 API 명세')
.setVersion('2.0')
.addBearerAuth()
.build();
const SwaggerDocument = SwaggerModule.createDocument(app, swaggerConfig);
SwaggerModule.setup('docs', app, SwaggerDocument);
Expand Down
189 changes: 187 additions & 2 deletions backend/src/v3/cabinet/cabinet.controller.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,36 @@
import { Controller, Logger, UseGuards } from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';
import {
Body,
Controller,
Get,
HttpException,
HttpStatus,
Logger,
Param,
ParseEnumPipe,
ParseIntPipe,
Patch,
UseGuards,
ValidationPipe,
} from '@nestjs/common';
import {
ApiBadRequestResponse,
ApiBearerAuth,
ApiForbiddenResponse,
ApiNoContentResponse,
ApiOkResponse,
ApiOperation,
ApiTags,
} from '@nestjs/swagger';
import { JWTAuthGuard } from 'src/auth/auth.guard';
import CabinetStatusType from 'src/enums/cabinet.status.type.enum';
import LentType from 'src/enums/lent.type.enum';
import { CabinetService } from './cabinet.service';
import { CabinetInfoResponseDto } from './dto/cabinet.info.response.dto';
import { CabinetStatusNoteRequestDto } from './dto/cabinet.status.note.request.dto';
import { CabinetTitleRequestDto } from './dto/cabinet.title.request.dto';

@ApiTags('(V3) Cabinet')
@ApiBearerAuth()
@Controller({
version: '3',
path: 'cabinet',
Expand All @@ -13,4 +40,162 @@ export class CabinetController {
constructor(private cabinetService: CabinetService) {}

private logger = new Logger(CabinetController.name);

@ApiOperation({
summary: '사물함 정보 호출',
description: 'cabinet_id를 받아 특정 사물함의 상세정보를 받아옵니다.',
})
@ApiOkResponse({
type: CabinetInfoResponseDto,
description:
'파라미터로 받은 사물함의 정보를 CabinetInfoResponseDto 형식으로 받아옵니다',
})
@ApiBadRequestResponse({
description: '비정상 파라미터',
})
@Get('/:cabinet_id')
async getCabinetInfo(
@Param('cabinet_id', ParseIntPipe) cabinet_id: number,
): Promise<CabinetInfoResponseDto> {
this.logger.debug(`Called ${this.getCabinetInfo.name}`);
return await this.cabinetService.getCabinetResponseInfo(cabinet_id);
}

@ApiOperation({
summary: '사물함 상태 변경',
description: 'cabinet_id를 받아 사물함의 상태를 변경합니다.',
})
@ApiNoContentResponse({
description: '성공',
})
@ApiBadRequestResponse({
description: '비정상 상태 및 cabinet_id',
})
@Patch('/status/:cabinet_id/:status')
async updateCabinetStatus(
@Param('cabinet_id', ParseIntPipe) cabinet_id: number,
@Param('status', new ParseEnumPipe(CabinetStatusType))
status: CabinetStatusType,
): Promise<void> {
this.logger.debug(`Called ${this.updateCabinetStatus.name}`);
await this.cabinetService.updateCabinetStatus(cabinet_id, status);
}

@ApiOperation({
summary: '사물함 lent_type 변경',
description: 'cabinet_id를 받아 사물함의 lent_type을 변경합니다.',
})
@ApiNoContentResponse({
description: '성공',
})
@ApiBadRequestResponse({
description: '비정상 lent_type 및 cabinet_id',
})
@ApiForbiddenResponse({
description: '대여 중인 캐비넷',
})
@Patch('/lent_type/:cabinet_id/:lent_type')
async updateLentType(
@Param('cabinet_id', ParseIntPipe) cabinet_id: number,
@Param('lent_type', new ParseEnumPipe(LentType)) lent_type: LentType,
): Promise<void> {
this.logger.debug(`Called ${this.updateLentType.name}`);
await this.cabinetService.updateLentType(cabinet_id, lent_type);
}

@ApiOperation({
summary: '사물함 고장 사유 변경',
description: 'cabinet_id를 받아 사물함의 고장 사유를 변경합니다.',
})
@ApiNoContentResponse({
description: '성공',
})
@ApiBadRequestResponse({
description: '존재하지 않는 cabinet_id',
})
@Patch('/status_note/:cabinet_id')
async updateStatusNote(
@Param('cabinet_id', ParseIntPipe) cabinet_id: number,
@Body(new ValidationPipe()) status_note: CabinetStatusNoteRequestDto,
): Promise<void> {
this.logger.debug(`Called ${this.updateStatusNote.name}`);
await this.cabinetService.updateStatusNote(
cabinet_id,
status_note.status_note,
);
}

@ApiOperation({
summary: '특정 사물함들의 상태 변경',
description: 'cabinet_id 배열을 받아 사물함들의 상태를 변경합니다.',
})
@ApiNoContentResponse({
description: '성공',
})
@ApiBadRequestResponse({
description: '비정상 상태 및 cabinet_id',
})
@Patch('/bundle/status/:status')
async updateCabinetStatusByBundle(
@Param('status', new ParseEnumPipe(CabinetStatusType))
status: CabinetStatusType,
@Body() bundle: number[],
): Promise<number[] | void> {
this.logger.debug(`Called ${this.updateCabinetStatusByBundle.name}`);
const fail = await this.cabinetService.updateCabinetStatusByBundle(
status,
bundle,
);
if (fail.length !== 0) {
throw new HttpException(fail, HttpStatus.BAD_REQUEST);
}
}

@ApiOperation({
summary: '특정 사물함들의 lent_type 변경',
description: 'cabinet_id 배열을 받아 사물함들의 lent_type을 변경합니다.',
})
@ApiNoContentResponse({
description: '성공',
})
@ApiBadRequestResponse({
description: '비정상 lent_type 및 cabinet_id',
})
@ApiForbiddenResponse({
description: '대여 중인 캐비넷',
})
@Patch('/bundle/lent_type/:lent_type')
async updateLentTypeByBundle(
@Param('lent_type', new ParseEnumPipe(LentType)) lent_type: LentType,
@Body() bundle: number[],
): Promise<number[] | void> {
this.logger.debug(`Called ${this.updateLentTypeByBundle.name}`);
const fail = await this.cabinetService.updateLentTypeByBundle(
lent_type,
bundle,
);
if (fail.length !== 0) {
throw new HttpException(fail, HttpStatus.BAD_REQUEST);
}
}

@ApiOperation({
summary: '사물함 title 변경',
description: 'cabinet_id를 받아 사물함의 title을 변경합니다.',
})
@ApiNoContentResponse({
description: '성공',
})
@ApiBadRequestResponse({
description: '존재하지 않는 cabinet_id',
})
@Patch('/title/:cabinet_id')
async updateCabinetTitle(
@Param('cabinet_id', ParseIntPipe) cabinet_id: number,
@Body(new ValidationPipe()) title: CabinetTitleRequestDto,
): Promise<void> {
this.logger.debug(`Called ${this.updateCabinetTitle.name}`);
console.log(title);
await this.cabinetService.updateCabinetTitle(cabinet_id, title.title);
}
}
150 changes: 149 additions & 1 deletion backend/src/v3/cabinet/cabinet.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
import { Inject, Injectable, Logger } from '@nestjs/common';
import {
HttpException,
HttpStatus,
Inject,
Injectable,
Logger,
} from '@nestjs/common';
import CabinetStatusType from 'src/enums/cabinet.status.type.enum';
import LentType from 'src/enums/lent.type.enum';
import { CabinetInfoResponseDto } from './dto/cabinet.info.response.dto';
import { ICabinetRepository } from './repository/cabinet.repository.interface';

@Injectable()
Expand All @@ -9,4 +18,143 @@ export class CabinetService {
@Inject('ICabinetRepository')
private cabinetRepository: ICabinetRepository,
) {}

async getCabinetResponseInfo(
cabinet_id: number,
): Promise<CabinetInfoResponseDto> {
this.logger.debug(
`Called ${CabinetService.name} ${this.getCabinetResponseInfo.name}`,
);
try {
return await this.cabinetRepository.getCabinetResponseInfo(cabinet_id);
} catch (e) {
throw new HttpException(
'🚨 존재하지 않는 사물함입니다 🚨',
HttpStatus.BAD_REQUEST,
);
}
}

async updateCabinetStatus(
cabinet_id: number,
status: CabinetStatusType,
): Promise<void> {
this.logger.debug(
`Called ${CabinetService.name} ${this.updateCabinetStatus.name}`,
);
if ((await this.isCabinetExist(cabinet_id)) === false) {
throw new HttpException(
'🚨 존재하지 않는 사물함입니다 🚨',
HttpStatus.BAD_REQUEST,
);
}
await this.cabinetRepository.updateCabinetStatus(cabinet_id, status);
}

async updateLentType(cabinet_id: number, lent_type: LentType): Promise<void> {
this.logger.debug(
`Called ${CabinetService.name} ${this.updateLentType.name}`,
);
const isLent = await this.cabinetRepository.cabinetIsLent(cabinet_id);
if (isLent === true) {
throw new HttpException(
'🚨 대여자가 있는 사물함입니다 🚨',
HttpStatus.FORBIDDEN,
);
}
if ((await this.isCabinetExist(cabinet_id)) === false) {
throw new HttpException(
'🚨 존재하지 않는 사물함입니다 🚨',
HttpStatus.BAD_REQUEST,
);
}
await this.cabinetRepository.updateLentType(cabinet_id, lent_type);
}

async updateStatusNote(
cabinet_id: number,
status_note: string,
): Promise<void> {
this.logger.debug(
`Called ${CabinetService.name} ${this.updateStatusNote.name}`,
);
if ((await this.isCabinetExist(cabinet_id)) === false) {
throw new HttpException(
'🚨 존재하지 않는 사물함입니다 🚨',
HttpStatus.BAD_REQUEST,
);
}
await this.cabinetRepository.updateStatusNote(cabinet_id, status_note);
}

async updateCabinetStatusByBundle(
status: CabinetStatusType,
bundle: number[],
): Promise<number[]> {
this.logger.debug(
`Called ${CabinetService.name} ${this.updateCabinetStatusByBundle.name}`,
);
const result = [];
for (const cabinet_id of bundle) {
if ((await this.isCabinetExist(cabinet_id)) === false) {
result.push(cabinet_id);
continue;
}
try {
await this.cabinetRepository.updateCabinetStatus(cabinet_id, status);
} catch (e) {
result.push(cabinet_id);
continue;
}
}
return result;
}

async updateLentTypeByBundle(
lent_type: LentType,
bundle: number[],
): Promise<number[]> {
this.logger.debug(
`Called ${CabinetService.name} ${this.updateCabinetStatusByBundle.name}`,
);
const result = [];
for (const cabinet_id of bundle) {
const isLent = await this.cabinetRepository.cabinetIsLent(cabinet_id);
if (isLent === true) {
result.push(cabinet_id);
continue;
}
if ((await this.isCabinetExist(cabinet_id)) === false) {
result.push(cabinet_id);
continue;
}
try {
await this.cabinetRepository.updateLentType(cabinet_id, lent_type);
} catch (e) {
result.push(cabinet_id);
continue;
}
}
return result;
}

async updateCabinetTitle(cabinet_id: number, title: string): Promise<void> {
this.logger.debug(
`Called ${CabinetService.name} ${this.updateCabinetTitle.name}`,
);
if ((await this.isCabinetExist(cabinet_id)) === false) {
throw new HttpException(
'🚨 존재하지 않는 사물함입니다 🚨',
HttpStatus.BAD_REQUEST,
);
}
await this.cabinetRepository.updateCabinetTitle(cabinet_id, title);
}

async isCabinetExist(cabinet_id: number): Promise<boolean> {
this.logger.debug(
`Called ${CabinetService.name} ${this.isCabinetExist.name}`,
);
return await this.cabinetRepository.isCabinetExist(cabinet_id);
}
}
Loading

0 comments on commit b3c6f9f

Please sign in to comment.