import type { Task, CreateTaskInput } from "../types.js";
import { TaskStatus } from "../types.js";
import type { TaskService } from "./TaskService.js";
/**
* In-memory implementation of TaskService.
* Tasks are stored in memory and will be lost on server restart.
*/
export class InMemoryTaskService implements TaskService {
private tasks: Map<string, Task> = new Map();
private nextId = 1;
/**
* Generate a unique task ID
*/
private generateId(): string {
return `task-${this.nextId++}`;
}
async list(status?: string): Promise<Task[]> {
const tasks = Array.from(this.tasks.values());
if (status) {
return tasks.filter((task) => task.status === status);
}
return tasks;
}
async get(id: string): Promise<Task | undefined> {
return this.tasks.get(id);
}
async create(input: CreateTaskInput): Promise<Task> {
const task: Task = {
id: this.generateId(),
title: input.title,
description: input.description,
status: TaskStatus.PENDING,
createdAt: new Date(),
};
this.tasks.set(task.id, task);
return task;
}
async complete(id: string): Promise<Task | undefined> {
const task = this.tasks.get(id);
if (!task) {
return undefined;
}
const updatedTask: Task = {
...task,
status: TaskStatus.COMPLETED,
completedAt: new Date(),
};
this.tasks.set(id, updatedTask);
return updatedTask;
}
async delete(id: string): Promise<boolean> {
return this.tasks.delete(id);
}
}