68 lines
2.5 KiB
TypeScript
68 lines
2.5 KiB
TypeScript
import { IUseCase } from '../../core/application/IUseCase';
|
|
import { Result } from '../../core/domain/Result';
|
|
import { DomainError, ValidationError } from '../../core/domain/DomainError';
|
|
import { IListRepository } from '../domain/IListRepository';
|
|
import { NoteList } from '../domain/NoteList';
|
|
import { ListItem } from '../domain/ListItem';
|
|
import { AddListItemDto } from './dtos/ListDtos';
|
|
|
|
export class AddListItemUseCase implements IUseCase<AddListItemDto, ListItem> {
|
|
constructor(private readonly listRepository: IListRepository) {}
|
|
|
|
public async execute(dto: AddListItemDto): Promise<Result<ListItem, DomainError>> {
|
|
if (!dto.title || dto.title.trim().length === 0) {
|
|
return Result.fail(new ValidationError('Item title is required'));
|
|
}
|
|
|
|
if (!dto.ownerId) {
|
|
return Result.fail(new ValidationError('Owner ID is required'));
|
|
}
|
|
|
|
let targetList: NoteList | null = null;
|
|
|
|
if (dto.listId) {
|
|
targetList = await this.listRepository.findListById(dto.listId);
|
|
} else if (dto.listTitle) {
|
|
targetList = await this.listRepository.findListByTitleAndOwner(dto.listTitle.trim(), dto.ownerId);
|
|
}
|
|
|
|
// Если список не найден по названию (например, первая запись фильма), создаем список автоматически
|
|
if (!targetList) {
|
|
const listTitle = dto.listTitle?.trim() || 'Общий список';
|
|
const category = dto.category || (listTitle.toLowerCase().includes('фильм') ? 'movies' : 'general');
|
|
|
|
targetList = new NoteList({
|
|
id: `list-${Date.now()}-${Math.random().toString(36).substr(2, 6)}`,
|
|
title: listTitle,
|
|
category: category,
|
|
description: `Автоматически создан для ${listTitle}`,
|
|
ownerId: dto.ownerId,
|
|
createdAt: new Date(),
|
|
updatedAt: new Date(),
|
|
items: []
|
|
});
|
|
|
|
await this.listRepository.saveList(targetList);
|
|
}
|
|
|
|
const newItem = new ListItem({
|
|
id: `item-${Date.now()}-${Math.random().toString(36).substr(2, 6)}`,
|
|
listId: targetList.id,
|
|
title: dto.title.trim(),
|
|
description: dto.description || '',
|
|
isCompleted: false,
|
|
status: targetList.category === 'movies' ? 'want_to_watch' : 'todo',
|
|
priority: dto.priority || 'normal',
|
|
metadata: dto.metadata || {},
|
|
dueDate: null,
|
|
createdAt: new Date(),
|
|
updatedAt: new Date()
|
|
});
|
|
|
|
await this.listRepository.saveItem(newItem);
|
|
targetList.addItem(newItem);
|
|
|
|
return Result.ok(newItem);
|
|
}
|
|
}
|