diff --git a/.env b/.env index ed93ff8..95a6415 100644 --- a/.env +++ b/.env @@ -1 +1 @@ -DATABASE_URL="postgresql://postgres:password@localhost:5433/sa_db?schema=public" \ No newline at end of file +DATABASE_URL="postgresql://postgres:1234@localhost:5432/sa_db?schema=public" \ No newline at end of file diff --git a/.http b/.http new file mode 100644 index 0000000..9ccd466 --- /dev/null +++ b/.http @@ -0,0 +1,14 @@ +POST http://localhost:8081/createappointment +content-type: application/json + +{ + "email": "test@gmail.com", + "firstName":"Willem", + "lastName":"Botha", + "cellphone":"0769693806", + "petName":"Jax", + "petType":"Dog", + "vistReason":"Sore foot", + "appointmentEndDate":"2021-07-29 08:04:03.137", + "appointmentStartDate":"2021-07-30 08:04:03.137" +} \ No newline at end of file diff --git a/routes/appointments.js b/routes/appointments.js index b815994..2b87765 100644 --- a/routes/appointments.js +++ b/routes/appointments.js @@ -1,19 +1,56 @@ const express = require("express"); const router = express.Router(); -const { PrismaClient } = require('@prisma/client') +const { + PrismaClient +} = require('@prisma/client'); +const { json } = require("express"); const prisma = new PrismaClient() // Health check -router.get('/', async (req,res) => { +router.get('/', async (req, res) => { res.send('Server is running'); const t = 1; }); //Get all appointments. -router.get('/appointments', async (req,res) => { +router.get('/appointments', async (req, res) => { const allUsers = await prisma.appointment.findMany(); res.send(allUsers); }); +router.post('/createappointment', async (req, res) => { + + const startDate = new Date(req.query.appointmentStartDate); + const endDate = new Date(req.query.appointmentEndDate); + + const existStartDate = await prisma.appointment.findFirst({ + where: { + appointmentStartDate: startDate, + }, + }); + + if(existStartDate == null){ + const insertAppointment = await prisma.appointment.create({ + data: { + email: req.query.email, + firstName: req.query.firstName, + lastName: req.query.lastName, + cellphone: req.query.cellphone, + petName: req.query.petName, + petType: req.query.petType, + vistReason: req.query.vistReason, + appointmentEndDate: startDate, + appointmentStartDate: endDate + } + }); + console.log("Inserted Appointment"); + res.json(insertAppointment); + } + else{ + console.log("Appointment Conflicts!"); + } + +}); + module.exports = router; \ No newline at end of file