Skip to content

Commit

Permalink
MERGE: pull request #109 from jiphyeonjeon-42/issue/108
Browse files Browse the repository at this point in the history
FEATURE: 대출 페이지에 필요한 api 수정
  • Loading branch information
DiceMono authored Nov 21, 2021
2 parents 224e5a6 + 5550664 commit f6a6c58
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 8 deletions.
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 @@ -53,6 +53,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

0 comments on commit f6a6c58

Please sign in to comment.