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

+3 -2
View File
@@ -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(
+22 -20
View File
@@ -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 }
)
}
}