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

FEATURE: 대출 페이지에 필요한 api 수정 #109

Merged
merged 2 commits into from
Nov 21, 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
6 changes: 6 additions & 0 deletions src/books/books.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ export class BooksService {
const queryBuilder = await this.booksRepository
.createQueryBuilder('book')
.leftJoinAndSelect('book.info', 'info')
.leftJoinAndSelect(
'book.lendings',
'lendings',
'NOT EXISTS(SELECT * FROM returning where returning.lendingId = lendings.id)',
)
.leftJoinAndSelect('lendings.returning', 'returning')
.where('book.callSign like :query', { query: `%${query}%` })
.orWhere('info.title like :query', { query: `%${query}%` })
.orWhere('info.author like :query', { query: `%${query}%` })
Expand Down
9 changes: 9 additions & 0 deletions src/books/entities/book.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,13 @@ export class Book {
} else if (this.status == 1) return '분실';
else if (this.status == 2) return '파손';
}

@Expose({ groups: ['books.searchBook'] })
get isLenderable() {
if (!this.lendings || this.lendings.length == 0) return true;
const lastLending = this.lendings.sort(
(a, b) => b.createdAt.getTime() - a.createdAt.getTime(),
)[0];
return !!lastLending.returning;
}
}
1 change: 1 addition & 0 deletions src/books/entities/bookInfo.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export class BookInfo {
'lendings.search',
'lendings.findOne',
'reservations.search',
'users.search',
],
})
title: string;
Expand Down
11 changes: 8 additions & 3 deletions src/lendings/entities/lending.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,22 @@ export class Lending {
@Expose({ groups: [] })
librarian: User;

@Expose({ groups: ['lendings.search', 'lendings.findOne'] })
@Expose({ groups: ['lendings.search', 'lendings.findOne', 'users.search'] })
@ManyToOne(() => Book, (book) => book.lendings, { nullable: false })
book: Book;

@OneToOne(() => Returning, (returning) => returning.lending)
@Expose({ groups: [] })
@Expose({ groups: ['users.search'] })
returning: Returning;

@Expose({
name: 'dueDate',
groups: ['lendings.search', 'lendings.findOne', 'reservations.search'],
groups: [
'lendings.search',
'lendings.findOne',
'reservations.search',
'users.search',
],
})
getdueDate() {
if (this.returning) throw new Error('lendings.service.find(All) catch');
Expand Down
25 changes: 23 additions & 2 deletions src/reservations/entities/reservation.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class Reservation {
@Column({
nullable: true,
})
@Expose({ groups: ['reservations.search'] })
@Expose({ groups: ['reservations.search', 'users.search'] })
endAt: Date;

@CreateDateColumn()
Expand All @@ -43,6 +43,27 @@ export class Reservation {
user: User;

@ManyToOne(() => Book, (book) => book.reservations)
@Expose({ groups: ['reservations.search'] })
@Expose({ groups: ['reservations.search', 'users.search'] })
book: Book;

@Expose({ name: 'lenderableDate', groups: ['users.search'] })
getLenderableDate() {
return this.book.getDueDate();
}

@Expose({ name: 'endAt', groups: ['users.search'] })
getEndAt() {
if (!this.endAt) {
return '-';
}
return this.endAt.toJSON().substring(2, 10).split('-').join('.') + '.';
}

@Expose({ name: 'rank', groups: ['users.search'] })
getRank() {
if (!this.endAt) {
return null;
}
return 1;
}
}
21 changes: 18 additions & 3 deletions src/users/entities/user.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ export class User {

@Column()
@Expose({
groups: ['lendings.search', 'lendings.findOne', 'reservations.search'],
groups: [
'lendings.search',
'lendings.findOne',
'reservations.search',
'users.search',
],
})
login: string;

Expand Down Expand Up @@ -64,11 +69,11 @@ export class User {
librarianLendings: Lending[];

@OneToMany(() => Lending, (lending) => lending.user)
@Expose({ groups: [] })
@Expose({ groups: ['users.search'] })
lendings: Lending[];

@OneToMany(() => Reservation, (reservation) => reservation.user)
@Expose({ groups: [] })
@Expose({ groups: ['users.search'] })
reservations: Reservation[];

@Expose({ groups: ['users.search'] })
Expand All @@ -85,4 +90,14 @@ export class User {
if (penalty < today) return 0;
return Math.ceil(Math.abs(+penalty - +today) / (1000 * 3600 * 24));
}

@Expose({
groups: ['users.search'],
})
get lendingCnt() {
if (!this.lendings) {
return 0;
}
return this.lendings.length;
}
}
19 changes: 19 additions & 0 deletions src/users/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,25 @@ export class UsersService {
async searchByLogin(login: string, options: IPaginationOptions) {
const queryBuilder = await this.userRepository
.createQueryBuilder('user')
.leftJoinAndSelect(
'user.reservations',
'reservations',
'reservations.canceledAt IS NULL AND (reservations.endAt IS NULL OR reservations.endAt >= :current)',
{ current: new Date() },
)
.leftJoinAndSelect('reservations.book', 'reservationsBook')
.leftJoinAndSelect('reservationsBook.info', 'reservationsBookInfo')
.leftJoinAndSelect(
'reservationsBook.lendings',
'reservationsBookLendings',
)
.leftJoinAndSelect(
'user.lendings',
'lendings',
'NOT EXISTS(SELECT * FROM returning where returning.lendingId = lendings.id)',
)
.leftJoinAndSelect('lendings.book', 'lendingsBook')
.leftJoinAndSelect('lendingsBook.info', 'lendingsBookInfo')
.where('user.login like :login', { login: `%${login}%` });
return paginate(queryBuilder, options);
}
Expand Down