get/post requests
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
import express from "express";
|
||||
import randomUUID from "crypto";
|
||||
import dotenv from "dotenv"
|
||||
import DBAdapter from "./db/database.js";
|
||||
import { error, timeStamp } from "console";
|
||||
import { randomUUID } from "crypto";
|
||||
import DBAdapter, { getCurrentDate, setCurrentDate } from "./db/database.js";
|
||||
import { DB_INTERNAL_ERROR, DB_USER_ERROR } from "./db/database.js";
|
||||
|
||||
dotenv.config({
|
||||
path: './server/.env'
|
||||
@@ -27,15 +27,10 @@ const adapter = new DBAdapter({
|
||||
dbUserPassword: PASSWORD
|
||||
});
|
||||
|
||||
let currentDate = (new Date()).toISOString().slice(0, 10);
|
||||
|
||||
async function getCurrentDate() {
|
||||
return currentDate;
|
||||
}
|
||||
function AddDays(dateString, days) {
|
||||
let date = new Date(dateString);
|
||||
date.setDate(date.getDate() + days);
|
||||
return formatDate(date);
|
||||
return date.toISOString().slice(0, 10);
|
||||
}
|
||||
|
||||
function Random_Increase() {
|
||||
@@ -74,13 +69,23 @@ app.get('/api/products', async(req, res) => {
|
||||
|
||||
app.post('/api/current-date/advance', async (req, res) => {
|
||||
let currentDate = await getCurrentDate();
|
||||
// TODO: select orders with current date and delete them from database
|
||||
try {
|
||||
const raw_ids = await adapter.getOrdersByDate(currentDate);
|
||||
const ids = raw_ids.map(item => item.id);
|
||||
await adapter.deleteOrdersByIds(ids);
|
||||
|
||||
// TODO: get all products and increase with Random_Increase()
|
||||
|
||||
let nextDate = AddDays(currentDate, 1);
|
||||
currentDate = nextDate;
|
||||
res.json({ currentDate: nextDate });
|
||||
let nextDate = AddDays(currentDate, 1);
|
||||
await setCurrentDate(nextDate);
|
||||
res.json({ currentDate: nextDate });
|
||||
} catch (err){
|
||||
res.statusCode = 500;
|
||||
res.message = "WHOOPS";
|
||||
res.json({
|
||||
timeStamp: new Date().toISOString(),
|
||||
statusCode: 500,
|
||||
error : `${err}`
|
||||
})
|
||||
}
|
||||
});
|
||||
|
||||
app.get('/api/orders', async(req, res) => {
|
||||
@@ -120,28 +125,55 @@ app.get('/api/orders', async(req, res) => {
|
||||
|
||||
app.post('/api/orders', async (req, res) => {
|
||||
let { customerName, orderDate } = req.body;
|
||||
if (!customerName || !orderDate) {
|
||||
return res.status(400).json({ message: 'Full name and date, please!' });
|
||||
}
|
||||
let currentDate = await getCurrentDate();
|
||||
if (orderDate < currentDate) {
|
||||
return res.status(400).json({ message: 'Date of order can\'t be more, than current!' });
|
||||
}
|
||||
let id = randomUUID();
|
||||
// TODO: Insert id into orders
|
||||
|
||||
res.status(201).json({ id, customer_name: customerName, date: orderDate });
|
||||
|
||||
try {
|
||||
await adapter.addOrder({
|
||||
id,
|
||||
customer_name: customerName,
|
||||
orderDate
|
||||
});
|
||||
|
||||
res.status(201).json({
|
||||
id,
|
||||
customerName,
|
||||
orderDate
|
||||
});
|
||||
|
||||
} catch (err) {
|
||||
console.log('Error in POST /api/orders:', err);
|
||||
switch(err.type) {
|
||||
case DB_INTERNAL_ERROR:
|
||||
res.status(500).json({
|
||||
timeStamp: new Date().toISOString(),
|
||||
statusCode: 500,
|
||||
error: err.error?.message || err.message
|
||||
});
|
||||
break;
|
||||
|
||||
case DB_USER_ERROR:
|
||||
res.status(400).json({
|
||||
timeStamp: new Date().toISOString(),
|
||||
statusCode: 400,
|
||||
error: err.error?.message || err.message
|
||||
});
|
||||
break;
|
||||
|
||||
default:
|
||||
res.status(500).json({
|
||||
timeStamp: new Date().toISOString(),
|
||||
statusCode: 500,
|
||||
message: "Unknown error",
|
||||
error: err.message
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
app.use((req, res) => {
|
||||
res.status(404).json({ error: 'Invalid route' });
|
||||
});
|
||||
|
||||
app.use((err, req, res, next) => {
|
||||
console.error(err.stack);
|
||||
res.status(500).json({ error: 'Something went wrong!' });
|
||||
});
|
||||
|
||||
app.listen(3000, async() => {
|
||||
try {
|
||||
await adapter.connect();
|
||||
|
||||
Reference in New Issue
Block a user