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

Reconstruction2023 #225

2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
node_modules/
.pnp/
.pnp.js
back-end/package-lock.json
front-end/package-lock.json

# testing
coverage/
Expand Down
7 changes: 7 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"singleQuote": false,
"tabWidth": 4,
"useTabs": true,
"semi": true,
"trailingComma": "all"
}
1 change: 1 addition & 0 deletions back-end/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
/node_modules
/.pnp
.pnp.js
/package-lock.json

# testing
/coverage
Expand Down
18,749 changes: 0 additions & 18,749 deletions back-end/package-lock.json

This file was deleted.

18 changes: 10 additions & 8 deletions back-end/src/app.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
// Config
const path = require("path");

require("dotenv").config({ path: path.join(__dirname, "..", ".env") });

// External modules
const express = require("express");
const cors = require("cors");

// Internal modules
const errorHandler = require("./errors/errorHandler");
const notFound = require("./errors/notFound");
const reservationsRouter = require("./reservations/reservations.router");

const tablesRouter = require("./tables/tables.router");
// Define app
const app = express();

// App middleware
app.use(cors());
app.use(express.json());

// Define routes
app.use("/reservations", reservationsRouter);

app.use("/tables", tablesRouter);
// Handle errors
app.use(notFound);
app.use(errorHandler);

// Export
module.exports = app;
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
exports.up = function (knex) {
return knex.schema.createTable("reservations", (table) => {
table.increments("reservation_id").primary();
table.timestamps(true, true);
});
return knex.schema.createTable("reservations", (table) => {
table.increments("reservation_id").primary();
table.string("first_name");
table.string("last_name");
table.string("mobile_number");
table.date("reservation_date");
table.time("reservation_time");
table.integer("people");
table.timestamps(true, true);
});
};

exports.down = function (knex) {
return knex.schema.dropTable("reservations");
return knex.schema.dropTable("reservations");
};
12 changes: 12 additions & 0 deletions back-end/src/db/migrations/20231226131225_createTablesTable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
exports.up = function (knex) {
return knex.schema.createTable("tables", (table) => {
table.increments("table_id").primary();
table.string("table_name");
table.integer("capacity");
table.timestamps(true, true);
});
};

exports.down = function (knex) {
return knex.schema.dropTable("tables");
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
exports.up = function (knex) {
return knex.schema.table("tables", (table) => {
table
.integer("reservation_id")
.references("reservation_id")
.inTable("reservations")
.onDelete("cascade");
});
};

exports.down = function (knex) {
return knex.schema.table("tables", (table) => {
table.dropColumn("reservation_id");
});
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
exports.up = function (knex) {
return knex.schema.table("reservations", (table) => {
table.string("status").defaultTo("booked");
});
};

exports.down = function (knex) {
return knex.schema.table("reservations", (table) => {
table.dropColumn("status");
});
};
8 changes: 7 additions & 1 deletion back-end/src/db/seeds/00-reservations.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
const reservations = require("./00-reservations.json");

exports.seed = function (knex) {
return knex.raw("TRUNCATE TABLE reservations RESTART IDENTITY CASCADE");
return knex
.raw("TRUNCATE TABLE reservations RESTART IDENTITY CASCADE")
.then(() => {
return knex("reservations").insert(reservations);
});
};
100 changes: 50 additions & 50 deletions back-end/src/db/seeds/00-reservations.json
Original file line number Diff line number Diff line change
@@ -1,52 +1,52 @@
[
{
"first_name": "Rick",
"last_name": "Sanchez",
"mobile_number": "202-555-0164",
"reservation_date": "2020-12-31",
"reservation_time": "20:00:00",
"people": 6,
"created_at": "2020-12-10T08:30:32.326Z",
"updated_at": "2020-12-10T08:30:32.326Z"
},
{
"first_name": "Frank",
"last_name": "Palicky",
"mobile_number": "202-555-0153",
"reservation_date": "2020-12-30",
"reservation_time": "20:00",
"people": 1,
"created_at": "2020-12-10T08:31:32.326Z",
"updated_at": "2020-12-10T08:31:32.326Z"
},
{
"first_name": "Bird",
"last_name": "Person",
"mobile_number": "808-555-0141",
"reservation_date": "2020-12-30",
"reservation_time": "18:00",
"people": 1,
"created_at": "2020-12-10T08:31:32.326Z",
"updated_at": "2020-12-10T08:31:32.326Z"
},
{
"first_name": "Tiger",
"last_name": "Lion",
"mobile_number": "808-555-0140",
"reservation_date": "2025-12-30",
"reservation_time": "18:00",
"people": 3,
"created_at": "2020-12-10T08:31:32.326Z",
"updated_at": "2020-12-10T08:31:32.326Z"
},
{
"first_name": "Anthony",
"last_name": "Charboneau",
"mobile_number": "620-646-8897",
"reservation_date": "2026-12-30",
"reservation_time": "18:00",
"people": 2,
"created_at": "2020-12-10T08:31:32.326Z",
"updated_at": "2020-12-10T08:31:32.326Z"
}
{
"first_name": "Rick",
"last_name": "Sanchez",
"mobile_number": "202-555-0164",
"reservation_date": "2020-12-31",
"reservation_time": "20:00:00",
"people": 6,
"created_at": "2020-12-10T08:30:32.326Z",
"updated_at": "2020-12-10T08:30:32.326Z"
},
{
"first_name": "Frank",
"last_name": "Palicky",
"mobile_number": "202-555-0153",
"reservation_date": "2020-12-30",
"reservation_time": "20:00",
"people": 1,
"created_at": "2020-12-10T08:31:32.326Z",
"updated_at": "2020-12-10T08:31:32.326Z"
},
{
"first_name": "Bird",
"last_name": "Person",
"mobile_number": "808-555-0141",
"reservation_date": "2020-12-30",
"reservation_time": "18:00",
"people": 1,
"created_at": "2020-12-10T08:31:32.326Z",
"updated_at": "2020-12-10T08:31:32.326Z"
},
{
"first_name": "Tiger",
"last_name": "Lion",
"mobile_number": "808-555-0140",
"reservation_date": "2025-12-30",
"reservation_time": "18:00",
"people": 3,
"created_at": "2020-12-10T08:31:32.326Z",
"updated_at": "2020-12-10T08:31:32.326Z"
},
{
"first_name": "Anthony",
"last_name": "Charboneau",
"mobile_number": "620-646-8897",
"reservation_date": "2026-12-30",
"reservation_time": "18:00",
"people": 2,
"created_at": "2020-12-10T08:31:32.326Z",
"updated_at": "2020-12-10T08:31:32.326Z"
}
]
9 changes: 9 additions & 0 deletions back-end/src/db/seeds/01-tables.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const tables = require("./01-tables.json");

exports.seed = function (knex) {
return knex
.raw("TRUNCATE TABLE tables RESTART IDENTITY CASCADE")
.then(() => {
return knex("tables").insert(tables);
});
};
18 changes: 18 additions & 0 deletions back-end/src/db/seeds/01-tables.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[
{
"table_name": "#1",
"capacity": 8
},
{
"table_name": "#2",
"capacity": 6
},
{
"table_name": "Bar #1",
"capacity": 1
},
{
"table_name": "Bar #2",
"capacity": 1
}
]
4 changes: 2 additions & 2 deletions back-end/src/errors/errorHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
* Express API error handler.
*/
function errorHandler(error, request, response, next) {
const { status = 500, message = "Something went wrong!" } = error;
response.status(status).json({ error: message });
const { status = 500, message = "Something went wrong!" } = error;
response.status(status).json({ error: message });
}

module.exports = errorHandler;
11 changes: 11 additions & 0 deletions back-end/src/errors/methodNotAllowed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Express API method not yet defined
*/
function methodNotAllowed(req, res, next) {
next({
status: 405,
message: `${req.method} not allowed for ${req.originalUrl}`,
});
}

module.exports = methodNotAllowed;
2 changes: 1 addition & 1 deletion back-end/src/errors/notFound.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Express API "Not found" handler.
*/
function notFound(req, res, next) {
next({ status: 404, message: `Path not found: ${req.originalUrl}` });
next({ status: 404, message: `Path not found: ${req.originalUrl}` });
}

module.exports = notFound;
76 changes: 70 additions & 6 deletions back-end/src/reservations/reservations.controller.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,76 @@
const service = require("./reservations.service");
const validation = require("./reservations.validation");

/**
* List handler for reservation resources
* Handlers for reservation resources
*/
async function list(req, res) {
res.json({
data: [],
});
const date = req.query.date;
const mobile_number = req.query.mobile_number;
let data = [];
// if mobile number query, filter by mobile number
// if data query, filter by date
// else, return all unfiltered
if (mobile_number) {
data = await service.search(mobile_number);
} else if (date) {
data = await service.listDate(date);
} else {
data = await service.list();
}
// return result
res.json({ data });
}

async function create(req, res) {
// find validated reservation in locals
const { validReservation } = res.locals;
// call API and return response
const data = await service.create(validReservation);
res.status(201).json({ data });
}
function read(req, res) {
// find existing reservation in locals and return
const { foundReservation } = res.locals;
res.json({ data: foundReservation });
}
async function update(req, res) {
const data = await service.update(
res.locals.validReservation,
res.locals.foundReservation.reservation_id,
);
res.json({ data });
}
/**
* Update status handler for reservation resources
*/
async function updateStatus(req, res) {
const { foundReservation, validStatus } = res.locals;
// set reservation status to validated status
foundReservation.status = validStatus;
const data = await service.update(
foundReservation,
foundReservation.reservation_id,
);
res.json({ data });
}
/**
* Return with required validation in order
*/
module.exports = {
list,
list,
create: [validation.isValidReservation, validation.isStatusBooked, create],
read: [validation.reservationExists, read],
update: [
validation.reservationExists,
validation.isValidReservation,
validation.statusIsFinished,
update,
],
updateStatus: [
validation.reservationExists,
validation.reservationIsFinished,
validation.statusIsFinished,
validation.isValidStatus,
updateStatus,
],
};
Loading