-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #244 from UTDallasEPICS/integration
integration->main
- Loading branch information
Showing
42 changed files
with
1,300 additions
and
228 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { PrismaClient } from '@prisma/client'; | ||
|
||
const prisma = new PrismaClient(); | ||
|
||
interface VolunteerRequestBody { | ||
firstName: string; | ||
lastName: string; | ||
email: string; | ||
phone: string; | ||
} | ||
|
||
export async function POST(req: Request) { | ||
if (req.method !== 'POST') { | ||
return Response.json({ | ||
status: 405, | ||
message: 'Method Not Allowed', | ||
}); | ||
} | ||
|
||
try { | ||
const { firstName, lastName, email, phone } = (await req.json()) as VolunteerRequestBody; | ||
|
||
const existingVolunteer = await prisma.volunteer.findFirst({ | ||
where: { | ||
OR: [{ email: email }, { phone: phone }], | ||
}, | ||
}); | ||
|
||
if (existingVolunteer) { | ||
// Return a specific error message if the email or phone exists | ||
if (existingVolunteer.email === email) { | ||
return Response.json({ | ||
status: 400, | ||
message: 'Volunteer not added! Email already exists', | ||
}); | ||
} else if (existingVolunteer.phone === phone) { | ||
return Response.json({ | ||
status: 400, | ||
message: 'Volunteer not added! Phone number already exists', | ||
}); | ||
} | ||
} | ||
|
||
const volunteer = await prisma.volunteer.create({ | ||
data: { | ||
firstName: firstName, | ||
lastName: lastName, | ||
email: email, | ||
phone: phone, | ||
rides: { | ||
create: [], | ||
}, | ||
}, | ||
}); | ||
|
||
return Response.json({ | ||
status: 200, | ||
message: `${volunteer} created successfully!`, | ||
volunteer: volunteer, | ||
}); | ||
} catch (error) { | ||
console.error('Error creating volunteer:', error); | ||
return Response.json({ | ||
status: 500, | ||
message: 'Internal Server Error', | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { PrismaClient } from '@prisma/client'; | ||
|
||
const prisma = new PrismaClient(); | ||
|
||
interface DeleteVolunteerParams { | ||
id: number; | ||
} | ||
|
||
export async function DELETE(req: Request) { | ||
if (req.method !== 'DELETE') { | ||
return Response.json({ | ||
status: 405, | ||
message: 'Method Not Allowed', | ||
}); | ||
} | ||
|
||
try { | ||
const { id } = (await req.json()) as DeleteVolunteerParams; | ||
|
||
const existingVolunteer = await prisma.volunteer.findUnique({ | ||
where: { | ||
VolunteerID: id, | ||
}, | ||
}); | ||
|
||
if (!existingVolunteer) { | ||
return Response.json({ | ||
status: 404, | ||
message: 'Volunteer not found', | ||
}); | ||
} | ||
|
||
await prisma.volunteer.delete({ | ||
where: { | ||
VolunteerID: id, | ||
}, | ||
}); | ||
|
||
return Response.json({ | ||
status: 200, | ||
message: 'Volunteer deleted successfully', | ||
}); | ||
} catch (error) { | ||
console.error('Error deleting volunteer:', error); | ||
return Response.json({ | ||
status: 500, | ||
message: 'Internal Server Error', | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { PrismaClient } from '@prisma/client'; | ||
|
||
const prisma = new PrismaClient(); | ||
|
||
interface EditVolunteerParams { | ||
id: number; | ||
firstName: string; | ||
lastName: string; | ||
email: string; | ||
phone: string; | ||
} | ||
|
||
export async function PUT(req: Request) { | ||
if (req.method !== 'PUT') { | ||
return Response.json({ | ||
status: 405, | ||
message: 'Method Not Allowed', | ||
}); | ||
} | ||
|
||
try { | ||
const { id, firstName, lastName, email, phone } = (await req.json()) as EditVolunteerParams; | ||
|
||
const existingVolunteer = await prisma.volunteer.findUnique({ | ||
where: { | ||
VolunteerID: id, | ||
}, | ||
}); | ||
|
||
if (!existingVolunteer) { | ||
return Response.json({ | ||
status: 404, | ||
message: 'Volunteer not found', | ||
}); | ||
} | ||
|
||
const existingEmailVolunteer = await prisma.volunteer.findFirst({ | ||
where: { | ||
email: email, | ||
NOT: { | ||
VolunteerID: id, | ||
}, | ||
}, | ||
}); | ||
|
||
if (existingEmailVolunteer && existingEmailVolunteer.VolunteerID !== id) { | ||
return Response.json({ | ||
status: 409, | ||
message: 'Email is already in use by another volunteer', | ||
}); | ||
} | ||
|
||
const existingPhoneVolunteer = await prisma.volunteer.findFirst({ | ||
where: { | ||
phone: phone, | ||
NOT: { | ||
VolunteerID: id, | ||
}, | ||
}, | ||
}); | ||
|
||
if (existingPhoneVolunteer && existingPhoneVolunteer.VolunteerID !== id) { | ||
return Response.json({ | ||
status: 409, | ||
message: 'Phone number is already in use by another volunteer', | ||
}); | ||
} | ||
|
||
const updatedVolunteer = await prisma.volunteer.update({ | ||
where: { | ||
VolunteerID: id, | ||
}, | ||
data: { | ||
firstName, | ||
lastName, | ||
email, | ||
phone, | ||
}, | ||
}); | ||
|
||
return Response.json({ | ||
status: 200, | ||
message: 'Volunteer updated successfully', | ||
volunteer: updatedVolunteer, | ||
}); | ||
} catch (error) { | ||
console.error('Error updating volunteer:', error); | ||
return Response.json({ | ||
status: 500, | ||
message: 'Internal Server Error', | ||
}); | ||
} | ||
} |
Oops, something went wrong.