-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
114 changed files
with
1,795 additions
and
673 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,14 @@ | ||
-- CreateEnum | ||
CREATE TYPE "BookState" AS ENUM ('AVAILABLE', 'LOANED'); | ||
|
||
-- CreateTable | ||
CREATE TABLE "Book" ( | ||
"id" TEXT NOT NULL, | ||
"authors" TEXT[], | ||
"image" TEXT NOT NULL, | ||
"title" TEXT NOT NULL, | ||
"version" INTEGER NOT NULL DEFAULT 0, | ||
"state" "BookState" NOT NULL DEFAULT 'AVAILABLE', | ||
|
||
CONSTRAINT "Book_pkey" PRIMARY KEY ("id") | ||
); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
-- CreateTable | ||
CREATE TABLE "Loan" ( | ||
"id" TEXT NOT NULL, | ||
"version" INTEGER NOT NULL DEFAULT 0, | ||
"startsAt" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
"userId" TEXT NOT NULL, | ||
"bookId" TEXT NOT NULL, | ||
|
||
CONSTRAINT "Loan_pkey" PRIMARY KEY ("id") | ||
); | ||
|
||
-- CreateTable | ||
CREATE TABLE "LoanRegistry" ( | ||
"id" TEXT NOT NULL, | ||
"version" INTEGER NOT NULL DEFAULT 0, | ||
"startsAt" TIMESTAMPTZ NOT NULL, | ||
"finishedAt" TIMESTAMPTZ NOT NULL, | ||
"userId" TEXT NOT NULL, | ||
"bookId" TEXT NOT NULL, | ||
|
||
CONSTRAINT "LoanRegistry_pkey" PRIMARY KEY ("id") | ||
); | ||
|
||
-- CreateIndex | ||
CREATE UNIQUE INDEX "Loan_bookId_key" ON "Loan"("bookId"); | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "Loan" ADD CONSTRAINT "Loan_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "Loan" ADD CONSTRAINT "Loan_bookId_fkey" FOREIGN KEY ("bookId") REFERENCES "Book"("id") ON DELETE RESTRICT ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "LoanRegistry" ADD CONSTRAINT "LoanRegistry_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "LoanRegistry" ADD CONSTRAINT "LoanRegistry_bookId_fkey" FOREIGN KEY ("bookId") REFERENCES "Book"("id") ON DELETE RESTRICT ON UPDATE CASCADE; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
53 changes: 53 additions & 0 deletions
53
src/core/book/application/__tests__/create-book.use-case.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { describe, expect, it } from 'vitest' | ||
|
||
import CreateBookUseCase from '@/core/book/application/create-book.use-case' | ||
import BooksInMemory from '@/core/book/infrastructure/services/books-in-memory.repository' | ||
import DuplicateIdError from '@/core/common/domain/errors/application/duplicate-id-error' | ||
import unexpected from '@/lib/utils/unexpected' | ||
import BooksExamples from '@/tests/examples/books.examples' | ||
import bookRequestExamples from '@/tests/examples/books-request.examples' | ||
|
||
describe('CreateBookUseCase', () => { | ||
it('should create a new book', async () => { | ||
// Arrange | ||
const books = new BooksInMemory() | ||
|
||
const command = bookRequestExamples.create() | ||
const useCase = new CreateBookUseCase(books) | ||
|
||
// Act | ||
const result = await useCase.with(command) | ||
|
||
// Assert | ||
result.match( | ||
() => { | ||
const savedBook = books.books.get(command.id) | ||
expect(savedBook?.version).toEqual(0) | ||
}, | ||
(error) => unexpected.error(error), | ||
) | ||
}) | ||
|
||
it('should rejects to create a book with the same id', async () => { | ||
// Arrange | ||
const book = BooksExamples.available() | ||
const books = new BooksInMemory([book]) | ||
|
||
const command = { | ||
...bookRequestExamples.create(), | ||
id: book.id.value, | ||
} | ||
const useCase = new CreateBookUseCase(books) | ||
|
||
// Act | ||
const result = await useCase.with(command) | ||
|
||
// Assert | ||
result.match( | ||
(success) => unexpected.success(success), | ||
(error) => { | ||
expect(error).toBeInstanceOf(DuplicateIdError) | ||
}, | ||
) | ||
}) | ||
}) |
73 changes: 73 additions & 0 deletions
73
src/core/book/application/__tests__/loan-book.use-case.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { describe, expect, it } from 'vitest' | ||
|
||
import { LoanBookUseCase } from '@/core/book/application/loan-book-use.case' | ||
import LoanBookRequest from '@/core/book/dto/requests/loan-book.request' | ||
import BooksInMemory from '@/core/book/infrastructure/services/books-in-memory.repository' | ||
import ApplicationError from '@/core/common/domain/errors/application-error' | ||
import LoanBookService from '@/core/loan/domain/services/loan-book.service' | ||
import LoansInMemory from '@/core/loan/infrastructure/services/loans-in-memory.repository' | ||
import unexpected from '@/lib/utils/unexpected' | ||
import BooksExamples from '@/tests/examples/books.examples' | ||
import LoansExamples from '@/tests/examples/loans.examples' | ||
import UsersExamples from '@/tests/examples/users.examples' | ||
|
||
describe('Loan book', () => { | ||
it('should loan a available book to a user', async () => { | ||
// Arrange | ||
const book = BooksExamples.available() | ||
const books = new BooksInMemory([book]) | ||
|
||
const user = UsersExamples.basic() | ||
|
||
const loans = new LoansInMemory([]) | ||
|
||
const loanBookService = new LoanBookService(loans) | ||
|
||
const useCase = new LoanBookUseCase(books, loanBookService) | ||
const request = LoanBookRequest.with({ | ||
bookId: book.id.value, | ||
userId: user.id.value, | ||
}) | ||
|
||
// Act | ||
const result = useCase.with(request) | ||
|
||
// Assert | ||
await result.match( | ||
() => { | ||
expect(loans.loans).toHaveLength(1) | ||
}, | ||
(error) => unexpected.error(error), | ||
) | ||
}) | ||
|
||
it('should not loan an unavailable book to a user', async () => { | ||
// Arrange | ||
const book = BooksExamples.loaned() | ||
const books = new BooksInMemory([book]) | ||
|
||
const user = UsersExamples.basic() | ||
|
||
const loan = LoansExamples.ofBookAndUser(book, user) | ||
const loans = new LoansInMemory([loan]) | ||
|
||
const loanBookService = new LoanBookService(loans) | ||
|
||
const useCase = new LoanBookUseCase(books, loanBookService) | ||
const request = LoanBookRequest.with({ | ||
bookId: book.id.value, | ||
userId: user.id.value, | ||
}) | ||
|
||
// Act | ||
const result = useCase.with(request) | ||
|
||
// Assert | ||
await result.match( | ||
(_ok) => unexpected.success(_ok), | ||
(_error) => { | ||
expect(_error).instanceof(ApplicationError) | ||
}, | ||
) | ||
}) | ||
}) |
42 changes: 42 additions & 0 deletions
42
src/core/book/application/__tests__/return-book.use-case.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { describe, expect, it } from 'vitest' | ||
|
||
import ReturnBookUseCase from '@/core/book/application/return-book.use-case' | ||
import ReturnBookRequest from '@/core/book/dto/requests/return-book.request' | ||
import BooksInMemory from '@/core/book/infrastructure/services/books-in-memory.repository' | ||
import ReturnBookService from '@/core/loan/domain/services/return-book.service' | ||
import LoansInMemory from '@/core/loan/infrastructure/services/loans-in-memory.repository' | ||
import unexpected from '@/lib/utils/unexpected' | ||
import BooksExamples from '@/tests/examples/books.examples' | ||
import LoansExamples from '@/tests/examples/loans.examples' | ||
import UsersExamples from '@/tests/examples/users.examples' | ||
|
||
describe('Return book', () => { | ||
it('should return a loaned book', async () => { | ||
// Arrange | ||
const book = BooksExamples.loaned() | ||
const books = new BooksInMemory([book]) | ||
|
||
const user = UsersExamples.basic() | ||
|
||
const loan = LoansExamples.ofBookAndUser(book, user) | ||
const loans = new LoansInMemory([loan]) | ||
|
||
const returnBookService = new ReturnBookService(loans) | ||
|
||
const useCase = new ReturnBookUseCase(books, returnBookService) | ||
const request = ReturnBookRequest.with({ | ||
bookId: book.id.value, | ||
}) | ||
|
||
// Act | ||
const result = useCase.with(request) | ||
|
||
// Assert | ||
await result.match( | ||
() => { | ||
expect(loans.loans).toHaveLength(0) | ||
}, | ||
(error) => unexpected.error(error), | ||
) | ||
}) | ||
}) |
Oops, something went wrong.