db layer implemented

using sqlite
This commit is contained in:
dimitar committed 2025-11-07 23:46:46 +01:00
1 parent e287e55f1f
commit df668927f5
14 files changed
+2541 -70

No files matched your search

+22 -11
View File
@@ -1,9 +1,10 @@
import { NextRequest, NextResponse } from 'next/server'
import { users, fitnessProfiles } from '../../../../lib/database'
import { getDatabase } from '../../../../lib/database/index'
export async function POST(request: NextRequest) {
try {
const profileData: FitnessProfile = await request.json()
const db = await getDatabase()
const profileData = await request.json()
if (!profileData.userId || !profileData.height || !profileData.weight || !profileData.age) {
return NextResponse.json(
@@ -12,21 +13,29 @@ export async function POST(request: NextRequest) {
)
}
// For demo purposes, we'll allow profile creation without strict user validation
// In production, you'd validate against a persistent database
console.log('Creating fitness profile for user ID:', profileData.userId)
// Check if user exists
const user = await db.getUserById(profileData.userId)
if (!user) {
return NextResponse.json(
{ error: 'User not found' },
{ status: 404 }
)
}
const existingProfile = fitnessProfiles.find(p => p.userId === profileData.userId)
// Check if profile already exists
const existingProfile = await db.getFitnessProfileByUserId(profileData.userId)
let profile
if (existingProfile) {
Object.assign(existingProfile, profileData)
profile = await db.updateFitnessProfile(profileData.userId, profileData)
} else {
fitnessProfiles.push(profileData)
profile = await db.createFitnessProfile(profileData)
}
return NextResponse.json(
{
message: 'Fitness profile saved successfully',
profile: profileData
profile
},
{ status: 201 }
)
@@ -41,11 +50,12 @@ export async function POST(request: NextRequest) {
export async function GET(request: NextRequest) {
try {
const db = await getDatabase()
const { searchParams } = new URL(request.url)
const userId = searchParams.get('userId')
if (userId) {
const profile = fitnessProfiles.find(p => p.userId === userId)
const profile = await db.getFitnessProfileByUserId(userId)
if (!profile) {
return NextResponse.json(
{ error: 'Profile not found' },
@@ -55,7 +65,8 @@ export async function GET(request: NextRequest) {
return NextResponse.json({ profile })
}
return NextResponse.json({ profiles: fitnessProfiles })
const profiles = await db.getAllFitnessProfiles()
return NextResponse.json({ profiles })
} catch (error) {
console.error('Get fitness profiles error:', error)
return NextResponse.json(