Skip to content

Commit

Permalink
feat: add book model and title, image value object
Browse files Browse the repository at this point in the history
  • Loading branch information
joseandrestrujillo committed Nov 22, 2023
1 parent 5a0c2f9 commit aab517e
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 70 deletions.
76 changes: 7 additions & 69 deletions src/components/BookGrid/BookGrid.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<>
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4 place-items-center">
{books.map((book) => (
{props.books.map((book) => (
<Book
key={book.id}
authors={book.authors}
Expand Down
6 changes: 6 additions & 0 deletions src/core/book/application/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export interface FindBookResponse {
authors: string[]
id: string
image: string
title: string
}
43 changes: 43 additions & 0 deletions src/core/book/domain/model/book.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import Image from '@/core/book/domain/model/image.value-object'
import Tittle from '@/core/book/domain/model/tittle.value-object'

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

static create(
id: string,
authors: string[],
tittle: string,
image: string,
): Book {
const tittleObj = Tittle.create(tittle)
const imageObj = Image.create(image)

return new Book(id, tittleObj, authors, imageObj)
}

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

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

set tittle(tittle: string) {
this._tittle = Tittle.create(tittle)
}

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

get image(): string {
return this._image.value
}
}
17 changes: 17 additions & 0 deletions src/core/book/domain/model/image.value-object.ts
Original file line number Diff line number Diff line change
@@ -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)
}
}
13 changes: 13 additions & 0 deletions src/core/book/domain/model/tittle.value-object.ts
Original file line number Diff line number Diff line change
@@ -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)
}
}
9 changes: 9 additions & 0 deletions src/core/book/domain/services/book.repository.ts
Original file line number Diff line number Diff line change
@@ -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<User | null>

// Saves a user
save(user: User): Promise<void>
}
5 changes: 5 additions & 0 deletions src/core/book/infrastructure/actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { FindBookResponse } from '../application/types'

export async function findBooks(): Promise<FindBookResponse[] | null> {
return null
}
19 changes: 18 additions & 1 deletion stories/components/BookGrid.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,21 @@ const meta = {
export default meta
type Story = StoryObj<typeof meta>

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',
},
],
},
}

0 comments on commit aab517e

Please sign in to comment.