Files
task-manager/server/storage.ts
T
paul-nothaft 52f29fa101 Add a system for organizing tasks with customizable color-coded labels
Implement a label system with API endpoints for CRUD operations on labels and tasks, including schema definitions for labels and tasks with label associations.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: ceced2fc-aa46-458d-ba87-ddd4b7bb1518
Replit-Commit-Checkpoint-Type: intermediate_checkpoint
Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/659922a9-0087-461c-90dd-6d9a58b81d4d/ceced2fc-aa46-458d-ba87-ddd4b7bb1518/XrG8SoT
2025-09-11 11:54:28 +00:00

143 lines
4.0 KiB
TypeScript

import { type User, type InsertUser, type Label, type InsertLabel, type Task, type InsertTask } from "@shared/schema";
import { randomUUID } from "crypto";
// modify the interface with any CRUD methods
// you might need
export interface IStorage {
getUser(id: string): Promise<User | undefined>;
getUserByUsername(username: string): Promise<User | undefined>;
createUser(user: InsertUser): Promise<User>;
// Labels
getAllLabels(): Promise<Label[]>;
getLabel(id: string): Promise<Label | undefined>;
createLabel(label: InsertLabel): Promise<Label>;
updateLabel(id: string, updates: Partial<InsertLabel>): Promise<Label | undefined>;
deleteLabel(id: string): Promise<boolean>;
// Tasks
getAllTasks(): Promise<Task[]>;
getTask(id: string): Promise<Task | undefined>;
createTask(task: InsertTask): Promise<Task>;
updateTask(id: string, updates: Partial<InsertTask>): Promise<Task | undefined>;
deleteTask(id: string): Promise<boolean>;
}
export class MemStorage implements IStorage {
private users: Map<string, User>;
private labels: Map<string, Label>;
private tasks: Map<string, Task>;
constructor() {
this.users = new Map();
this.labels = new Map();
this.tasks = new Map();
// Create some default labels
this.createDefaultLabels();
}
private async createDefaultLabels() {
const defaultLabels = [
{ name: "Work", color: "#3B82F6" },
{ name: "Personal", color: "#10B981" },
{ name: "Urgent", color: "#EF4444" },
{ name: "Study", color: "#8B5CF6" },
];
for (const labelData of defaultLabels) {
await this.createLabel(labelData);
}
}
async getUser(id: string): Promise<User | undefined> {
return this.users.get(id);
}
async getUserByUsername(username: string): Promise<User | undefined> {
return Array.from(this.users.values()).find(
(user) => user.username === username,
);
}
async createUser(insertUser: InsertUser): Promise<User> {
const id = randomUUID();
const user: User = { ...insertUser, id };
this.users.set(id, user);
return user;
}
// Labels
async getAllLabels(): Promise<Label[]> {
return Array.from(this.labels.values());
}
async getLabel(id: string): Promise<Label | undefined> {
return this.labels.get(id);
}
async createLabel(insertLabel: InsertLabel): Promise<Label> {
const id = randomUUID();
const label: Label = { ...insertLabel, id };
this.labels.set(id, label);
return label;
}
async updateLabel(id: string, updates: Partial<InsertLabel>): Promise<Label | undefined> {
const existing = this.labels.get(id);
if (!existing) return undefined;
const updated: Label = { ...existing, ...updates };
this.labels.set(id, updated);
return updated;
}
async deleteLabel(id: string): Promise<boolean> {
return this.labels.delete(id);
}
// Tasks
async getAllTasks(): Promise<Task[]> {
return Array.from(this.tasks.values());
}
async getTask(id: string): Promise<Task | undefined> {
return this.tasks.get(id);
}
async createTask(insertTask: InsertTask): Promise<Task> {
const id = randomUUID();
const task: Task = {
id,
title: insertTask.title,
description: insertTask.description || null,
status: insertTask.status || "todo",
priority: insertTask.priority || "medium",
dueDate: insertTask.dueDate || null,
timeTracked: insertTask.timeTracked || 0,
isTracking: insertTask.isTracking || false,
projectId: insertTask.projectId || null,
notes: insertTask.notes || null,
labelId: insertTask.labelId || null,
};
this.tasks.set(id, task);
return task;
}
async updateTask(id: string, updates: Partial<InsertTask>): Promise<Task | undefined> {
const existing = this.tasks.get(id);
if (!existing) return undefined;
const updated: Task = { ...existing, ...updates };
this.tasks.set(id, updated);
return updated;
}
async deleteTask(id: string): Promise<boolean> {
return this.tasks.delete(id);
}
}
export const storage = new MemStorage();