-
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
14 changed files
with
110 additions
and
71 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import Book from '../domain/model/book.entity' | ||
import BookId from '../domain/model/id.value-object' | ||
import Books from '../domain/services/books.repository' | ||
import { CreateBookCommand } from './types' | ||
|
||
export default class CreateBookUseCase { | ||
constructor(private readonly bookRepository: Books) {} | ||
|
||
async with(command: CreateBookCommand): Promise<void> { | ||
if ( | ||
!(this.bookRepository.findById(BookId.create(command.id)) instanceof Book) | ||
) { | ||
throw Error('This book already exists') | ||
} | ||
const book: Book = Book.create( | ||
command.id, | ||
command.authors, | ||
command.title, | ||
command.image, | ||
) | ||
this.bookRepository.save(book) | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
export class BookAuthor { | ||
constructor(public readonly value: string) {} | ||
|
||
public static create(author: string): BookAuthor { | ||
if (author === undefined) { | ||
throw Error('Author can not be undefined') | ||
} | ||
if (author.length < 3) { | ||
throw Error('Author is too short') | ||
} | ||
|
||
return new BookAuthor(author) | ||
} | ||
} |
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
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
2 changes: 1 addition & 1 deletion
2
src/core/book/domain/model/book_title.ts → ...e/book/domain/model/title.value-object.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
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,4 +1,10 @@ | ||
import Book from '@/core/book/domain/model/book.entity' | ||
|
||
import BookId from '../model/id.value-object' | ||
|
||
export default interface Books { | ||
// Saves a user | ||
//save(book: Book): Promise<void> | ||
// Finds a user by email | ||
findAll(): Promise<Book[]> | ||
findById(id: BookId): Promise<Book | null> | ||
save(book: Book): Promise<void> | ||
} |
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 |
---|---|---|
@@ -1,7 +1,19 @@ | ||
import container from '@/lib/container' | ||
|
||
import { CreateBookCommand } from '../application/types' | ||
import BookId from '../domain/model/id.value-object' | ||
import { FindBookResponse } from '../application/types' | ||
|
||
export async function findBooks(): Promise<FindBookResponse[]> { | ||
return container.findBooks.with() | ||
} | ||
|
||
export async function createBook( | ||
title: string, | ||
authors: string[], | ||
image: string, | ||
): Promise<void> { | ||
const id = BookId.generate() | ||
await container.createBook.with( | ||
new CreateBookCommand(id.value, title, authors, image), | ||
) | ||
} |
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