File size: 3,594 Bytes
22df730
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2ff651b
 
22df730
2ff651b
22df730
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import {
  Controller,
  Get,
  Post,
  Patch,
  Delete,
  Param,
  Body,
  NotFoundException,
  BadRequestException,
} from '@nestjs/common';
import { BookService } from './book.service';
import { VisitorService } from '../visitor/visitor.service';
import { CreateBookDto } from './dto/create-book.dto';
import { BorrowBookDto } from './dto/borrow-book.dto';
import { ReturnBookDto } from './dto/return-book.dto';
import { DateUtils } from 'src/utils/date.utils';
import { WorkerService } from 'src/worker/worker.service';
import { UpdateBookDto } from './dto/update-book.dto';
import { buildDownloadFile } from 'src/utils/download.utils';

@Controller('books')
export class BookController {
  constructor(
    private readonly bookService: BookService,
    private readonly visitorService: VisitorService,
    private readonly workerService: WorkerService,
  ) {}

  @Get('all')
  async getAll() {
    return await this.bookService.getAll();
  }

  @Get(':id/download')
  async download(@Param('id') id: string) {
    const book = await this.bookService.getById(id);
    if (!book) throw new NotFoundException(`Book with id ${id} not found`);
    return buildDownloadFile('books', id, book);
  }

  @Post('create')
  async create(@Body() dto: CreateBookDto) {
    return await this.bookService.add(dto);
  }

  @Get(':id')
  async getById(@Param('id') id: string) {
    const book = await this.bookService.getById(id);
    if (!book) throw new NotFoundException(`Book with id ${id} not found`);
    return book;
  }

  @Patch(':id')
  async update(@Param('id') id: string, @Body() dto: UpdateBookDto) {
    const updated = await this.bookService.update(id, dto);
    if (!updated) throw new NotFoundException(`Book with id ${id} not found`);
    return updated;
  }

  @Delete(':id')
  async remove(@Param('id') id: string) {
    const deleted = await this.bookService.delete(id);
    if (!deleted) throw new NotFoundException(`Book with id ${id} not found`);
    return { message: `Book with id ${id} deleted successfully` };
  }

  @Post('borrow')
  async borrow(@Body() dto: BorrowBookDto) {
    const visitor = await this.visitorService.getById(dto.visitorId);
    if (!visitor) throw new NotFoundException(`Visitor not found`);

    const worker = await this.workerService.getById(dto.workerId);
    if (!worker) throw new NotFoundException(`Worker not found`);

    if (!DateUtils.isWorkingDay(dto.borrowDate, worker.workDays)) {
      throw new BadRequestException('Library is closed on this day');
    }

    const borrowedBooks = await this.bookService.borrowBooks(
      dto.bookIds,
      visitor,
    );
    await this.visitorService.addCurrentBooks(visitor.id, borrowedBooks);
    await this.workerService.addIssuedBooks(worker.id, borrowedBooks);

    return { message: `Successfully borrowed ${borrowedBooks.length} book(s)` };
  }

  @Post('return')
  async return(@Body() dto: ReturnBookDto) {
    const visitor = await this.visitorService.getById(dto.visitorId);
    if (!visitor) throw new NotFoundException(`Visitor not found`);

    const worker = await this.workerService.getById(dto.workerId);
    if (!worker) throw new NotFoundException(`Worker not found`);

    if (!DateUtils.isWorkingDay(dto.returnDate, worker.workDays)) {
      throw new BadRequestException('Library is closed on this day');
    }

    const returnedBooks = await this.bookService.returnBooks(
      dto.bookIds,
      visitor,
    );
    await this.visitorService.moveToHistory(visitor.id, returnedBooks);

    return { message: `Successfully returned ${returnedBooks.length} book(s)` };
  }
}