feat: add server persistence for rosary streak
All checks were successful
CI / update (push) Successful in 1m21s
All checks were successful
CI / update (push) Successful in 1m21s
- Add RosaryStreak MongoDB model for logged-in users - Add /api/glaube/rosary-streak GET/POST endpoints - Sync streak to server when logged in, merge local/server data - Auto-sync when coming back online (PWA offline support) - Falls back to localStorage for guests
This commit is contained in:
61
src/routes/api/glaube/rosary-streak/+server.ts
Normal file
61
src/routes/api/glaube/rosary-streak/+server.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
import { json, error, type RequestHandler } from '@sveltejs/kit';
|
||||
import { RosaryStreak } from '../../../../models/RosaryStreak';
|
||||
import { dbConnect } from '../../../../utils/db';
|
||||
|
||||
export const GET: RequestHandler = async ({ locals }) => {
|
||||
const session = await locals.auth();
|
||||
|
||||
if (!session?.user?.nickname) {
|
||||
throw error(401, 'Authentication required');
|
||||
}
|
||||
|
||||
await dbConnect();
|
||||
|
||||
try {
|
||||
const streak = await RosaryStreak.findOne({
|
||||
username: session.user.nickname
|
||||
}).lean();
|
||||
|
||||
return json({
|
||||
length: streak?.length ?? 0,
|
||||
lastPrayed: streak?.lastPrayed ?? null
|
||||
});
|
||||
} catch (e) {
|
||||
throw error(500, 'Failed to fetch rosary streak');
|
||||
}
|
||||
};
|
||||
|
||||
export const POST: RequestHandler = async ({ request, locals }) => {
|
||||
const session = await locals.auth();
|
||||
|
||||
if (!session?.user?.nickname) {
|
||||
throw error(401, 'Authentication required');
|
||||
}
|
||||
|
||||
const { length, lastPrayed } = await request.json();
|
||||
|
||||
if (typeof length !== 'number' || length < 0) {
|
||||
throw error(400, 'Valid streak length required');
|
||||
}
|
||||
|
||||
if (lastPrayed !== null && typeof lastPrayed !== 'string') {
|
||||
throw error(400, 'Invalid lastPrayed format');
|
||||
}
|
||||
|
||||
await dbConnect();
|
||||
|
||||
try {
|
||||
const updated = await RosaryStreak.findOneAndUpdate(
|
||||
{ username: session.user.nickname },
|
||||
{ length, lastPrayed },
|
||||
{ upsert: true, new: true }
|
||||
).lean();
|
||||
|
||||
return json({
|
||||
length: updated.length,
|
||||
lastPrayed: updated.lastPrayed
|
||||
});
|
||||
} catch (e) {
|
||||
throw error(500, 'Failed to update rosary streak');
|
||||
}
|
||||
};
|
||||
@@ -1238,7 +1238,7 @@ l536 389l-209 -629zM1671 934l-370 267l150 436l-378 -271l-371 271q8 -34 15 -68q10
|
||||
<LanguageToggle />
|
||||
</div>
|
||||
|
||||
<StreakCounter />
|
||||
<StreakCounter user={data.session?.user} />
|
||||
</div>
|
||||
|
||||
<div class="rosary-layout">
|
||||
|
||||
Reference in New Issue
Block a user