Skip to content

Commit

Permalink
fix order api please to store in db
Browse files Browse the repository at this point in the history
  • Loading branch information
M-Sufiyan10 committed May 13, 2024
1 parent 82d3ad3 commit 9d98de1
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 5 deletions.
13 changes: 13 additions & 0 deletions client/src/api/orders.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"use server"
export async function saveReservation(reservationData: any) {
const res = await fetch("http://localhost:8080/api/saveReservation", {
method: "POST",
cache: "no-store",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(reservationData),
});
const data = await res.json();
return data;
}
29 changes: 24 additions & 5 deletions client/src/app/destinations/[destination]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,33 @@
import Description from "@/components/gallery/description";
import { getDestination } from "@/api/destination";
import { saveReservation } from "@/api/orders";

const Destination = async ({ params }: { params: { destination: string } }) => {
const destination = await getDestination(params.destination);
console.log(destination);
return (
<>
<Description {...destination.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);
}
return (
<>
<Description {...destination.destination} />
</>
);
};

export default Destination;
11 changes: 11 additions & 0 deletions server/controllers/ordersController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const reservation = require('../models/ordersModel');
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 });
}
}
module.exports = {saveReservation}
17 changes: 17 additions & 0 deletions server/models/ordersModel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const mongoose=require('mongoose');
const reservation = new mongoose.Schema({
id: String,
title: String,
host: String,
price: Number,
description: String,
from: Date,
to: Date,
});

reservation.statics.saveReservation = async function (data) {
const reservation = new this.insertMany(data);
return reservation.save();
};

module.exports = mongoose.model('Reservation', reservation);
10 changes: 10 additions & 0 deletions server/routes/orderRoutes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const express = require("express");
const router = express.Router();

const {
saveReservation,
} = require("../controllers/ordersController");

// Sign in route , creating user
router.post("/saveReservation", saveReservation);
module.exports = router;

0 comments on commit 9d98de1

Please sign in to comment.