Add children and families API
This commit is contained in:
parent
eddb1dd8ea
commit
54df45cc8a
2 changed files with 93 additions and 0 deletions
51
src/app/api/children/route.ts
Normal file
51
src/app/api/children/route.ts
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
import { NextResponse } from "next/server";
|
||||
import { sql } from "@/db";
|
||||
|
||||
interface ChildEntry {
|
||||
familyId: string;
|
||||
name: string;
|
||||
birthDate: string;
|
||||
sex: "male" | "female" | "other";
|
||||
}
|
||||
|
||||
export async function POST(request: Request) {
|
||||
try {
|
||||
const body: ChildEntry = await request.json();
|
||||
const { familyId, name, birthDate, sex } = body;
|
||||
|
||||
if (!familyId || !name || !birthDate || !sex) {
|
||||
return NextResponse.json({ error: "Missing required fields" }, { status: 400 });
|
||||
}
|
||||
|
||||
const [child] = await sql.unsafe(
|
||||
`INSERT INTO children (family_id, name, birth_date, sex, stage) VALUES ($1, $2, $3, $4, 'newborn') RETURNING *`,
|
||||
[familyId, name, birthDate, sex]
|
||||
);
|
||||
|
||||
return NextResponse.json({ success: true, child });
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return NextResponse.json({ error: String(error) }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
export async function GET(request: Request) {
|
||||
const { searchParams } = new URL(request.url);
|
||||
const familyId = searchParams.get("familyId");
|
||||
|
||||
if (!familyId) {
|
||||
return NextResponse.json({ error: "familyId required" }, { status: 400 });
|
||||
}
|
||||
|
||||
try {
|
||||
const children = await sql.unsafe(
|
||||
`SELECT * FROM children WHERE family_id = $1 ORDER BY created_at DESC`,
|
||||
[familyId]
|
||||
);
|
||||
|
||||
return NextResponse.json({ children });
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return NextResponse.json({ error: String(error) }, { status: 500 });
|
||||
}
|
||||
}
|
||||
42
src/app/api/families/route.ts
Normal file
42
src/app/api/families/route.ts
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
import { NextResponse } from "next/server";
|
||||
import { sql } from "@/db";
|
||||
|
||||
export async function POST(request: Request) {
|
||||
try {
|
||||
const body = await request.json();
|
||||
const { name } = body;
|
||||
|
||||
const [family] = await sql.unsafe(
|
||||
`INSERT INTO families (name) VALUES ($1) RETURNING *`,
|
||||
[name || "My Family"]
|
||||
);
|
||||
|
||||
return NextResponse.json({ success: true, family });
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return NextResponse.json({ error: String(error) }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
export async function GET(request: Request) {
|
||||
const { searchParams } = new URL(request.url);
|
||||
const userId = searchParams.get("userId");
|
||||
|
||||
if (!userId) {
|
||||
return NextResponse.json({ error: "userId required" }, { status: 400 });
|
||||
}
|
||||
|
||||
try {
|
||||
const families = await sql.unsafe(
|
||||
`SELECT f.* FROM families f
|
||||
JOIN family_members fm ON f.id = fm.family_id
|
||||
WHERE fm.user_id = $1`,
|
||||
[userId]
|
||||
);
|
||||
|
||||
return NextResponse.json({ families });
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return NextResponse.json({ error: String(error) }, { status: 500 });
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue