Skip to content

Commit

Permalink
reservation api created
Browse files Browse the repository at this point in the history
  • Loading branch information
Holy-Morphism committed May 14, 2024
1 parent 2db5072 commit 940397c
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 44 deletions.
6 changes: 4 additions & 2 deletions client/src/api/orders.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
"use server"
export async function saveReservation(reservationData: any) {

export async function saveReservation(reservationData: String) {
console.log(JSON.stringify(reservationData));
const res = await fetch("http://localhost:8080/api/saveReservation", {
method: "POST",
cache: "no-store",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(reservationData),
body: JSON.stringify({id:reservationData}),
});
const data = await res.json();
return data;
Expand Down
21 changes: 3 additions & 18 deletions client/src/app/destinations/[destination]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,9 @@ import { saveReservation } from "@/api/orders";
const Destination = async ({ params }: { params: { destination: string } }) => {
const destination = await getDestination(params.destination);
console.log(destination);
const reservationData = {
id: destination.id,
title: destination.title,
host: destination.host,
price: destination.price,
description: destination.description
};
const reserve = await saveReservation(reservationData);
try {
if (reserve.ok) {
console.log('Data saved successfully');
} else {
console.error('Error saving data');
}
} catch (error)
{
console.error('Error saving data', error);
}

const reserve = await saveReservation(destination.id);
console.log(reserve);
return (
<>
<Description {...destination.destination} />
Expand Down
31 changes: 22 additions & 9 deletions server/controllers/ordersController.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
const reservation = require('../models/ordersModel');
const reservation = require("../models/ordersModel");
const Destination = require("../models/destinationModel");
const jwt = require("jsonwebtoken");

const saveReservation = async (req, res) => {
const reservations = req.body;
try {
const reservedData = await reservation.saveReservation(reservations);
res.json({ status: 'ok', reservedData });
} catch (error) {
res.json({ status: 'error', error: error.message });
const cookie = req.cookies.jwt;
const id = req.body;
try {
if (cookie) {
const decoded = jwt.verify(cookie, process.env.JWT_SECRET);
const dest = await Destination.getDestination(id);
const reservedData = await reservation.saveReservation(
dest._id,
decoded.id
);
res.json({ status: "ok", reservedData });
} else {
res.json({ status: "error", error: "User not authenticated" });
}
}
module.exports = {saveReservation}
} catch (error) {
res.json({ status: "error", error: error.message });
}
};
module.exports = { saveReservation };
33 changes: 18 additions & 15 deletions server/models/ordersModel.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
const mongoose=require('mongoose');
const reservation = new mongoose.Schema({
id: String,
title: String,
host: String,
price: Number,
description: String,
from: Date,
to: Date,
});
const mongoose = require("mongoose");

reservation.statics.saveReservation = async function (data) {
const reservation = new this.insertMany(data);
return reservation.save();
};
const Reservation = new mongoose.Schema(
{
destinationID: { type: String },
userID: { type: String, required: true, unique: true },
},
{ collection: "reservation" }
);

module.exports = mongoose.model('Reservation', reservation);
Reservation.statics.saveReservation = async function (destinationID, userID) {
if (!destinationID || !userID) {
throw("Missing IDs")
}

const reservation = await this.create(destinationID, userID);
return reservation;
};

module.exports = mongoose.model("Reservation", Reservation);

0 comments on commit 940397c

Please sign in to comment.