Skip to content

Commit

Permalink
✨ add cabinet/lent API #114
Browse files Browse the repository at this point in the history
add cabinet/lent API #114
  • Loading branch information
sichoi42 committed Nov 5, 2022
1 parent d6ecff8 commit 03d0a37
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 38 deletions.
11 changes: 11 additions & 0 deletions backend/src/config/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,15 @@ export default () => ({
secret: process.env.JWT_SECRET,
expiresIn: process.env.JWT_EXPIRES,
},
lent_term: {
private: parseInt(process.env.LENT_TERM_PRIVATE, 10),
share: parseInt(process.env.LENT_TERM_SHARE, 10),
},
expire_term: {
soonoverdue: parseInt(process.env.EXPIRE_TERM_SOONOVERDUE, 10),
overdue: parseInt(process.env.EXPIRE_TERM_OVERDUE, 10),
lastoverdue: parseInt(process.env.EXPIRE_TERM_LASTOVERDUE, 10),
forcedreturn: parseInt(process.env.EXPIRE_TERM_FORCEDRETURN, 10),
},
penalty_day_share: parseInt(process.env.PENALTY_DAY_SHARE, 10),
});
21 changes: 11 additions & 10 deletions backend/src/v3/lent/lent.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ConfigService } from "@nestjs/config";
import CabinetStatusType from "src/enums/cabinet.status.type.enum";
import LentExceptionType from "src/enums/lent.exception.enum";
import LentType from "src/enums/lent.type.enum";
import { IsolationLevel, Propagation, runOnTransactionComplete, Transactional } from "typeorm-transactional";
import { UserDto } from "./dto/user.dto";
import { LentService } from "./lent.service";
import { ILentRepository } from "./repository/lent.repository.interface";
Expand All @@ -26,10 +27,10 @@ export class LentTools {
* @param last_lent_time
* @param lent_type
*/
// @Transactional({
// propagation: Propagation.REQUIRED,
// isolationLevel: IsolationLevel.SERIALIZABLE,
// })
@Transactional({
propagation: Propagation.REQUIRED,
isolationLevel: IsolationLevel.SERIALIZABLE,
})
async setExpireTimeAll(
cabinet_id: number,
last_lent_time: Date,
Expand All @@ -49,13 +50,13 @@ export class LentTools {
);
}
await this.lentRepository.setExpireTimeAll(cabinet_id, expire_time);
// runOnTransactionComplete((err) => err && this.logger.error(err));
runOnTransactionComplete((err) => err && this.logger.error(err));
}

// @Transactional({
// propagation: Propagation.REQUIRED,
// isolationLevel: IsolationLevel.SERIALIZABLE,
// })
@Transactional({
propagation: Propagation.REQUIRED,
isolationLevel: IsolationLevel.SERIALIZABLE,
})
async lentStateTransition(
cabinet_id: number,
user: UserDto,
Expand Down Expand Up @@ -121,7 +122,7 @@ export class LentTools {
excepction_type = LentExceptionType.LENT_BANNED;
break;
}
// runOnTransactionComplete((err) => err && this.logger.error(err));
runOnTransactionComplete((err) => err && this.logger.error(err));
return excepction_type;
}
}
20 changes: 10 additions & 10 deletions backend/src/v3/lent/lent.controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Controller, Get, HttpCode, HttpException, HttpStatus, InternalServerErrorException, Logger, Param, Post, UseGuards } from '@nestjs/common';
import { ApiBadRequestResponse, ApiConflictResponse, ApiCreatedResponse, ApiForbiddenResponse, ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
import { Controller, Get, HttpCode, HttpException, HttpStatus, InternalServerErrorException, Logger, Param, ParseIntPipe, Post, UseGuards } from '@nestjs/common';
import { ApiBadRequestResponse, ApiConflictResponse, ApiCreatedResponse, ApiForbiddenResponse, ApiNotFoundResponse, ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
import { JWTAuthGuard } from 'src/auth/auth.guard';
import { LentService } from './lent.service';

Expand All @@ -20,28 +20,28 @@ export class LentController {
@ApiCreatedResponse({
description: '대여에 성공 시, 201 Created를 응답합니다.',
})
@ApiBadRequestResponse({
description:
'이미 대여중인 사물함이 있는 경우, 400 Bad_Request를 응답합니다.',
@ApiNotFoundResponse({
description: '해당 캐비넷이나 유저가 존재하지 않으면, 404 Not Found를 응답합니다.',
})
@ApiForbiddenResponse({
@ApiBadRequestResponse({
description:
'임시 밴 사물함이나 고장 사물함을 대여 시도한 경우, 403 Forbidden을 응답합니다.',
'이미 대여중인 사물함이 있는 경우, 400 Bad Request를 응답합니다.',
})
@ApiResponse({
status: HttpStatus.I_AM_A_TEAPOT,
description:
"동아리 사물함을 대여 시도한 경우, 418 I'm a teapot을 응답합니다.",
})
@ApiConflictResponse({
description: '잔여 자리가 없는 경우, 409 Conflict를 응답합니다.',
description: `잔여 자리가 없는 경우, 연체 사물함을 대여 시도한 경우,
임시 밴 사물함이나 고장 사물함을 대여 시도한 경우 409 Conflict를 응답합니다.`,
})
@Post('/cabinet/:cabinet_id/:user_id')
@HttpCode(HttpStatus.CREATED)
@UseGuards(JWTAuthGuard)
async lentCabinet(
@Param('cabinet_id') cabinet_id: number,
@Param('user_id') user_id: number,
@Param('cabinet_id', ParseIntPipe) cabinet_id: number,
@Param('user_id', ParseIntPipe) user_id: number,
): Promise<void> {
try {
this.logger.debug(`Called ${this.lentCabinet.name}`);
Expand Down
2 changes: 1 addition & 1 deletion backend/src/v3/lent/lent.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export class LentService {
switch (excepction_type) {
case LentExceptionType.LENT_CIRCLE:
throw new HttpException(
`🚨 해당 사물함은 동아리 전용 사물함입니다 🚨`,
`🚨 해당 사물함은 동아리 전용 사물함입니다. 🚨`,
HttpStatus.I_AM_A_TEAPOT,
);
case LentExceptionType.LENT_FULL:
Expand Down
35 changes: 18 additions & 17 deletions backend/src/v3/lent/repository/lent.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Cabinet from "src/entities/cabinet.entity";
import Lent from "src/entities/lent.entity";
import User from "src/entities/user.entity";
import { Repository } from "typeorm";
import { IsolationLevel, Propagation, Transactional } from "typeorm-transactional";
import { LentCabinetDataDto } from "../dto/lent.cabinet.data.dto";
import { LentDto } from "../dto/lent.dto";
import { UserDto } from "../dto/user.dto";
Expand Down Expand Up @@ -34,7 +35,7 @@ export class LentRepository implements ILentRepository {
}

async isCabinetExist(cabinet_id: number): Promise<boolean> {
const result = await this.cabinetRepository.find({
const result = await this.cabinetRepository.findOne({
where: {
cabinet_id: cabinet_id,
}
Expand All @@ -57,10 +58,10 @@ export class LentRepository implements ILentRepository {
return true;
}

// @Transactional({
// propagation: Propagation.REQUIRED,
// isolationLevel: IsolationLevel.SERIALIZABLE,
// })
@Transactional({
propagation: Propagation.REQUIRED,
isolationLevel: IsolationLevel.SERIALIZABLE,
})
async getLentCabinetData(cabinet_id: number): Promise<LentCabinetDataDto> {
const result = await this.cabinetRepository
.createQueryBuilder('c')
Expand All @@ -82,10 +83,10 @@ export class LentRepository implements ILentRepository {
};
}

// @Transactional({
// propagation: Propagation.REQUIRED,
// isolationLevel: IsolationLevel.SERIALIZABLE,
// })
@Transactional({
propagation: Propagation.REQUIRED,
isolationLevel: IsolationLevel.SERIALIZABLE,
})
async lentCabinet(cabinet_id: number, user: UserDto): Promise<LentDto> {
const lent_time = new Date();
const expire_time: Date | null = null;
Expand All @@ -110,10 +111,10 @@ export class LentRepository implements ILentRepository {
};
}

// @Transactional({
// propagation: Propagation.REQUIRED,
// isolationLevel: IsolationLevel.SERIALIZABLE,
// })
@Transactional({
propagation: Propagation.REQUIRED,
isolationLevel: IsolationLevel.SERIALIZABLE,
})
async setExpireTimeAll(cabinet_id: number, expire_time: Date): Promise<void> {
expire_time.setHours(23, 59, 59, 999);
await this.lentRepository
Expand All @@ -128,10 +129,10 @@ export class LentRepository implements ILentRepository {
.execute();
}

// @Transactional({
// propagation: Propagation.REQUIRED,
// isolationLevel: IsolationLevel.SERIALIZABLE,
// })
@Transactional({
propagation: Propagation.REQUIRED,
isolationLevel: IsolationLevel.SERIALIZABLE,
})
async setExpireTime(lent_id: number, expire_time: Date): Promise<void> {
expire_time.setHours(23, 59, 59, 999);
await this.lentRepository
Expand Down

0 comments on commit 03d0a37

Please sign in to comment.