-
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
13 changed files
with
245 additions
and
1 deletion.
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,13 @@ | ||
import { DomainError } from '@/core/common/domain/errors/domain-error' | ||
|
||
export class Description { | ||
constructor(public readonly value: string) {} | ||
|
||
public static create(author: string): Description { | ||
if (!author || author.length < 3) { | ||
throw DomainError.cause('La descripción es demasiado corta') | ||
} | ||
|
||
return new Description(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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { Id } from '@/core/common/domain/value-objects/id' | ||
|
||
export class ReviewId extends Id {} |
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,17 @@ | ||
import { DomainError } from '@/core/common/domain/errors/domain-error' | ||
|
||
export class Score { | ||
constructor(public readonly value: number) {} | ||
|
||
public static create(value: number): Score { | ||
if (!Number.isInteger(value)) { | ||
throw DomainError.cause('La puntuación debe ser un entero') | ||
} | ||
|
||
if (value < 1 || value > 5) { | ||
throw DomainError.cause('La puntuación debe ser un entero entre 1 y 5') | ||
} | ||
|
||
return new Score(value) | ||
} | ||
} |
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,12 @@ | ||
import { ReviewFactory } from '@/core/review/domain/model/review.factory' | ||
import { Reviews } from '@/core/review/domain/services/reviews.repository' | ||
import { CreateReviewRequest } from '@/core/review/dto/requests/create-review.request' | ||
|
||
export class CreateReviewUseCase { | ||
constructor(private readonly reviews: Reviews) {} | ||
|
||
async with(command: CreateReviewRequest) { | ||
const review = ReviewFactory.create(command) | ||
return this.reviews.save(review) | ||
} | ||
} |
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,44 @@ | ||
import { AggregateRoot } from '@/core/common/domain/model/aggregate-root' | ||
import { BookId } from '@/core/common/domain/value-objects/book-id' | ||
import { Description } from '@/core/common/domain/value-objects/description' | ||
import { ReviewId } from '@/core/common/domain/value-objects/review-id' | ||
import { Score } from '@/core/common/domain/value-objects/score' | ||
import { Title } from '@/core/common/domain/value-objects/title' | ||
import { UserId } from '@/core/common/domain/value-objects/user-id' | ||
|
||
export class Review extends AggregateRoot { | ||
constructor( | ||
protected _id: ReviewId, | ||
protected _bookId: BookId, | ||
protected _userId: UserId, | ||
protected _title: Title, | ||
protected _description: Description, | ||
protected _score: Score, | ||
) { | ||
super() | ||
} | ||
|
||
get id(): ReviewId { | ||
return this._id | ||
} | ||
|
||
get bookId(): BookId { | ||
return this._bookId | ||
} | ||
|
||
get userId(): UserId { | ||
return this._userId | ||
} | ||
|
||
get title(): Title { | ||
return this._title | ||
} | ||
|
||
get description(): Description { | ||
return this._description | ||
} | ||
|
||
get score(): Score { | ||
return this._score | ||
} | ||
} |
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,21 @@ | ||
import { BookId } from '@/core/common/domain/value-objects/book-id' | ||
import { Description } from '@/core/common/domain/value-objects/description' | ||
import { ReviewId } from '@/core/common/domain/value-objects/review-id' | ||
import { Score } from '@/core/common/domain/value-objects/score' | ||
import { Title } from '@/core/common/domain/value-objects/title' | ||
import { UserId } from '@/core/common/domain/value-objects/user-id' | ||
import { Review } from '@/core/review/domain/model/review.entity' | ||
import { CreateReviewRequest } from '@/core/review/dto/requests/create-review.request' | ||
|
||
export const ReviewFactory = { | ||
create: (reviewResponse: CreateReviewRequest): Review => { | ||
const reviewId = ReviewId.create(reviewResponse.id) | ||
const bookId = BookId.create(reviewResponse.bookId) | ||
const userId = UserId.create(reviewResponse.userId) | ||
const title = Title.create(reviewResponse.title) | ||
const description = Description.create(reviewResponse.description) | ||
const score = Score.create(reviewResponse.score) | ||
|
||
return new Review(reviewId, bookId, userId, title, description, score) | ||
}, | ||
} |
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,5 @@ | ||
import { Review } from '@/core/review/domain/model/review.entity' | ||
|
||
export interface Reviews { | ||
save(review: Review): Promise<void> | ||
} |
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 @@ | ||
type CreateReviewRequest = { | ||
bookId: string | ||
description: string | ||
id: string | ||
score: number | ||
title: string | ||
userId: string | ||
} | ||
|
||
const CreateReviewRequest = { | ||
with: (properties: CreateReviewRequest): CreateReviewRequest => properties, | ||
} | ||
|
||
export { CreateReviewRequest } |
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
14 changes: 14 additions & 0 deletions
14
src/core/review/infrastructure/persistence/review.data-mapper.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Review } from '@/core/review/domain/model/review.entity' | ||
|
||
const ReviewDataMapper = { | ||
toPrisma: (review: Review) => ({ | ||
bookId: review.bookId.value, | ||
description: review.description.value, | ||
id: review.id.value, | ||
score: review.score.value, | ||
title: review.title.value, | ||
userId: review.userId.value, | ||
}), | ||
} | ||
|
||
export { ReviewDataMapper } |
38 changes: 38 additions & 0 deletions
38
src/core/review/infrastructure/persistence/review.publisher.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { PrismaClient } from '@prisma/client' | ||
|
||
import { ApplicationError } from '@/core/common/domain/errors/application-error' | ||
import { Publisher } from '@/core/common/domain/publisher/publisher' | ||
import { Review } from '@/core/review/domain/model/review.entity' | ||
import { ReviewDataMapper } from '@/core/review/infrastructure/persistence/review.data-mapper' | ||
|
||
export class ReviewPublisher extends Publisher<Review> { | ||
constructor(private readonly prisma: PrismaClient) { | ||
super() | ||
} | ||
|
||
async create(review: Review): Promise<void> { | ||
const data = ReviewDataMapper.toPrisma(review) | ||
|
||
try { | ||
await this.prisma.review.create({ data }) | ||
} catch (error) { | ||
throw new ApplicationError((error as Error).toString()) | ||
} | ||
} | ||
|
||
protected async update(review: Review, version: number): Promise<void> { | ||
const { id, ...data } = ReviewDataMapper.toPrisma(review) | ||
|
||
try { | ||
await this.prisma.review.update({ | ||
data, | ||
where: { | ||
id, | ||
version, | ||
}, | ||
}) | ||
} catch (error) { | ||
throw new ApplicationError((error as Error).toString()) | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/core/review/infrastructure/services/reviews-prisma.repository.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { PrismaClient } from '@prisma/client' | ||
|
||
import { Review } from '@/core/review/domain/model/review.entity' | ||
import { Reviews } from '@/core/review/domain/services/reviews.repository' | ||
import { ReviewPublisher } from '@/core/review/infrastructure/persistence/review.publisher' | ||
|
||
export class ReviewsPrisma implements Reviews { | ||
private publisher: ReviewPublisher | ||
|
||
constructor(private readonly prisma: PrismaClient) { | ||
this.publisher = new ReviewPublisher(prisma) | ||
} | ||
|
||
async save(review: Review): Promise<void> { | ||
return this.publisher.mergeObjectContext(review).commit() | ||
} | ||
} |
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