A social knowledge tool for researchers built on ATProto
at development 2.0 kB view raw
1import { ValueObject } from '../../../../../shared/domain/ValueObject'; 2import { ok, err, Result } from '../../../../../shared/core/Result'; 3import { CardTypeEnum } from '../CardType'; 4import { CuratorId } from '../CuratorId'; 5 6export class NoteCardContentValidationError extends Error { 7 constructor(message: string) { 8 super(message); 9 this.name = 'NoteCardContentValidationError'; 10 } 11} 12 13interface NoteCardContentProps { 14 type: CardTypeEnum.NOTE; 15 text: string; 16} 17 18export class NoteCardContent extends ValueObject<NoteCardContentProps> { 19 public static readonly MAX_TEXT_LENGTH = 10000; 20 21 get type(): CardTypeEnum.NOTE { 22 return this.props.type; 23 } 24 25 get text(): string { 26 return this.props.text; 27 } 28 29 private constructor(props: NoteCardContentProps) { 30 super(props); 31 } 32 33 public static create( 34 text: string, 35 ): Result<NoteCardContent, NoteCardContentValidationError> { 36 if (!text || text.trim().length === 0) { 37 return err( 38 new NoteCardContentValidationError('Note text cannot be empty'), 39 ); 40 } 41 42 if (text.length > this.MAX_TEXT_LENGTH) { 43 return err( 44 new NoteCardContentValidationError( 45 `Note text cannot exceed ${this.MAX_TEXT_LENGTH} characters`, 46 ), 47 ); 48 } 49 50 return ok( 51 new NoteCardContent({ 52 type: CardTypeEnum.NOTE, 53 text: text.trim(), 54 }), 55 ); 56 } 57 58 public updateText( 59 newText: string, 60 ): Result<NoteCardContent, NoteCardContentValidationError> { 61 if (!newText || newText.trim().length === 0) { 62 return err( 63 new NoteCardContentValidationError('Note text cannot be empty'), 64 ); 65 } 66 67 if (newText.length > NoteCardContent.MAX_TEXT_LENGTH) { 68 return err( 69 new NoteCardContentValidationError( 70 `Note text cannot exceed ${NoteCardContent.MAX_TEXT_LENGTH} characters`, 71 ), 72 ); 73 } 74 75 return ok( 76 new NoteCardContent({ 77 ...this.props, 78 text: newText.trim(), 79 }), 80 ); 81 } 82}