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

STYLE: entity 내에서 데이터 리턴 #28

Merged
merged 1 commit into from
Sep 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 6 additions & 24 deletions src/books/books.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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) {
Expand Down
11 changes: 10 additions & 1 deletion src/books/entities/book.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export class Book {
@Column()
callSign: string;

@Exclude()
@Column()
status: number;

Expand All @@ -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) {
Expand All @@ -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 '파손';
}
}
12 changes: 8 additions & 4 deletions src/books/entities/bookInfo.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,19 @@ 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);
}
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() + '월';
}
}