|
1 | 1 | import { |
2 | | - Entity, |
3 | 2 | Guard, |
4 | | - IdValueObject, |
| 3 | + createEntityFactory, |
| 4 | + createIdValueObject, |
5 | 5 | success, |
6 | 6 | } from "@clean-architecture/shared-kernel"; |
7 | | -import type { GetValueFromValueObject } from "@clean-architecture/shared-kernel"; |
| 7 | +import type { Entity } from "@clean-architecture/shared-kernel"; |
8 | 8 |
|
9 | | -import { CreatedAtValueObject } from "../../shared/entities/CreatedAtValueObject"; |
10 | | -import { AuthorValueObject } from "../../shared/entities/AuthorValueObject"; |
| 9 | +import { createCreatedAtValueObject } from "../../shared/entities/CreatedAtValueObject"; |
| 10 | +import type { CreatedAtValueObject } from "../../shared/entities/CreatedAtValueObject"; |
| 11 | +import { createAuthorValueObject } from "../../shared/entities/AuthorValueObject"; |
| 12 | +import type { AuthorValueObject } from "../../shared/entities/AuthorValueObject"; |
11 | 13 |
|
12 | | -type QuoteEntityAttributes = { |
13 | | - id: IdValueObject; |
| 14 | +export type QuoteEntity = Entity<{ |
14 | 15 | author: AuthorValueObject; |
15 | 16 | content: string; |
16 | 17 | createdAt: CreatedAtValueObject; |
| 18 | +}>; |
| 19 | + |
| 20 | +export type QuoteEntityFactoryInput = Pick<QuoteEntity, "content"> & { |
| 21 | + id?: string; |
| 22 | + fullName: string; |
17 | 23 | }; |
18 | 24 |
|
19 | | -export type QuoteEntityCreateInput = |
20 | | - GetValueFromValueObject<AuthorValueObject> & |
21 | | - Pick<QuoteEntityAttributes, "content"> & { |
22 | | - id?: string; |
23 | | - }; |
| 25 | +export const createQuoteEntity = createEntityFactory< |
| 26 | + QuoteEntity, |
| 27 | + QuoteEntityFactoryInput |
| 28 | +>((helpers, { id, content, fullName }) => { |
| 29 | + const guardContentResult = Guard.mustBeLessThanCharacters(content, 280); |
| 30 | + |
| 31 | + if (guardContentResult.type === "failure") return guardContentResult; |
| 32 | + |
| 33 | + const authorValueObject = createAuthorValueObject({ fullName }); |
24 | 34 |
|
25 | | -export class QuoteEntity extends Entity<QuoteEntityAttributes> { |
26 | | - private constructor(public override attributes: QuoteEntityAttributes) { |
27 | | - super(attributes); |
28 | | - } |
| 35 | + if (authorValueObject.type === "failure") return authorValueObject; |
29 | 36 |
|
30 | | - public static override create({ |
31 | | - id, |
| 37 | + const entity: QuoteEntity = { |
| 38 | + id: createIdValueObject(id), |
| 39 | + author: authorValueObject.payload, |
32 | 40 | content, |
33 | | - fullName, |
34 | | - }: QuoteEntityCreateInput) { |
35 | | - const guardContentResult = Guard.mustBeLessThanCharacters(content, 280); |
36 | | - |
37 | | - if (guardContentResult.type === "failure") return guardContentResult; |
38 | | - |
39 | | - const author = AuthorValueObject.create({ fullName }); |
40 | | - |
41 | | - if (author.type === "failure") return author; |
42 | | - |
43 | | - return success( |
44 | | - new QuoteEntity({ |
45 | | - id: IdValueObject.create(id), |
46 | | - author: author.payload, |
47 | | - content, |
48 | | - createdAt: CreatedAtValueObject.create(), |
49 | | - }), |
50 | | - ); |
51 | | - } |
52 | | -} |
| 41 | + createdAt: createCreatedAtValueObject(undefined), |
| 42 | + isEqualTo(input) { |
| 43 | + return helpers.isEqualTo(entity, input); |
| 44 | + }, |
| 45 | + }; |
| 46 | + |
| 47 | + return success(entity); |
| 48 | +}); |
0 commit comments