feat: garden containers crud (task 71)
- add listContainers/getContainer/searchContainers queries - add createContainer/updateContainer/deleteContainer server actions with logActivity - add ContainerList, ContainerDetail, ContainerForm client components - add /garden, /garden/containers/[id], /garden/containers/new pages - register garden.container entity with share + search in manifest - add sprout icon to NavIcon registry - add garden e2e test spec
This commit is contained in:
@@ -1 +1,135 @@
|
||||
// Garden query functions — implemented in tasks 71–74
|
||||
import { and, desc, eq, sql } from "drizzle-orm";
|
||||
import { db } from "@/lib/db";
|
||||
import { getCurrentSession } from "@/lib/session";
|
||||
import { gardenContainers, gardenPlants } from "../schema";
|
||||
|
||||
export type ContainerDto = {
|
||||
id: string;
|
||||
householdId: string;
|
||||
name: string;
|
||||
type: string;
|
||||
locationNotes: string | null;
|
||||
coverImageUrl: string | null;
|
||||
plantCount: number;
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
};
|
||||
|
||||
export type ContainerDetailDto = ContainerDto & {
|
||||
plants: PlantSummaryDto[];
|
||||
};
|
||||
|
||||
export type PlantSummaryDto = {
|
||||
id: string;
|
||||
name: string;
|
||||
scientificName: string | null;
|
||||
healthStatus: string;
|
||||
primaryImageUrl: string | null;
|
||||
category: string;
|
||||
};
|
||||
|
||||
export async function listContainers(): Promise<ContainerDto[]> {
|
||||
const { household } = await getCurrentSession();
|
||||
|
||||
const rows = await db
|
||||
.select({
|
||||
id: gardenContainers.id,
|
||||
householdId: gardenContainers.householdId,
|
||||
name: gardenContainers.name,
|
||||
type: gardenContainers.type,
|
||||
locationNotes: gardenContainers.locationNotes,
|
||||
coverImageUrl: gardenContainers.coverImageUrl,
|
||||
createdAt: gardenContainers.createdAt,
|
||||
updatedAt: gardenContainers.updatedAt,
|
||||
plantCount: sql<number>`count(${gardenPlants.id})::int`,
|
||||
})
|
||||
.from(gardenContainers)
|
||||
.leftJoin(gardenPlants, eq(gardenPlants.containerId, gardenContainers.id))
|
||||
.where(eq(gardenContainers.householdId, household.id))
|
||||
.groupBy(gardenContainers.id)
|
||||
.orderBy(gardenContainers.name);
|
||||
|
||||
return rows.map((r) => ({
|
||||
...r,
|
||||
plantCount: r.plantCount ?? 0,
|
||||
createdAt: r.createdAt.toISOString(),
|
||||
updatedAt: r.updatedAt.toISOString(),
|
||||
}));
|
||||
}
|
||||
|
||||
export async function getContainer(id: string): Promise<ContainerDetailDto | null> {
|
||||
const { household } = await getCurrentSession();
|
||||
|
||||
const [container] = await db
|
||||
.select({
|
||||
id: gardenContainers.id,
|
||||
householdId: gardenContainers.householdId,
|
||||
name: gardenContainers.name,
|
||||
type: gardenContainers.type,
|
||||
locationNotes: gardenContainers.locationNotes,
|
||||
coverImageUrl: gardenContainers.coverImageUrl,
|
||||
createdAt: gardenContainers.createdAt,
|
||||
updatedAt: gardenContainers.updatedAt,
|
||||
plantCount: sql<number>`(select count(*)::int from garden_plants where container_id = ${gardenContainers.id})`,
|
||||
})
|
||||
.from(gardenContainers)
|
||||
.where(and(eq(gardenContainers.id, id), eq(gardenContainers.householdId, household.id)))
|
||||
.limit(1);
|
||||
|
||||
if (!container) return null;
|
||||
|
||||
const plants = await db
|
||||
.select({
|
||||
id: gardenPlants.id,
|
||||
name: gardenPlants.name,
|
||||
scientificName: gardenPlants.scientificName,
|
||||
healthStatus: gardenPlants.healthStatus,
|
||||
primaryImageUrl: gardenPlants.primaryImageUrl,
|
||||
category: gardenPlants.category,
|
||||
})
|
||||
.from(gardenPlants)
|
||||
.where(eq(gardenPlants.containerId, id))
|
||||
.orderBy(desc(gardenPlants.name));
|
||||
|
||||
return {
|
||||
id: container.id,
|
||||
householdId: container.householdId,
|
||||
name: container.name,
|
||||
type: container.type,
|
||||
locationNotes: container.locationNotes,
|
||||
coverImageUrl: container.coverImageUrl,
|
||||
plantCount: container.plantCount ?? 0,
|
||||
createdAt: container.createdAt.toISOString(),
|
||||
updatedAt: container.updatedAt.toISOString(),
|
||||
plants,
|
||||
};
|
||||
}
|
||||
|
||||
export async function canAccessContainer(id: string, householdId: string): Promise<boolean> {
|
||||
const [row] = await db
|
||||
.select({ id: gardenContainers.id })
|
||||
.from(gardenContainers)
|
||||
.where(and(eq(gardenContainers.id, id), eq(gardenContainers.householdId, householdId)))
|
||||
.limit(1);
|
||||
|
||||
return !!row;
|
||||
}
|
||||
|
||||
export async function searchContainers(query: string, householdId: string) {
|
||||
const rows = await db
|
||||
.select({ id: gardenContainers.id, name: gardenContainers.name })
|
||||
.from(gardenContainers)
|
||||
.where(
|
||||
and(
|
||||
eq(gardenContainers.householdId, householdId),
|
||||
sql`${gardenContainers.name} ilike ${`%${query}%`}`,
|
||||
),
|
||||
)
|
||||
.limit(10);
|
||||
|
||||
return rows.map((r) => ({
|
||||
id: r.id,
|
||||
title: r.name,
|
||||
url: `/garden/containers/${r.id}`,
|
||||
}));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user