phase 1-6 implemented
This commit is contained in:
1 parent
22c274bb83
commit
2c4927478a
73 files changed
+9702
-2354
No files matched your search
@@ -2,6 +2,7 @@ import { NextRequest, NextResponse } from "next/server";
|
||||
import { auth } from "@clerk/nextjs/server";
|
||||
import Database from "better-sqlite3";
|
||||
import { randomBytes } from "crypto";
|
||||
import log from "@/lib/logger";
|
||||
|
||||
const db = new Database("./data/fitai.db");
|
||||
|
||||
@@ -15,16 +16,14 @@ export async function GET(request: NextRequest) {
|
||||
}
|
||||
|
||||
const profile = db
|
||||
.prepare(
|
||||
`SELECT * FROM fitness_profiles WHERE userId = ?`
|
||||
)
|
||||
.prepare(`SELECT * FROM fitness_profiles WHERE userId = ?`)
|
||||
.get(userId);
|
||||
|
||||
if (profile) {
|
||||
const p = profile as any;
|
||||
// Parse JSON fields
|
||||
try {
|
||||
p.fitnessGoals = JSON.parse(p.fitnessGoals || '[]');
|
||||
p.fitnessGoals = JSON.parse(p.fitnessGoals || "[]");
|
||||
} catch (e) {
|
||||
p.fitnessGoals = [];
|
||||
}
|
||||
@@ -32,10 +31,10 @@ export async function GET(request: NextRequest) {
|
||||
|
||||
return NextResponse.json({ profile: profile || null });
|
||||
} catch (error) {
|
||||
console.error("Error fetching fitness profile:", error);
|
||||
log.error("Failed to fetch fitness profile", error);
|
||||
return NextResponse.json(
|
||||
{ error: "Failed to fetch fitness profile" },
|
||||
{ status: 500 }
|
||||
{ status: 500 },
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -61,7 +60,7 @@ export async function POST(request: NextRequest) {
|
||||
allergies,
|
||||
injuries,
|
||||
exerciseHabits,
|
||||
dietHabits
|
||||
dietHabits,
|
||||
} = body;
|
||||
|
||||
// Check if profile exists
|
||||
@@ -88,7 +87,7 @@ export async function POST(request: NextRequest) {
|
||||
exerciseHabits = ?,
|
||||
dietHabits = ?,
|
||||
updatedAt = ?
|
||||
WHERE userId = ?`
|
||||
WHERE userId = ?`,
|
||||
).run(
|
||||
height || null,
|
||||
weight || null,
|
||||
@@ -102,7 +101,7 @@ export async function POST(request: NextRequest) {
|
||||
exerciseHabits || null,
|
||||
dietHabits || null,
|
||||
now,
|
||||
userId
|
||||
userId,
|
||||
);
|
||||
|
||||
return NextResponse.json({
|
||||
@@ -115,7 +114,7 @@ export async function POST(request: NextRequest) {
|
||||
`INSERT INTO fitness_profiles
|
||||
(userId, height, weight, age, gender, fitnessGoals, activityLevel,
|
||||
medicalConditions, allergies, injuries, exerciseHabits, dietHabits, createdAt, updatedAt)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
||||
).run(
|
||||
userId,
|
||||
height || null,
|
||||
@@ -130,7 +129,7 @@ export async function POST(request: NextRequest) {
|
||||
exerciseHabits || null,
|
||||
dietHabits || null,
|
||||
now,
|
||||
now
|
||||
now,
|
||||
);
|
||||
|
||||
return NextResponse.json(
|
||||
@@ -138,14 +137,14 @@ export async function POST(request: NextRequest) {
|
||||
message: "Fitness profile created successfully",
|
||||
userId,
|
||||
},
|
||||
{ status: 201 }
|
||||
{ status: 201 },
|
||||
);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Error saving fitness profile:", error);
|
||||
log.error("Failed to save fitness profile", error);
|
||||
return NextResponse.json(
|
||||
{ error: "Failed to save fitness profile" },
|
||||
{ status: 500 }
|
||||
{ status: 500 },
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user