Move Add Prayer button to sidebar and implement comprehensive prayer functionality

- Integrated prayer data with Prisma database schema
- Updated API endpoints to use actual database
- Implemented AI prayer generation with Azure OpenAI
- Added user authentication for prayer creation
- Moved Add Prayer button from FAB to sidebar top
- Added prayer count tracking and user prayer status

🤖 Generated with Claude Code

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
andupetcu
2025-09-21 23:17:20 +03:00
parent 6492601355
commit 5a3c6a6691
7 changed files with 669 additions and 259 deletions

View File

@@ -1,11 +1,13 @@
import { NextRequest, NextResponse } from 'next/server'
import { z } from 'zod'
import { prisma } from '@/lib/db'
import { getServerSession } from 'next-auth'
const createPrayerSchema = z.object({
title: z.string().min(1).max(200),
description: z.string().min(1).max(1000),
category: z.enum(['personal', 'family', 'health', 'work', 'ministry', 'world']),
author: z.string().optional().default('Anonim')
isAnonymous: z.boolean().optional().default(false)
})
export async function GET(request: NextRequest) {
@@ -13,89 +15,54 @@ export async function GET(request: NextRequest) {
const { searchParams } = new URL(request.url)
const category = searchParams.get('category')
const limit = parseInt(searchParams.get('limit') || '20')
const userId = searchParams.get('userId')
// Mock prayer data for now
// TODO: Replace with actual database queries
const allPrayers = [
{
id: '1',
title: 'Rugăciune pentru vindecare',
description: 'Te rog să te rogi pentru tatăl meu care se află în spital. Are nevoie de vindecarea lui Dumnezeu.',
category: 'health',
author: 'Maria P.',
timestamp: new Date(Date.now() - 2 * 60 * 60 * 1000),
prayerCount: 23,
isPrayedFor: false,
},
{
id: '2',
title: 'Îndrumarea lui Dumnezeu în carieră',
description: 'Caut direcția lui Dumnezeu pentru următorul pas în cariera mea. Te rog să te rogi pentru claritate și pace.',
category: 'work',
author: 'Alexandru M.',
timestamp: new Date(Date.now() - 5 * 60 * 60 * 1000),
prayerCount: 15,
isPrayedFor: true,
},
{
id: '3',
title: 'Unitatea în familia noastră',
description: 'Rugați-vă pentru restaurarea relațiilor în familia noastră și pentru iertarea reciprocă.',
category: 'family',
author: 'Anonim',
timestamp: new Date(Date.now() - 1 * 24 * 60 * 60 * 1000),
prayerCount: 41,
isPrayedFor: false,
},
{
id: '4',
title: 'Pentru misionarii din Africa',
description: 'Rugați-vă pentru protecția și proviziunea pentru misionarii noștri care lucrează în Africa.',
category: 'ministry',
author: 'Pavel R.',
timestamp: new Date(Date.now() - 6 * 60 * 60 * 1000),
prayerCount: 12,
isPrayedFor: false,
},
{
id: '5',
title: 'Pace în Ucraina',
description: 'Să ne rugăm pentru pace și protecție pentru poporul ucrainean în aceste timpuri dificile.',
category: 'world',
author: 'Comunitatea',
timestamp: new Date(Date.now() - 12 * 60 * 60 * 1000),
prayerCount: 89,
isPrayedFor: true,
},
{
id: '6',
title: 'Trecerea prin depresie',
description: 'Am nevoie de rugăciuni pentru a trece prin această perioadă grea de depresie și anxietate.',
category: 'personal',
author: 'Anonim',
timestamp: new Date(Date.now() - 8 * 60 * 60 * 1000),
prayerCount: 34,
isPrayedFor: false,
}
]
let filteredPrayers = allPrayers
// Apply category filter
if (category && category !== 'all') {
filteredPrayers = allPrayers.filter(prayer => prayer.category === category)
// Build the where clause
const where: any = {
isActive: true
}
// Apply limit
filteredPrayers = filteredPrayers.slice(0, limit)
if (category && category !== 'all') {
where.category = category
}
// Sort by timestamp (newest first)
filteredPrayers.sort((a, b) => b.timestamp.getTime() - a.timestamp.getTime())
// Fetch prayers from database with user prayer status
const prayers = await prisma.prayerRequest.findMany({
where,
take: limit,
orderBy: {
createdAt: 'desc'
},
include: {
user: {
select: {
name: true
}
},
userPrayers: userId ? {
where: {
userId: userId
}
} : false
}
})
// Format prayers for response
const formattedPrayers = prayers.map(prayer => ({
id: prayer.id,
title: prayer.title,
description: prayer.description,
category: prayer.category,
author: prayer.isAnonymous ? 'Anonim' : prayer.author,
timestamp: prayer.createdAt,
prayerCount: prayer.prayerCount,
isPrayedFor: userId && prayer.userPrayers ? prayer.userPrayers.length > 0 : false
}))
return NextResponse.json({
success: true,
prayers: filteredPrayers,
total: filteredPrayers.length
prayers: formattedPrayers,
total: formattedPrayers.length
})
} catch (error) {
console.error('Error fetching prayers:', error)
@@ -115,25 +82,51 @@ export async function POST(request: NextRequest) {
const body = await request.json()
const validatedData = createPrayerSchema.parse(body)
// Create new prayer object
const newPrayer = {
id: Date.now().toString(),
title: validatedData.title,
description: validatedData.description,
category: validatedData.category,
author: validatedData.author,
timestamp: new Date(),
prayerCount: 0,
isPrayedFor: false,
// Get user from auth token if available
const authHeader = request.headers.get('authorization')
let userId: string | null = null
let userName: string = 'Anonim'
if (authHeader && authHeader.startsWith('Bearer ')) {
const token = authHeader.slice(7)
// Verify token and get user
const session = await prisma.session.findUnique({
where: { token },
include: { user: true }
})
if (session && session.expiresAt > new Date()) {
userId = session.userId
userName = session.user.name || 'Anonim'
}
}
// TODO: Save to database
// For now, just return the created prayer
console.log('New prayer created:', newPrayer)
// Create new prayer in database
const newPrayer = await prisma.prayerRequest.create({
data: {
title: validatedData.title,
description: validatedData.description,
category: validatedData.category,
author: validatedData.isAnonymous ? 'Anonim' : userName,
isAnonymous: validatedData.isAnonymous,
userId: validatedData.isAnonymous ? null : userId,
prayerCount: 0,
isActive: true
}
})
return NextResponse.json({
success: true,
prayer: newPrayer,
prayer: {
id: newPrayer.id,
title: newPrayer.title,
description: newPrayer.description,
category: newPrayer.category,
author: newPrayer.author,
timestamp: newPrayer.createdAt,
prayerCount: newPrayer.prayerCount,
isPrayedFor: false
},
message: 'Prayer request submitted successfully'
}, { status: 201 })
} catch (error) {