import { Post } from './Post' ; import { DomainException } from 'domain/exceptions/DomainException' ; import { IEntity } from 'domain/shared/IEntity' ; export class User implements IEntity { id ? : number ; name : string ; email : string ; posts ? : Post [ ] ; createdAt ? : Date ; updatedAt ? : Date ; constructor ( name , email , posts ? , id ? ) { this . name = name ; this . email = email ; this . posts = posts ; this . id = id ; } findPost ( postId ) { return this . posts ?. find ( p => p . id === postId ) ?? null ; } findPosts ( ) { return this . posts ?? [ ] ; } createPost ( post ) { if ( ! this . posts ) this . posts = new Array < Post > ( ) ; if ( this . posts . map ( p => p . title ) . includes ( post . title ) ) throw new DomainException ( 'Post with the same name already exists' ) ; this . posts . push ( post ) ; } equals ( entity ) { if ( ! ( entity instanceof User ) ) return false ; return this . id === entity . id ; } }