Skip to content

Commit

Permalink
feat: createBook feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Salsedini committed Nov 22, 2023
1 parent 0dee8b4 commit a0020b3
Show file tree
Hide file tree
Showing 14 changed files with 110 additions and 71 deletions.
23 changes: 23 additions & 0 deletions src/core/book/application/create-book.use-case.ts
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)
}
}
4 changes: 2 additions & 2 deletions src/core/book/application/find-books.use-case.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Books from '@/core/book/domain/services/book.repository'
import Books from '@/core/book/domain/services/books.repository'

import { FindBookResponse } from './types'

Expand All @@ -12,7 +12,7 @@ export default class FindBooksUseCase {
authors: book.authors,
id: book.id,
image: book.image,
title: book.tittle,
title: book.title,
}))
}
}
14 changes: 14 additions & 0 deletions src/core/book/domain/model/author.value-object.ts
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)
}
}
37 changes: 19 additions & 18 deletions src/core/book/domain/model/book.entity.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,41 @@
import Image from '@/core/book/domain/model/image.value-object'
import Tittle from '@/core/book/domain/model/tittle.value-object'
import BookImage from '@/core/book/domain/model/image.value-object'

import { BookAuthor } from './author.value-object'
import BookId from './id.value-object'
import { BookTitle } from './title.value-object'

export default class Book {
constructor(
private _id: string,
private _tittle: Tittle,
private _authors: string[],
private _image: Image,
private _id: BookId,
private _title: BookTitle,
private _authors: BookAuthor[],
private _image: BookImage,
) {}

static create(
id: string,
authors: string[],
tittle: string,
title: string,
image: string,
): Book {
const tittleObj = Tittle.create(tittle)
const imageObj = Image.create(image)
const idObj = BookId.create(id)
const titleObj = BookTitle.create(title)
const imageObj = BookImage.create(image)
const authorObj = authors.map((item) => BookAuthor.create(item))

return new Book(id, tittleObj, authors, imageObj)
return new Book(idObj, titleObj, authorObj, imageObj)
}

get id(): string {
return this._id
}

get tittle(): string {
return this._tittle.value
return this._id.value
}

set tittle(tittle: string) {
this._tittle = Tittle.create(tittle)
get title(): string {
return this._title.value
}

get authors(): string[] {
return this._authors.map((author) => author)
return this._authors.map((author) => author.value)
}

get image(): string {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default class BookId {
return new BookId(uuid())
}

public static with(id: string): BookId {
public static create(id: string): BookId {
return new BookId(id)
}
}
8 changes: 4 additions & 4 deletions src/core/book/domain/model/image.value-object.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
export default class Image {
export default class BookImage {
constructor(public readonly value: string) {}

static create(name: string): Image {
static create(name: string): BookImage {
if (!name.trim()) {
return new Image('')
return new BookImage('')
}

try {
Expand All @@ -12,6 +12,6 @@ export default class Image {
throw new Error('Invalid URL')
}

return new Image(name)
return new BookImage(name)
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export class BookTitle {
constructor(public readonly value: string) {}

public static with(title: string): BookTitle {
public static create(title: string): BookTitle {
if (title === undefined) {
throw Error('Title can not be undefined')
}
Expand Down
13 changes: 0 additions & 13 deletions src/core/book/domain/model/tittle.value-object.ts

This file was deleted.

9 changes: 0 additions & 9 deletions src/core/book/domain/services/book.repository.ts

This file was deleted.

10 changes: 8 additions & 2 deletions src/core/book/domain/services/books.repository.ts
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>
}
13 changes: 0 additions & 13 deletions src/core/book/infraestructure/actions.ts

This file was deleted.

14 changes: 13 additions & 1 deletion src/core/book/infrastructure/actions.ts
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),
)
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import Book from '../../domain/model/book.entity'
import Books from '../../domain/services/book.repository'
import BookId from '../../domain/model/id.value-object'
import Books from '../../domain/services/books.repository'

export default class BooksInMemory implements Books {
async findAll(): Promise<Book[]> {
private books: Map<string, Book> = new Map();

constructor(){
const booksRaw = [
{
authors: ['Donald Knuth'],
Expand Down Expand Up @@ -76,11 +79,24 @@ export default class BooksInMemory implements Books {
},
]

return await booksRaw.map((book) =>
Book.create(book.id, book.authors, book.title, book.image),
)
booksRaw.map((book) =>
this.books.set(book.id, Book.create(book.id, book.authors, book.title, book.image))
)}

async findById(id: BookId): Promise<Book | null> {
const book = this.books.get(id.value);
return (book ) ? book : null;
}
save(book: Book): Promise<void> {
throw new Error('Method not implemented.' + book.tittle)

async findAll(): Promise<Book[]> {
return Array.from(this.books.values())
}

async save(book: Book): Promise<void> {
this.books.set(book.id, book)
}

purge(): void {
this.books = new Map()
}
}
2 changes: 2 additions & 0 deletions src/lib/container/container.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import CreateBookUseCase from '@/core/book/application/create-book.use-case'
import FindBooksUseCase from '@/core/book/application/find-books.use-case'
import BooksInMemory from '@/core/book/infrastructure/services/books-in-memory.retository'
import FindUserUseCase from '@/core/user/application/find-user.use-case'
Expand All @@ -11,6 +12,7 @@ const Container = {
const books = new BooksInMemory()
return {
findBooks: new FindBooksUseCase(books),
createBook: new CreateBookUseCase(books),
findUser: new FindUserUseCase(users),
updateUser: new UpdateUserUseCase(users),
}
Expand Down

0 comments on commit a0020b3

Please sign in to comment.