|
|
@@ -1,9 +1,116 @@
|
|
|
import express from 'express';
|
|
|
+import passport from "passport";
|
|
|
+import {mongoose} from "mongoose";
|
|
|
+import Appointment from "../schemas/appointmentSchema.js";
|
|
|
+import Booking from "../schemas/bookingSchema.js";
|
|
|
+import AppointmentSchema from "../schemas/appointmentSchema.js";
|
|
|
const router = express.Router();
|
|
|
|
|
|
-/* GET home page. */
|
|
|
-router.get('/', function(req, res, next) {
|
|
|
- res.render('index', { title: 'Express' });
|
|
|
+
|
|
|
+
|
|
|
+router.use(passport.authenticate('jwt', { session: false }));
|
|
|
+
|
|
|
+router.use((req, res, next) => {
|
|
|
+ if (req.user && req.user.id) {
|
|
|
+ req.userId = req.user.id; // Pass user ID to req.userId
|
|
|
+ }
|
|
|
+ next();
|
|
|
+});
|
|
|
+
|
|
|
+
|
|
|
+router.post('/create', function(req, res, next) {
|
|
|
+ const user = req.userId;
|
|
|
+ const { title, description, dueDate, place, startDate, endDate } = req.body;
|
|
|
+
|
|
|
+ if (!title || title.length === 0 || !dueDate || !dueDate || !description || !startDate || !endDate || !place) {
|
|
|
+ res.status(400).json({ 'error': 'Empty parameter' });
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ const newTask = new Appointment({ user, title, description, dueDate, place, startDate, endDate });
|
|
|
+
|
|
|
+ newTask.save()
|
|
|
+ .then(doc => res.json({ "success": true }))
|
|
|
+ .catch(err => res.status(500).json({ 'error': err }));
|
|
|
+
|
|
|
});
|
|
|
|
|
|
+router.put('/modify', function (req, res, next) {
|
|
|
+ const user = req.userId;
|
|
|
+
|
|
|
+ const { appointmentId, title, description, dueDate, place, startDate, endDate } = req.body;
|
|
|
+ if (!title || !dueDate || !dueDate || !description || !startDate || !endDate || !place || !appointmentId) {
|
|
|
+ res.status(400).json({ 'error': 'Empty parameter' });
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ console.log(appointmentId);
|
|
|
+ console.log(user);
|
|
|
+
|
|
|
+ Appointment.updateOne(
|
|
|
+ { _id: appointmentId, user: user },
|
|
|
+ { $set: { title, description, dueDate, place, startDate, endDate } }
|
|
|
+ )
|
|
|
+ .then(result => {
|
|
|
+ if (result.modifiedCount === 1) {
|
|
|
+ console.log("Successfully updated the document.");
|
|
|
+ res.json({ "success": true });
|
|
|
+ } else {
|
|
|
+ console.log("Document not updated");
|
|
|
+ res.status(500).json({ 'error': 'Document has not changed' });
|
|
|
+ }
|
|
|
+ })
|
|
|
+ .catch(err => res.status(404).json({ 'error': 'Not found' }));
|
|
|
+});
|
|
|
+
|
|
|
+router.delete('/delete', function (req, res, next) {
|
|
|
+ const user = req.userId;
|
|
|
+ const {appointmentId} = req.body;
|
|
|
+ if (!appointmentId) {
|
|
|
+ res.status(400).json({ 'error': 'Invalid parameter' });
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ Appointment.deleteOne({ _id: appointmentId, user: user })
|
|
|
+ .then(result => {
|
|
|
+ if (result.deletedCount === 1) {
|
|
|
+ res.json({ 'success': true });
|
|
|
+ } else {
|
|
|
+ res.status(404).json({ 'error': 'Not found' });
|
|
|
+ }
|
|
|
+ })
|
|
|
+ .catch(err => res.status(500).json({ 'error': err }));
|
|
|
+});
|
|
|
+
|
|
|
+
|
|
|
+router.get('/appointments', function (req, res, next) {
|
|
|
+ const user = req.userId;
|
|
|
+ Appointment.find({ user: user })
|
|
|
+ .then(appointments => {
|
|
|
+ res.json(appointments);
|
|
|
+ })
|
|
|
+ .catch(err => {
|
|
|
+ res.status(404).json({'message': "Appointments not found"});
|
|
|
+ });
|
|
|
+});
|
|
|
+
|
|
|
+router.get('/bookings', function (req, res, next) {
|
|
|
+ const { appointmentId } = req.query;
|
|
|
+
|
|
|
+ if (!appointmentId) {
|
|
|
+ res.status(400).json({ 'error': 'Empty parameter' });
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ Booking.find({ appointment: appointmentId })
|
|
|
+ .then(tasks => {
|
|
|
+ res.json(tasks);
|
|
|
+ })
|
|
|
+ .catch(err => {
|
|
|
+ res.status(404).json({'message': "Appointment not found"});
|
|
|
+ });
|
|
|
+
|
|
|
+});
|
|
|
+
|
|
|
+
|
|
|
export { router };
|