Files
task-manager/server/storage.ts
T
paul-nothaft 18fc3fed81 Add labels to tasks for better organization and filtering
Introduce a label system to categorize tasks, enhancing organization and enabling filtering. The initial setup includes default labels like 'Work', 'Personal', 'Urgent', and 'Study', with fixed IDs for consistent data management. The client-side now supports associating labels with tasks, and the `TaskCard` component fetches labels to display their corresponding colors. The server-side storage has been updated to use predefined IDs for these default labels.

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

144 lines
4.3 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() {
// Use fixed IDs to prevent ID churn on server restarts
const defaultLabels = [
{ id: 'cb44bed1-8ba3-43fe-9498-bb28e483ed1f', name: "Work", color: "#3B82F6" },
{ id: '274f0ba4-a133-471a-bbe9-8189aa3b0106', name: "Personal", color: "#10B981" },
{ id: '2e8c3aa0-278f-4220-b2c5-aa109b4279b7', name: "Urgent", color: "#EF4444" },
{ id: 'c5eccac9-7919-4eb1-bb50-badacad6b1c1', name: "Study", color: "#8B5CF6" },
];
for (const label of defaultLabels) {
this.labels.set(label.id, label);
}
}
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();