From 0e69db72f65db72dc21a89a0a9b4eb4fa1a0ab2e Mon Sep 17 00:00:00 2001 From: HyeongSeon Date: Tue, 28 Sep 2021 15:20:03 +0900 Subject: [PATCH] =?UTF-8?q?Revert=20"FEATURE:=20lending=EA=B4=80=EB=A0=A8?= =?UTF-8?q?=20=EC=BD=94=EB=93=9C=20=EC=9E=91=EC=84=B1=EC=A4=91"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/books/entities/bookInfo.entity.ts | 8 --- src/lendings/dto/create-lending.dto.ts | 6 +- src/lendings/entities/lending.entity.ts | 16 +----- src/lendings/lendings.controller.ts | 24 ++------ src/lendings/lendings.service.ts | 73 +++---------------------- src/users/entities/user.entity.ts | 4 +- 6 files changed, 18 insertions(+), 113 deletions(-) diff --git a/src/books/entities/bookInfo.entity.ts b/src/books/entities/bookInfo.entity.ts index 74c1cce..4907e19 100644 --- a/src/books/entities/bookInfo.entity.ts +++ b/src/books/entities/bookInfo.entity.ts @@ -27,25 +27,20 @@ export class BookInfo { id: number; @Column() - @Expose({ groups: ['findAll'] }) title: string; @Column() - @Exclude() author: string; @Column() - @Exclude() publisher: string; @Column({ nullable: true, }) - @Exclude() isbn: string; @Column() - @Exclude() image: string; @Column({ @@ -53,14 +48,12 @@ export class BookInfo { enum: BookCategory, default: BookCategory.WEB_PROGRAMMING, }) - @Exclude() category: BookCategory; @Column({ type: 'date', nullable: true, }) - @Exclude() publishedAt: Date; @Exclude() @@ -87,7 +80,6 @@ export class BookInfo { @Expose({ name: 'publishedAt', groups: ['detail'] }) getDate() { const date = new Date(this.publishedAt); - console.log(date); return date.getFullYear() + '년 ' + date.getMonth() + '월'; } } diff --git a/src/lendings/dto/create-lending.dto.ts b/src/lendings/dto/create-lending.dto.ts index cc25762..e6096e2 100644 --- a/src/lendings/dto/create-lending.dto.ts +++ b/src/lendings/dto/create-lending.dto.ts @@ -1,5 +1 @@ -export class CreateLendingDto { - condition: string; - userId: number; - bookId: number; -} +export class CreateLendingDto {} diff --git a/src/lendings/entities/lending.entity.ts b/src/lendings/entities/lending.entity.ts index 53353e6..d9eeb3d 100644 --- a/src/lendings/entities/lending.entity.ts +++ b/src/lendings/entities/lending.entity.ts @@ -12,7 +12,7 @@ import { import { User } from '../../users/entities/user.entity'; import { Returning } from '../../returns/entities/return.entity'; import { Book } from '../../books/entities/book.entity'; -import { Exclude, Expose } from 'class-transformer'; +import { Exclude } from 'class-transformer'; @Entity() export class Lending { @@ -21,39 +21,29 @@ export class Lending { } @PrimaryGeneratedColumn() + @Exclude() id: number; @Column({ default: '' }) - @Expose({groups:['findAll']}) + @Exclude() condition: string; @CreateDateColumn() - @Expose({groups:['findAll']}) createdAt: Date; @UpdateDateColumn() @Exclude() updatedAt: Date; - @Expose({groups:['findAll']}) @ManyToOne(() => User, (user) => user.lendings) user: User; @ManyToOne(() => User, (librarian) => librarian.librarianLendings) - @Exclude() librarian: User; - @Expose({groups:['findAll']}) @ManyToOne(() => Book, (book) => book.lendings) book: Book; @OneToOne(() => Returning, (returning) => returning.lending) - @Exclude returning: Returning; - - @Expose({ name: 'dueDate', groups: ['findAll'] }) - getDate() { - const date = new Date(this.createdAt); - return date.getFullYear() + '.' + date.getMonth() + '.' + date.getDate() + '.'; - } } diff --git a/src/lendings/lendings.controller.ts b/src/lendings/lendings.controller.ts index 2fd4307..3721253 100644 --- a/src/lendings/lendings.controller.ts +++ b/src/lendings/lendings.controller.ts @@ -7,40 +7,26 @@ import { Param, Delete, Query, - UseGuards, - Req, - UseInterceptors, - ClassSerializerInterceptor, - SerializeOptions, } from '@nestjs/common'; -import { JwtAuthGuard } from '../auth/jwt-auth.guard'; import { LendingsService } from './lendings.service'; import { UpdateLendingDto } from './dto/update-lending.dto'; -import { Lending } from './entities/lending.entity'; -import { CreateLendingDto } from './dto/create-lending.dto'; @Controller('lendings') export class LendingsController { constructor(private readonly lendingsService: LendingsService) {} - @UseGuards(JwtAuthGuard) @Post() - async create(@Req() req, @Body() createLendingDto: CreateLendingDto) { - // const librarianId = req.user.id; - const librarianId = 1; // - return this.lendingsService.create(createLendingDto, librarianId); + create(@Query('bookId') bookId: string, @Query('userId') userId: string) { + return this.lendingsService.create(+bookId, +userId); } - @SerializeOptions({ groups: ['findAll'] }) - @UseInterceptors(ClassSerializerInterceptor) + @Get() - async findAll() { + findAll() { return this.lendingsService.findAll(); } - @SerializeOptions({ groups: ['find'] }) - @UseInterceptors(ClassSerializerInterceptor) @Get(':id') - async findOne(@Param('id') id: string) { + findOne(@Param('id') id: string) { return this.lendingsService.findOne(+id); } diff --git a/src/lendings/lendings.service.ts b/src/lendings/lendings.service.ts index f708984..2ef7a58 100644 --- a/src/lendings/lendings.service.ts +++ b/src/lendings/lendings.service.ts @@ -1,83 +1,26 @@ -import { - BadRequestException, - Injectable, - MethodNotAllowedException, - NotFoundException, -} from '@nestjs/common'; +import { Injectable } from '@nestjs/common'; import { UpdateLendingDto } from './dto/update-lending.dto'; -import { Connection, getConnection, getManager, Repository } from 'typeorm'; +import { Repository } from 'typeorm'; import { Lending } from './entities/lending.entity'; import { InjectRepository } from '@nestjs/typeorm'; -import { User } from 'src/users/entities/user.entity'; -import { CreateLendingDto } from './dto/create-lending.dto'; - -async function checkLendingCnt(userId: number) { - const userData = await getConnection().getRepository('User').findOne(userId); - if (userData == undefined) return 0; - const today: Date = new Date(); - const penalty: Date = new Date(userData['penaltiyAt']); - if (2 <= userData['lendingCnt'] || today <= penalty) return 0; - return 1; -} - -async function checkLibrarian(librarianId: number) { - const librarian = await getConnection() - .getRepository('User') - .findOne(librarianId); - if (librarian == undefined) return 0; - if (!librarian['librarian']) return 0; - return 1; -} @Injectable() export class LendingsService { constructor( - private connection: Connection, @InjectRepository(Lending) private readonly lendingsRepository: Repository, ) {} - async create(dto: CreateLendingDto, librarianId: number) { - if ( - !(await checkLendingCnt(dto.userId)) || - !(await checkLibrarian(librarianId)) - ) - throw new BadRequestException(dto.userId || librarianId); - // TODO : 생성자 사용해서 줄이기 - try { - await this.connection.transaction(async (manager) => { - await manager.insert(Lending, { - condition: dto.condition, - user: { id: dto.userId }, - librarian: { id: librarianId }, - book: { id: dto.bookId }, - }); - await manager.update(User, dto.userId, { - lendingCnt: () => 'lendingCnt + 1', - }); - }); - } catch (e) { - throw new Error("lendings.service.create() catch'"); - } + async create(bookId: number, userId: number) { return 'This action adds a new lending'; } - // TODO : 페이지네이션, 시리얼라이제이션 - async findAll() { - return await this.lendingsRepository.find({ - relations: ['user', 'librarian', 'book', 'returning', 'book.info'], - where: { returning: null }, - }); + findAll() { + return `This action returns all lendings`; } - // TODO : 페이지네이션, 시리얼라이제이션 - async findOne(lendingId: number) { - const lendingData = await this.lendingsRepository.findOne({ - relations: ['user', 'librarian', 'book', 'returning', 'book.info'], - where: { id: lendingId }, - }); - if (lendingData == undefined || lendingData['returning']) - throw new NotFoundException(); - return lendingData; + + findOne(lendingId: number) { + return `This action returns a #${lendingId} lending`; } update(id: number, updateLendingDto: UpdateLendingDto) { diff --git a/src/users/entities/user.entity.ts b/src/users/entities/user.entity.ts index 58f75d2..73ef400 100644 --- a/src/users/entities/user.entity.ts +++ b/src/users/entities/user.entity.ts @@ -22,7 +22,6 @@ export class User { id: number; @Column() - @Expose({ groups: ['findAll'] }) login: string; @Column() @@ -34,11 +33,10 @@ export class User { slack: string; @Column({ type: 'datetime', default: () => 'CURRENT_TIMESTAMP' }) - @Expose({ groups: ['findAll'] }) + @Exclude() penaltiyAt: Date; @Column({ default: 0 }) - @Exclude() lendingCnt: number; @Column({ default: 0 })