db layer implemented
using sqlite
This commit is contained in:
1 parent
e287e55f1f
commit
df668927f5
14 files changed
+2541
-70
No files matched your search
@@ -1,9 +1,10 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import bcrypt from 'bcryptjs'
|
||||
import { users } from '../../../../lib/database'
|
||||
import { getDatabase } from '../../../../lib/database/index'
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const db = await getDatabase()
|
||||
const { email, password } = await request.json()
|
||||
|
||||
if (!email || !password) {
|
||||
@@ -13,7 +14,7 @@ export async function POST(request: NextRequest) {
|
||||
)
|
||||
}
|
||||
|
||||
const user = users.find(u => u.email === email)
|
||||
const user = await db.getUserByEmail(email)
|
||||
|
||||
if (!user) {
|
||||
return NextResponse.json(
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import bcrypt from 'bcryptjs'
|
||||
import { users, clients } from '../../../../lib/database'
|
||||
import { getDatabase } from '../../../../lib/database/index'
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const db = await getDatabase()
|
||||
const { email, password, firstName, lastName, phone } = await request.json()
|
||||
|
||||
if (!email || !password || !firstName || !lastName) {
|
||||
@@ -13,7 +14,7 @@ export async function POST(request: NextRequest) {
|
||||
)
|
||||
}
|
||||
|
||||
const existingUser = users.find(u => u.email === email)
|
||||
const existingUser = await db.getUserByEmail(email)
|
||||
|
||||
if (existingUser) {
|
||||
return NextResponse.json(
|
||||
@@ -23,31 +24,22 @@ export async function POST(request: NextRequest) {
|
||||
}
|
||||
|
||||
const hashedPassword = await bcrypt.hash(password, 10)
|
||||
const userId = Math.random().toString(36).substr(2, 9)
|
||||
|
||||
const newUser = {
|
||||
id: userId,
|
||||
const newUser = await db.createUser({
|
||||
email,
|
||||
firstName,
|
||||
lastName,
|
||||
password: hashedPassword,
|
||||
phone,
|
||||
role: 'client',
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
}
|
||||
role: 'client'
|
||||
})
|
||||
|
||||
users.push(newUser)
|
||||
|
||||
const newClient = {
|
||||
id: Math.random().toString(36).substr(2, 9),
|
||||
userId,
|
||||
const newClient = await db.createClient({
|
||||
userId: newUser.id,
|
||||
membershipType: 'basic',
|
||||
membershipStatus: 'active',
|
||||
joinDate: new Date(),
|
||||
}
|
||||
|
||||
clients.push(newClient)
|
||||
joinDate: new Date()
|
||||
})
|
||||
|
||||
const { password: _, ...userWithoutPassword } = newUser
|
||||
|
||||
@@ -68,6 +60,16 @@ export async function POST(request: NextRequest) {
|
||||
}
|
||||
|
||||
export async function GET() {
|
||||
const usersWithoutPassword = users.map(({ password: _, ...user }) => user)
|
||||
return NextResponse.json({ users: usersWithoutPassword })
|
||||
try {
|
||||
const db = await getDatabase()
|
||||
const allUsers = await db.getAllUsers()
|
||||
const usersWithoutPassword = allUsers.map(({ password: _, ...user }) => user)
|
||||
return NextResponse.json({ users: usersWithoutPassword })
|
||||
} catch (error) {
|
||||
console.error('Get users error:', error)
|
||||
return NextResponse.json(
|
||||
{ error: 'Internal server error' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -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(
|
||||
|
||||
@@ -1,21 +1,25 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { users, clients } from '../../../lib/database'
|
||||
import { getDatabase } from '../../../lib/database/index'
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
const db = await getDatabase()
|
||||
const { searchParams } = new URL(request.url)
|
||||
const role = searchParams.get('role')
|
||||
|
||||
let filteredUsers = users
|
||||
let users = await db.getAllUsers()
|
||||
|
||||
if (role) {
|
||||
filteredUsers = users.filter(user => user.role === role)
|
||||
users = users.filter(user => user.role === role)
|
||||
}
|
||||
|
||||
const usersWithClients = filteredUsers.map(({ password: _, ...user }) => {
|
||||
const client = clients.find(c => c.userId === user.id)
|
||||
return { ...user, client }
|
||||
})
|
||||
const usersWithClients = await Promise.all(
|
||||
users.map(async (user) => {
|
||||
const { password: _, ...userWithoutPassword } = user
|
||||
const client = await db.getClientByUserId(user.id)
|
||||
return { ...userWithoutPassword, client }
|
||||
})
|
||||
)
|
||||
|
||||
return NextResponse.json({ users: usersWithClients })
|
||||
} catch (error) {
|
||||
|
||||
Reference in new issue
Block a user