Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backend Orders API #12

Open
wants to merge 3 commits into
base: setup-backend
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions server/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const router = express.Router()
require('./routes/customers')(router)
require('./routes/orders')(router)
require('./routes/products')(router)
require('./routes/pickupStation')(router)

module.exports = router

17 changes: 12 additions & 5 deletions server/api/routes/orders.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
const {
addNewOrder,
getAllOrders,
getOrdersFromAStation,
} = require("../../controllers/orders");

module.exports = (router) => {
// GET: list of all the orders
router.get('/orders', (req, res) => {
return res.json({ message: 'GET: list of all the orders' })
})
}
// GET: list of all the orders
router.get("/orders", getAllOrders);

router.post("/orders", addNewOrder);

router.get("/orders/station", getOrdersFromAStation);
};
13 changes: 13 additions & 0 deletions server/api/routes/pickupStation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const {
AddNewStation,
getAllStations
} = require("../../controllers/pickupStations");

module.exports = (router) => {
// GET: list of all the stations
router.get("/stations", getAllStations);

router.post("/stations", AddNewStation);

};

104 changes: 104 additions & 0 deletions server/controllers/orders.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
const PickupStationList = require("../models/deliveryStations");
const OrderList = require("../models/orders");

// get all orders
const getAllOrders = async (req, res) => {
const allOrders = await OrderList.find().sort({ status: -1, createdAt: -1 });
const ordersInDetails = await Promise.all(
allOrders.map(async (order) => {
// get the customer details

// get pickup station details
const pickupStation = await PickupStationList.findById(
order.deliveryAddress
);

if (!pickupStation) {
return res
.status(400)
.json({ message: "Fail to get all orders", isSuccess: false });
}

return {
...order,
deliveryAddress: pickupStation,
};
})
);

if (ordersInDetails) {
return res.status(200).json({ data: ordersInDetails, isSucces: true });
} else {
return res
.status(400)
.json({ message: "Fail to get all orders", isSucces: false });
}
};

// get all orders by a customer

// add new order
const addNewOrder = async (req, res) => {
const {
customer,
items,
shippingAddress,
totalAmount,
currency,
deliveryAddress,
} = req.body;

// First to check if the customer exist in the database before allow to place an order

const newOrder = await OrderList.create({
customer,
items,
shippingAddress,
totalAmount,
currency,
deliveryAddress,
});

if (!newOrder) {
return res
.status(400)
.json({ message: "Opps! Fail to add new order", isSuccess: false });
}

return res.status(200).json({
message: "Congratulation your order has been placed successfully",
isSuccess: true,
});
};

// get all order from a particular stattion
const getOrdersFromAStation = async (req, res) => {
const { id } = req.query;
const allOrders = await OrderList.findOne({ deliveryAddress: id }).sort({
status: -1,
createdAt: -1,
});
const ordersInDetails = await Promise.all(
allOrders.map(async (order) => {
// get the customer details
return {
...order,
customer: null,
};
})
);

if (ordersInDetails) {
return res.status(200).json({ data: ordersInDetails, isSuccess: true });
} else {
return res
.status(400)
.json({ message: "Fail to get all orders", isSuccess: false });
}
};

module.exports = {
getOrdersFromAStation,
addNewOrder,
getAllOrders
};
46 changes: 46 additions & 0 deletions server/controllers/pickupStations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const PickupStationList = require("../models/deliveryStations");

// get all pickup stations
const getAllStations = async (req, res) => {
const stations = await PickupStationList.find();

return res.status(200).json(stations);
};

// add new pickup station
const AddNewStation = async (req, res) => {
const {
name,
region,
city,
digitalAddress,
customerService,
email,
description,
} = req.body;

const newStation = await PickupStationList.create({
name,
region,
city,
digitalAddress,
customerService,
email,
description,
});

if (!newStation) {
return res
.status(400)
.json({ message: "Failed to add new pickup station", isSuccess: false });
}

return res
.status(200)
.json({
message: `You have successfuly added ${newStation.name}, ${newStation.region} to the pickup stations `,
isSuccess: true,
});
};

module.exports = {AddNewStation, getAllStations}
17 changes: 17 additions & 0 deletions server/models/deliveryStations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const mongoose = require("mongoose");
const Schema = mongoose.Schema;

const deliveryStationSchema = new Schema(
{
name: String,
region: String,
city: String,
digitalAddress: String,
customerService: String,
email: String,
description: String,
},
{ timestamps: true }
);

module.exports = mongoose.model("PickUpStation", deliveryStationSchema);
40 changes: 40 additions & 0 deletions server/models/orders.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const mongoose = require("mongoose");
const Schema = mongoose.Schema;

const orderSchema = new Schema(
{
customer: {
type: String, // String for now but will actually be ObjectId
require: true,
},
items: [
{
productId: {
type: String, // String for now but will actually be ObjectId
},
quantity: Number,
price: Number,
},
],
shippingAddress: {
region: String,
city: String,
emai: String,
number: String,
},
totalAmount: Number,
currency: String,
status: {
type: String,
default: 'pending',
enum: ["pending", "delivered",],
},
deliveryAddress: {
type: mongoose.Schema.Types.ObjectId,
ref: "PickUpStation",
},
},
{ timestamps: true }
);

module.exports = mongoose.model("Order", orderSchema);