// API client for communicating with our server-side endpoints import type { FlowerPot, WateringLogEntry, User, DashboardSection, StatsCard } from './stores'; export interface DashboardLayout { sections: DashboardSection[]; statsCards: StatsCard[]; } // Plants API export const plantsApi = { async getAll(): Promise { const response = await fetch('/api/plants'); if (!response.ok) throw new Error('Failed to fetch plants'); return response.json(); }, async getById(id: string): Promise { const response = await fetch(`/api/plants/${id}`); if (!response.ok) throw new Error('Failed to fetch plant'); return response.json(); }, async create(plant: Omit): Promise { const response = await fetch('/api/plants', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(plant) }); if (!response.ok) throw new Error('Failed to create plant'); return response.json(); }, async update(id: string, plant: Partial): Promise { const response = await fetch(`/api/plants/${id}`, { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(plant) }); if (!response.ok) throw new Error('Failed to update plant'); return response.json(); }, async delete(id: string): Promise { const response = await fetch(`/api/plants/${id}`, { method: 'DELETE' }); if (!response.ok) throw new Error('Failed to delete plant'); } }; // Watering Logs API export const wateringLogsApi = { async getAll(limit?: number, potId?: string): Promise { const params = new URLSearchParams(); if (limit) params.append('limit', limit.toString()); if (potId) params.append('potId', potId); const response = await fetch(`/api/watering-logs?${params}`); if (!response.ok) throw new Error('Failed to fetch watering logs'); return response.json(); }, async getById(id: string): Promise { const response = await fetch(`/api/watering-logs/${id}`); if (!response.ok) throw new Error('Failed to fetch watering log'); return response.json(); }, async create(log: Omit): Promise { const response = await fetch('/api/watering-logs', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(log) }); if (!response.ok) throw new Error('Failed to create watering log'); return response.json(); }, async update(id: string, log: Partial): Promise { const response = await fetch(`/api/watering-logs/${id}`, { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(log) }); if (!response.ok) throw new Error('Failed to update watering log'); return response.json(); }, async delete(id: string): Promise { const response = await fetch(`/api/watering-logs/${id}`, { method: 'DELETE' }); if (!response.ok) throw new Error('Failed to delete watering log'); } }; // Users API export const usersApi = { async getAll(): Promise { const response = await fetch('/api/users'); if (!response.ok) throw new Error('Failed to fetch users'); return response.json(); }, async getById(id: string): Promise { const response = await fetch(`/api/users/${id}`); if (!response.ok) throw new Error('Failed to fetch user'); return response.json(); }, async create(user: User): Promise { const response = await fetch('/api/users', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(user) }); if (!response.ok) throw new Error('Failed to create user'); return response.json(); }, async update(id: string, user: Partial): Promise { const response = await fetch(`/api/users/${id}`, { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(user) }); if (!response.ok) throw new Error('Failed to update user'); return response.json(); }, async delete(id: string): Promise { const response = await fetch(`/api/users/${id}`, { method: 'DELETE' }); if (!response.ok) throw new Error('Failed to delete user'); } }; // Settings API export const settingsApi = { async getByUserId(userId: string): Promise { const response = await fetch(`/api/settings?userId=${userId}`); if (response.status === 404) return null; if (!response.ok) throw new Error('Failed to fetch settings'); return response.json(); }, async getById(id: string): Promise { const response = await fetch(`/api/settings/${id}`); if (!response.ok) throw new Error('Failed to fetch settings'); return response.json(); }, async create(settings: DashboardLayout & { userId: string }): Promise { const response = await fetch('/api/settings', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(settings) }); if (!response.ok) throw new Error('Failed to create settings'); return response.json(); }, async update(id: string, settings: Partial): Promise { const response = await fetch(`/api/settings/${id}`, { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(settings) }); if (!response.ok) throw new Error('Failed to update settings'); return response.json(); }, async delete(id: string): Promise { const response = await fetch(`/api/settings/${id}`, { method: 'DELETE' }); if (!response.ok) throw new Error('Failed to delete settings'); } };