mirror of
https://github.com/EdiFarcas/Car-Fuel-Tracking-App.git
synced 2026-06-28 17:00:42 +03:00
First protorype
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import { getSession } from '@/lib/auth';
|
||||
import { prisma } from '@/lib/prisma';
|
||||
|
||||
export async function GET(req: Request) {
|
||||
const session = await getSession();
|
||||
if (!session) return NextResponse.json({ message: 'Unauthorized' }, { status: 401 });
|
||||
|
||||
const { searchParams } = new URL(req.url);
|
||||
const carId = searchParams.get('carId');
|
||||
|
||||
const fillUps = await prisma.fillUp.findMany({
|
||||
where: {
|
||||
car: {
|
||||
id: carId || '',
|
||||
user: { email: session.user?.email! },
|
||||
},
|
||||
},
|
||||
orderBy: { date: 'desc' },
|
||||
});
|
||||
|
||||
return NextResponse.json(fillUps);
|
||||
}
|
||||
|
||||
export async function POST(req: Request) {
|
||||
const session = await getSession();
|
||||
if (!session) return NextResponse.json({ message: 'Unauthorized' }, { status: 401 });
|
||||
|
||||
const { carId, mileage, liters, cost, currency } = await req.json();
|
||||
if (!carId || !mileage || !liters || !cost || !currency) {
|
||||
return NextResponse.json({ message: 'Missing fields' }, { status: 400 });
|
||||
}
|
||||
|
||||
const car = await prisma.car.findFirst({
|
||||
where: {
|
||||
id: carId,
|
||||
user: { email: session.user?.email! },
|
||||
},
|
||||
});
|
||||
|
||||
if (!car) return NextResponse.json({ message: 'Car not found' }, { status: 404 });
|
||||
|
||||
const fillUp = await prisma.fillUp.create({
|
||||
data: {
|
||||
mileage: parseInt(mileage),
|
||||
liters: parseFloat(liters),
|
||||
cost: parseFloat(cost),
|
||||
currency,
|
||||
carId,
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json(fillUp);
|
||||
}
|
||||
Reference in New Issue
Block a user