-
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.
refactor: create dtos for requests and responses
- Loading branch information
Showing
38 changed files
with
334 additions
and
305 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,23 @@ | ||
import { errAsync } from 'neverthrow' | ||
|
||
import BookIdAlreadyExistsError from '@/core/book/domain/errors/book-id-already-exists.error' | ||
import Book from '@/core/book/domain/model/book.entity' | ||
import BookFactory from '@/core/book/domain/model/book.factory' | ||
import Books from '@/core/book/domain/services/books.repository' | ||
import CreateBookRequest from '@/core/book/dto/requests/create-book.request' | ||
import BookId from '@/core/common/domain/value-objects/book-id' | ||
|
||
import { CreateBookCommand } from './types' | ||
|
||
export default class CreateBookUseCase { | ||
constructor(private readonly books: Books) {} | ||
|
||
async with(command: CreateBookCommand) { | ||
async with(command: CreateBookRequest) { | ||
return await BookId.create(command.id) | ||
.asyncAndThen((bookId) => this.books.findById(bookId)) | ||
.match( | ||
(book) => errAsync(BookIdAlreadyExistsError.withId(book.id)), | ||
() => | ||
Book.create(command).asyncAndThen((_book) => this.books.save(_book)), | ||
BookFactory.create(command).asyncAndThen((_book) => | ||
this.books.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 |
---|---|---|
@@ -1,15 +1,15 @@ | ||
import { okAsync, ResultAsync } from 'neverthrow' | ||
|
||
import { BookDTO } from '@/core/book/application/types' | ||
import Books from '@/core/book/domain/services/books.repository' | ||
import BookResponse from '@/core/book/dto/responses/book.response' | ||
import ApplicationError from '@/core/common/domain/errors/application-error' | ||
|
||
export default class FindBooksUseCase { | ||
constructor(private readonly books: Books) {} | ||
|
||
with(): ResultAsync<BookDTO[], ApplicationError> { | ||
with(): ResultAsync<BookResponse[], ApplicationError> { | ||
return this.books.findAll().andThen((books) => { | ||
return okAsync(books.map((book) => BookDTO.fromModel(book))) | ||
return okAsync(books.map((book) => BookResponse.fromModel(book))) | ||
}) | ||
} | ||
} |
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,8 @@ | ||
import FullNameError from '@/core/common/domain/value-objects/fullname/fullname.error' | ||
import IdError from '@/core/common/domain/value-objects/id/id.error' | ||
import ImageError from '@/core/common/domain/value-objects/image/image.error' | ||
import TitleError from '@/core/common/domain/value-objects/title/title.error' | ||
|
||
type BookDomainError = IdError | TitleError | FullNameError | ImageError | ||
|
||
export default BookDomainError |
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,39 @@ | ||
import { ok, Result, safeTry } from 'neverthrow' | ||
|
||
import BookDomainError from '@/core/book/domain/errors/book-domain.error' | ||
import Book from '@/core/book/domain/model/book.entity' | ||
import BookResponse from '@/core/book/dto/responses/book.response' | ||
import BookId from '@/core/common/domain/value-objects/book-id' | ||
import FullName from '@/core/common/domain/value-objects/fullname' | ||
import FullNames from '@/core/common/domain/value-objects/fullnames' | ||
import Image from '@/core/common/domain/value-objects/image' | ||
import Title from '@/core/common/domain/value-objects/title' | ||
|
||
const BookFactory = { | ||
create: (bookResponse: BookResponse): Result<Book, BookDomainError> => | ||
safeTry<Book, BookDomainError>(function* () { | ||
const bookId = yield* BookId.create(bookResponse.id) | ||
.mapErr((error) => error) | ||
.safeUnwrap() | ||
const title = yield* Title.create(bookResponse.title) | ||
.mapErr((error) => error) | ||
.safeUnwrap() | ||
const authors = yield* FullNames.create(bookResponse.authors) | ||
.mapErr((error) => error) | ||
.safeUnwrap() | ||
const image = yield* Image.create(bookResponse.image) | ||
.mapErr((error) => error) | ||
.safeUnwrap() | ||
|
||
return ok(new Book(bookId, title, authors, image)) | ||
}), | ||
with: (bookResponse: BookResponse): Book => | ||
new Book( | ||
new BookId(bookResponse.id), | ||
new Title(bookResponse.title), | ||
new FullNames(bookResponse.authors.map((author) => new FullName(author))), | ||
new Image(bookResponse.image), | ||
), | ||
} | ||
|
||
export default BookFactory |
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 @@ | ||
import { DeepReadonly } from 'ts-essentials' | ||
|
||
type CreateBookRequest = DeepReadonly<{ | ||
authors: string[] | ||
id: string | ||
image: string | ||
title: string | ||
}> | ||
|
||
const CreateBookRequest = { | ||
with: (properties: CreateBookRequest) => properties, | ||
} | ||
|
||
export default CreateBookRequest |
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,22 @@ | ||
import { DeepReadonly } from 'ts-essentials' | ||
|
||
import Book from '@/core/book/domain/model/book.entity' | ||
|
||
type BookResponse = DeepReadonly<{ | ||
authors: string[] | ||
id: string | ||
image: string | ||
title: string | ||
}> | ||
|
||
const BookResponse = { | ||
fromModel: (book: Book): BookResponse => ({ | ||
authors: book.authors.map((author) => author.value), | ||
id: book.id.value, | ||
image: book.image.value, | ||
title: book.title.value, | ||
}), | ||
with: (properties: BookResponse) => properties, | ||
} | ||
|
||
export default BookResponse |
Oops, something went wrong.