From 5e47ec71afbdeb31669fe567b6d9f39ef3770d33 Mon Sep 17 00:00:00 2001 From: minkykim Date: Tue, 14 Sep 2021 16:37:42 +0900 Subject: [PATCH] =?UTF-8?q?STYLE:=20entity=20=EB=82=B4=EC=97=90=EC=84=9C?= =?UTF-8?q?=20=EB=8D=B0=EC=9D=B4=ED=84=B0=20=EB=A6=AC=ED=84=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/books/books.service.ts | 30 ++++++--------------------- src/books/entities/book.entity.ts | 11 +++++++++- src/books/entities/bookInfo.entity.ts | 12 +++++++---- 3 files changed, 24 insertions(+), 29 deletions(-) diff --git a/src/books/books.service.ts b/src/books/books.service.ts index a45f2bd..85cab38 100644 --- a/src/books/books.service.ts +++ b/src/books/books.service.ts @@ -8,19 +8,6 @@ import { paginate, IPaginationOptions } from 'nestjs-typeorm-paginate'; import { getConnection } from 'typeorm'; import { SearchService } from 'src/search/search.service'; -async function setBookDatas(bookData) { - if (bookData == undefined) throw new NotFoundException(bookData); - for (const book of bookData.books) { - if (book.status == 1) book.status = '비치중'; - else if (book.status == 2) book.status = '대출중'; - else if (book.status == 3) book.status = '분실'; - else if (book.status == 4) book.status = '파손'; - } - const date = new Date(bookData.publishedAt); - bookData.publishedAt = date.getFullYear() + '년 ' + date.getMonth() + '월'; - return bookData; -} - @Injectable() export class BooksService { constructor( @@ -70,17 +57,12 @@ export class BooksService { const connection = getConnection(); const bookInfoRepository = connection.getRepository(BookInfo); - return await bookInfoRepository - .findOne({ - where: { id: bookInfoId }, - relations: ['books', 'books.lendings', 'books.lendings.returning'], - }) - .then((bookData) => { - return setBookDatas(bookData); - }) - .then((tBookData) => { - return tBookData; - }); + let bookData = await bookInfoRepository.findOne({ + where: { id: bookInfoId }, + relations: ['books', 'books.lendings', 'books.lendings.returning'], + }); + if (bookData == undefined) throw new NotFoundException(bookData); + return bookData; } async findInfo(options: IPaginationOptions, sort: string) { diff --git a/src/books/entities/book.entity.ts b/src/books/entities/book.entity.ts index ec1bdc7..536df36 100644 --- a/src/books/entities/book.entity.ts +++ b/src/books/entities/book.entity.ts @@ -25,6 +25,7 @@ export class Book { @Column() callSign: string; + @Exclude() @Column() status: number; @@ -46,7 +47,7 @@ export class Book { @Exclude() lendings: Lending[]; - @Expose({ name: 'dueDate' }) + @Expose({ name: 'dueDate', groups: ['detail'] }) getDueDate() { if (this.lendings.length == 0) return '-'; for (const lending of this.lendings) { @@ -59,4 +60,12 @@ export class Book { } } } + + @Expose({ name: 'status', groups: ['detail'] }) + getStatus() { + if (this.status == 1) return '비치중'; + else if (this.status == 2) return '대출중'; + else if (this.status == 3) return '분실'; + else if (this.status == 4) return '파손'; + } } diff --git a/src/books/entities/bookInfo.entity.ts b/src/books/entities/bookInfo.entity.ts index 04c7bcb..4907e19 100644 --- a/src/books/entities/bookInfo.entity.ts +++ b/src/books/entities/bookInfo.entity.ts @@ -67,10 +67,8 @@ export class BookInfo { @OneToMany(() => Book, (book) => book.info) books: Book[]; - @Expose({ - groups: ['detail'], - }) - donators() { + @Expose({ name: 'donators', groups: ['detail'] }) + getDonators() { const donators = new Set(); for (const book of this.books) { if (book.donator != '') donators.add(book.donator); @@ -78,4 +76,10 @@ export class BookInfo { if (donators.size == 0) return '-'; return [...donators].join(', '); } + + @Expose({ name: 'publishedAt', groups: ['detail'] }) + getDate() { + const date = new Date(this.publishedAt); + return date.getFullYear() + '년 ' + date.getMonth() + '월'; + } }