From aab517e667f7de13ab3e154806cef507310f7dca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Andr=C3=A9s=20Trujillo?= Date: Wed, 22 Nov 2023 15:47:17 +0100 Subject: [PATCH] feat: add book model and title, image value object --- src/components/BookGrid/BookGrid.tsx | 76 ++----------------- src/core/book/application/types.ts | 6 ++ src/core/book/domain/model/book.entity.ts | 43 +++++++++++ .../book/domain/model/image.value-object.ts | 17 +++++ .../book/domain/model/tittle.value-object.ts | 13 ++++ .../book/domain/services/book.repository.ts | 9 +++ src/core/book/infrastructure/actions.ts | 5 ++ stories/components/BookGrid.stories.ts | 19 ++++- 8 files changed, 118 insertions(+), 70 deletions(-) create mode 100644 src/core/book/application/types.ts create mode 100644 src/core/book/domain/model/book.entity.ts create mode 100644 src/core/book/domain/model/image.value-object.ts create mode 100644 src/core/book/domain/model/tittle.value-object.ts create mode 100644 src/core/book/domain/services/book.repository.ts create mode 100644 src/core/book/infrastructure/actions.ts diff --git a/src/components/BookGrid/BookGrid.tsx b/src/components/BookGrid/BookGrid.tsx index 53b2f49..dfef8b1 100644 --- a/src/components/BookGrid/BookGrid.tsx +++ b/src/components/BookGrid/BookGrid.tsx @@ -1,78 +1,16 @@ +import { FindBookResponse } from '@/core/book/application/types' + import Book from '../Book/Book' -const books = [ - { - authors: ['Donald Knuth'], - id: '1', - image: 'https://m.media-amazon.com/images/I/91YfNb49PLL._SL1500_.jpg', - title: 'The Art of Computer Programming', - }, - { - authors: ['Erich Gamma', 'Richard Helm', 'Ralph Johnson', 'John Vlissides'], - id: '2', - image: 'https://m.media-amazon.com/images/I/91YfNb49PLL._SL1500_.jpg', - title: 'Design Patterns: Elements of Reusable Object-Oriented Software', - }, - { - authors: ['Martin Fowler'], - id: '3', - image: 'https://m.media-amazon.com/images/I/91YfNb49PLL._SL1500_.jpg', - title: 'Refactoring: Improving the Design of Existing Code', - }, - { - authors: ['Andrew S. Tanenbaum'], - id: '4', - image: 'https://m.media-amazon.com/images/I/91YfNb49PLL._SL1500_.jpg', - title: 'Computer Networks', - }, - { - authors: ['Robert C. Martin'], - id: '5', - image: 'https://m.media-amazon.com/images/I/91YfNb49PLL._SL1500_.jpg', - title: 'Clean Code: A Handbook of Agile Software Craftsmanship', - }, - { - authors: ['Bjarne Stroustrup'], - id: '6', - image: 'https://m.media-amazon.com/images/I/91YfNb49PLL._SL1500_.jpg', - title: 'The C++ Programming Language', - }, - { - authors: ['Brian W. Kernighan', 'Dennis M. Ritchie'], - id: '7', - image: 'https://m.media-amazon.com/images/I/91YfNb49PLL._SL1500_.jpg', - title: 'The C Programming Language', - }, - { - authors: ['Eric S. Raymond'], - id: '8', - image: 'https://m.media-amazon.com/images/I/91YfNb49PLL._SL1500_.jpg', - title: 'The Cathedral and the Bazaar', - }, - { - authors: [ - 'Alfred V. Aho', - 'Monica S. Lam', - 'Ravi Sethi', - 'Jeffrey D. Ullman', - ], - id: '9', - image: 'https://m.media-amazon.com/images/I/91YfNb49PLL._SL1500_.jpg', - title: 'Compilers: Principles, Techniques, and Tools', - }, - { - authors: ['Douglas C. Schmidt'], - id: '10', - image: 'https://m.media-amazon.com/images/I/91YfNb49PLL._SL1500_.jpg', - title: 'Pattern-Oriented Software Architecture', - }, -] +export interface BookGridProps { + books: FindBookResponse[] +} -export default function BookGrid() { +export default function BookGrid(props: BookGridProps) { return ( <>
- {books.map((book) => ( + {props.books.map((book) => ( author) + } + + get image(): string { + return this._image.value + } +} diff --git a/src/core/book/domain/model/image.value-object.ts b/src/core/book/domain/model/image.value-object.ts new file mode 100644 index 0000000..68303c6 --- /dev/null +++ b/src/core/book/domain/model/image.value-object.ts @@ -0,0 +1,17 @@ +export default class Image { + constructor(public readonly value: string) {} + + static create(name: string): Image { + if (!name.trim()) { + return new Image('') + } + + try { + new URL(name) + } catch (error) { + throw new Error('Invalid URL') + } + + return new Image(name) + } +} diff --git a/src/core/book/domain/model/tittle.value-object.ts b/src/core/book/domain/model/tittle.value-object.ts new file mode 100644 index 0000000..4e86967 --- /dev/null +++ b/src/core/book/domain/model/tittle.value-object.ts @@ -0,0 +1,13 @@ +export default class Name { + constructor(public readonly value: string) {} + + static create(name: string): Name { + const trimmedName = name.trim() + + if (!trimmedName) { + throw new Error('Name cannot be empty') + } + + return new Name(trimmedName) + } +} diff --git a/src/core/book/domain/services/book.repository.ts b/src/core/book/domain/services/book.repository.ts new file mode 100644 index 0000000..36eacd3 --- /dev/null +++ b/src/core/book/domain/services/book.repository.ts @@ -0,0 +1,9 @@ +import User from '@/core/user/domain/model/user.entity' + +export default interface Users { + // Finds a user by email + findByEmail(email: string): Promise + + // Saves a user + save(user: User): Promise +} diff --git a/src/core/book/infrastructure/actions.ts b/src/core/book/infrastructure/actions.ts new file mode 100644 index 0000000..df85908 --- /dev/null +++ b/src/core/book/infrastructure/actions.ts @@ -0,0 +1,5 @@ +import { FindBookResponse } from '../application/types' + +export async function findBooks(): Promise { + return null +} diff --git a/stories/components/BookGrid.stories.ts b/stories/components/BookGrid.stories.ts index e164c67..3bde983 100644 --- a/stories/components/BookGrid.stories.ts +++ b/stories/components/BookGrid.stories.ts @@ -10,4 +10,21 @@ const meta = { export default meta type Story = StoryObj -export const Basic: Story = {} +export const Basic: Story = { + args: { + books: [ + { + authors: ['Martin Fowler'], + id: '3', + image: 'https://m.media-amazon.com/images/I/91YfNb49PLL._SL1500_.jpg', + title: 'Refactoring: Improving the Design of Existing Code', + }, + { + authors: ['Andrew S. Tanenbaum', 'Andrew S. Tanenbaum'], + id: '4', + image: 'https://m.media-amazon.com/images/I/91YfNb49PLL._SL1500_.jpg', + title: 'Computer Networks', + }, + ], + }, +}