diff --git a/LICENSE b/LICENSE index 566d1fb..eb98e8d 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,5 @@ MIT License + Copyright (c) 2025 EPS/MDS Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -6,12 +7,15 @@ in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. \ No newline at end of file +SOFTWARE. + diff --git a/listenv.txt b/listenv.txt new file mode 100644 index 0000000..e537c70 --- /dev/null +++ b/listenv.txt @@ -0,0 +1,10 @@ +DB_PORT=5432 +DB_HOST=localhost +DB_USER=afonso +DB_PASSWORD=afonso +DB_NAME=test +ENVIRONMENT=dev +MAIL_HOST= +MAIL_USERNAME= +MAIL_PASSWORD= +APP_URL=http://localhost:3000 diff --git a/src/app.module.ts b/src/app.module.ts index c0f9ca1..10efb6a 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -4,6 +4,7 @@ import { UsersModule } from './users/users.module'; import { ConfigModule, ConfigService } from '@nestjs/config'; import { AuthModule } from './auth/auth.module'; import typeormConfig from './database/config'; +import { BooksModule } from './books/books.module'; @Module({ imports: [ @@ -18,6 +19,7 @@ import typeormConfig from './database/config'; }), UsersModule, AuthModule, + BooksModule, ], controllers: [], providers: [], diff --git a/src/books/books.controller.ts b/src/books/books.controller.ts new file mode 100644 index 0000000..f058bb6 --- /dev/null +++ b/src/books/books.controller.ts @@ -0,0 +1,32 @@ +import { Controller, Get, Param, Query, Put, Body } from '@nestjs/common'; +import { BooksService } from './books.service'; +import { SearchBooksDto } from './dtos/searchBooks.dto'; +import { BorrowBooksDto } from './dtos/borrowBooks.dto'; +import { UpdateBookStatusDto } from './dtos/updateBookStatus.dto'; // Certifique-se de importar esse DTO + +@Controller('books') +export class BooksController { + constructor(private readonly booksService: BooksService) {} + + @Get('search') + async searchBooks(@Query() searchParams: SearchBooksDto) { + return this.booksService.searchBooks(searchParams); + } + + @Get(':id') + async getBookById(@Param('id') id: string): Promise { + try { + return await this.booksService.getBookById(id); + } catch (error) { + throw error; + } + } + + @Put(':id/status') + async updateBookStatus( + @Param('id') id: string, + @Body() updateBookStatusDto: UpdateBookStatusDto, + ) { + return this.booksService.updateBookStatus(id, updateBookStatusDto); + } +} diff --git a/src/books/books.mock.ts b/src/books/books.mock.ts new file mode 100644 index 0000000..9c325a3 --- /dev/null +++ b/src/books/books.mock.ts @@ -0,0 +1,24 @@ +export const booksMock = [ + { + id: 1, + title: 'Curupira, O guardião da floresta', + author: 'Saci-Pererê', + rating: 4.5, + description: 'Descrição do livro', + coverImage: '/curupira.jpeg', + status: 'Available', + userId: '', + date: '', + }, + { + id: 2, + title: 'O boto cor de rosa', + author: 'Mula-sem-cabeça', + rating: 2.5, + description: 'Descrição do livro', + coverImage: '/boto.jpeg', + status: 'Available', + userId: '', + date: '', + }, +]; diff --git a/src/books/books.module.ts b/src/books/books.module.ts new file mode 100644 index 0000000..09b0114 --- /dev/null +++ b/src/books/books.module.ts @@ -0,0 +1,12 @@ +import { Module } from '@nestjs/common'; +import { BooksController } from './books.controller'; +import { BooksService } from './books.service'; +import { TypeOrmModule } from '@nestjs/typeorm'; +import { Book } from '../database/entities/book.entity'; + +@Module({ + imports: [TypeOrmModule.forFeature([Book])], + controllers: [BooksController], + providers: [BooksService], +}) +export class BooksModule {} diff --git a/src/books/books.service.spec.ts b/src/books/books.service.spec.ts new file mode 100644 index 0000000..adaa724 --- /dev/null +++ b/src/books/books.service.spec.ts @@ -0,0 +1,46 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { BooksService } from './books.service'; +import { NotFoundException } from '@nestjs/common'; + +describe('BooksService', () => { + let booksService: BooksService; + + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + providers: [BooksService], + }).compile(); + + booksService = module.get(BooksService); + }); + + it('deve buscar um livro por ID existente', async () => { + const result = await booksService.getBookById('1'); + expect(result).toBeDefined(); + expect(result.id).toBe(1); + }); + + it('deve lançar erro ao buscar um livro por ID inexistente', async () => { + await expect(booksService.getBookById('999')).rejects.toThrow( + NotFoundException, + ); + }); + + it('deve atualizar o status de um livro existente', async () => { + const updatedBook = await booksService.updateBookStatus('1', { + status: 'NotAvailable', + userId: '', + date: '', + }); + expect(updatedBook.status).toBe('NotAvailable'); + }); + + it('deve lançar erro ao tentar atualizar o status de um livro inexistente', async () => { + await expect( + booksService.updateBookStatus('999', { + status: 'NotAvailable', + userId: '', + date: '', + }), + ).rejects.toThrow(NotFoundException); + }); +}); diff --git a/src/books/books.service.ts b/src/books/books.service.ts new file mode 100644 index 0000000..00a4cdb --- /dev/null +++ b/src/books/books.service.ts @@ -0,0 +1,48 @@ +import { Injectable, NotFoundException } from '@nestjs/common'; +import { BorrowBooksDto } from './dtos/borrowBooks.dto'; +import { SearchBooksDto } from './dtos/searchBooks.dto'; +import { UpdateBookStatusDto } from './dtos/updateBookStatus.dto'; +import { booksMock } from './books.mock'; + +@Injectable() +export class BooksService { + private books = booksMock; + + async searchBooks(searchParams: SearchBooksDto) { + return this.books.filter( + (book) => + (searchParams.title ? book.title.includes(searchParams.title) : true) && + (searchParams.author + ? book.author.includes(searchParams.author) + : true), + ); + } + + async getBookById(id: string): Promise { + const book = this.books.find((book) => book.id === Number(id)); + if (!book) { + throw new NotFoundException('Livro não encontrado'); + } + return book; + } + + async updateBookStatus( + id: string, + updateBookStatusDto: UpdateBookStatusDto, + ): Promise { + const bookIndex = this.books.findIndex((book) => book.id === Number(id)); + + if (bookIndex === -1) { + throw new NotFoundException('Livro não encontrado'); + } + + this.books[bookIndex] = { + ...this.books[bookIndex], + status: updateBookStatusDto.status, + userId: updateBookStatusDto.userId, + date: new Date().toISOString(), + }; + + return this.books[bookIndex]; + } +} diff --git a/src/books/dtos/borrowBooks.dto.ts b/src/books/dtos/borrowBooks.dto.ts new file mode 100644 index 0000000..aefdaf7 --- /dev/null +++ b/src/books/dtos/borrowBooks.dto.ts @@ -0,0 +1,9 @@ +export class BorrowBooksDto { + id: number; + title: string; + author: string; + rating: number; + description: string; + coverImage: string; + status: string; +} diff --git a/src/books/dtos/searchBooks.dto.ts b/src/books/dtos/searchBooks.dto.ts new file mode 100644 index 0000000..cb41129 --- /dev/null +++ b/src/books/dtos/searchBooks.dto.ts @@ -0,0 +1,25 @@ +import { IsOptional, IsString, IsInt, Min } from 'class-validator'; + +export class SearchBooksDto { + @IsOptional() + @IsString() + title?: string; + + @IsOptional() + @IsString() + author?: string; + + @IsOptional() + @IsString() + theme?: string; + + @IsOptional() + @IsInt() + @Min(1) + page: number = 1; + + @IsOptional() + @IsInt() + @Min(1) + limit: number = 20; +} diff --git a/src/books/dtos/updateBookStatus.dto.ts b/src/books/dtos/updateBookStatus.dto.ts new file mode 100644 index 0000000..6c9939c --- /dev/null +++ b/src/books/dtos/updateBookStatus.dto.ts @@ -0,0 +1,5 @@ +export class UpdateBookStatusDto { + status: string; + userId: string; + date: string; +} diff --git a/src/database/entities/book.entity.ts b/src/database/entities/book.entity.ts new file mode 100644 index 0000000..95b2573 --- /dev/null +++ b/src/database/entities/book.entity.ts @@ -0,0 +1,43 @@ +import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm'; + +export enum Status { + AVAILABLE = 'available', + NOTAVAILABLE = 'notAvailable', +} + +@Entity() +export class Book { + @PrimaryGeneratedColumn('uuid') + id: string; + + @Column() + title: string; + + @Column() + coverImage: string; + + @Column() + author: string; + + @Column() + theme: string; + + @Column() + description: string; + + @Column({ type: 'decimal', precision: 2, scale: 1, default: 0 }) + averageRating: number; // Média de avaliação dos usuários (0.0 a 5.0) + + @Column({ nullable: true }) + coverUrl: string; // URL da capa do livro + + @Column({ nullable: true }) + borrowedBy: string; + + @Column({ + type: 'enum', + enum: Status, + default: Status.AVAILABLE, + }) + role: Status; +} diff --git a/src/main.ts b/src/main.ts index 3207518..f07638b 100644 --- a/src/main.ts +++ b/src/main.ts @@ -6,6 +6,6 @@ async function bootstrap() { const app = await NestFactory.create(AppModule); app.useGlobalPipes(new ValidationPipe()); app.enableCors(); - await app.listen(process.env.PORT || 3000); + await app.listen(process.env.PORT || 3001); } bootstrap();