updated README.md + edited orders + changed migrations

master
Vic 2 years ago
parent b5b447f1b9
commit 57493d4abd

@ -34,7 +34,7 @@ TOKEN_SECRET_TEST=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjp7ImlkIjoxLCJma
- Install packages dependencies: `npm install`
- Run tests: `npm run test`
- Run the database: `npm run up`
- Launch the application: `npm run start`
- Launch the application: `npm run start`, you can acces the application with this link `127.0.0.1:3000`
Note : If needed you can reset the tables by closing the app and running the following commands:
@ -74,6 +74,44 @@ Note : If needed you can reset the tables by closing the app and running the fol
| Post | `/orders/:id/products` | Create ( Token ) |
| Delete | `/orders/:id/products` | Delete ( Token ) |
# Data shapes
- Products :
| Column | Type |
|:-------:|:-----------------------|
| id | SERIAL PRIMARY KEY |
| name | VARCHAR(250) NOT NULL |
| price | INTEGER NOT NULL |
- Users :
| Column | Type |
|:------------:|:-----------------------|
| id | SERIAL PRIMARY KEY |
| firstName | VARCHAR(250) NOT NULL |
| lastName | VARCHAR(250) NOT NULL |
| username | VARCHAR(250) NOT NULL |
| password | VARCHAR(250) NOT NULL |
- Orders :
| Column | Type |
|:------------:|:---------------------------------------|
| id | SERIAL PRIMARY KEY |
| status | VARCHAR(15) |
| user_id | INTEGER NOT NULL REFERENCES users(id) |
- Orders Products :
| Column | Type |
|:-------------:|:------------------------------------------|
| id | SERIAL PRIMARY KEY |
| quantity | INTEGER NOT NULL, |
| order_id | INTEGER NOT NULL REFERENCES orders(id) |
| product_id | INTEGER NOT NULL REFERENCES products(id) |
# Built with

@ -0,0 +1,53 @@
'use strict';
var dbm;
var type;
var seed;
var fs = require('fs');
var path = require('path');
var Promise;
/**
* We receive the dbmigrate dependency from dbmigrate initially.
* This enables us to not have to rely on NODE_PATH.
*/
exports.setup = function(options, seedLink) {
dbm = options.dbmigrate;
type = dbm.dataType;
seed = seedLink;
Promise = options.Promise;
};
exports.up = function(db) {
var filePath = path.join(__dirname, 'sqls', '20220525184648-products-up.sql');
return new Promise( function( resolve, reject ) {
fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){
if (err) return reject(err);
console.log('received data: ' + data);
resolve(data);
});
})
.then(function(data) {
return db.runSql(data);
});
};
exports.down = function(db) {
var filePath = path.join(__dirname, 'sqls', '20220525184648-products-down.sql');
return new Promise( function( resolve, reject ) {
fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){
if (err) return reject(err);
console.log('received data: ' + data);
resolve(data);
});
})
.then(function(data) {
return db.runSql(data);
});
};
exports._meta = {
"version": 1
};

@ -19,7 +19,7 @@ exports.setup = function(options, seedLink) {
};
exports.up = function(db) {
var filePath = path.join(__dirname, 'sqls', '20220520025053-shelf-up.sql');
var filePath = path.join(__dirname, 'sqls', '20220525184653-users-up.sql');
return new Promise( function( resolve, reject ) {
fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){
if (err) return reject(err);
@ -34,7 +34,7 @@ exports.up = function(db) {
};
exports.down = function(db) {
var filePath = path.join(__dirname, 'sqls', '20220520025053-shelf-down.sql');
var filePath = path.join(__dirname, 'sqls', '20220525184653-users-down.sql');
return new Promise( function( resolve, reject ) {
fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){
if (err) return reject(err);

@ -0,0 +1,53 @@
'use strict';
var dbm;
var type;
var seed;
var fs = require('fs');
var path = require('path');
var Promise;
/**
* We receive the dbmigrate dependency from dbmigrate initially.
* This enables us to not have to rely on NODE_PATH.
*/
exports.setup = function(options, seedLink) {
dbm = options.dbmigrate;
type = dbm.dataType;
seed = seedLink;
Promise = options.Promise;
};
exports.up = function(db) {
var filePath = path.join(__dirname, 'sqls', '20220525184702-orders-up.sql');
return new Promise( function( resolve, reject ) {
fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){
if (err) return reject(err);
console.log('received data: ' + data);
resolve(data);
});
})
.then(function(data) {
return db.runSql(data);
});
};
exports.down = function(db) {
var filePath = path.join(__dirname, 'sqls', '20220525184702-orders-down.sql');
return new Promise( function( resolve, reject ) {
fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){
if (err) return reject(err);
console.log('received data: ' + data);
resolve(data);
});
})
.then(function(data) {
return db.runSql(data);
});
};
exports._meta = {
"version": 1
};

@ -0,0 +1,53 @@
'use strict';
var dbm;
var type;
var seed;
var fs = require('fs');
var path = require('path');
var Promise;
/**
* We receive the dbmigrate dependency from dbmigrate initially.
* This enables us to not have to rely on NODE_PATH.
*/
exports.setup = function(options, seedLink) {
dbm = options.dbmigrate;
type = dbm.dataType;
seed = seedLink;
Promise = options.Promise;
};
exports.up = function(db) {
var filePath = path.join(__dirname, 'sqls', '20220525184708-order-products-up.sql');
return new Promise( function( resolve, reject ) {
fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){
if (err) return reject(err);
console.log('received data: ' + data);
resolve(data);
});
})
.then(function(data) {
return db.runSql(data);
});
};
exports.down = function(db) {
var filePath = path.join(__dirname, 'sqls', '20220525184708-order-products-down.sql');
return new Promise( function( resolve, reject ) {
fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){
if (err) return reject(err);
console.log('received data: ' + data);
resolve(data);
});
})
.then(function(data) {
return db.runSql(data);
});
};
exports._meta = {
"version": 1
};

@ -1,4 +0,0 @@
DROP TABLE order_products;
DROP TABLE orders;
DROP TABLE users;
DROP TABLE products;

@ -1,29 +0,0 @@
CREATE TABLE products (
id SERIAL PRIMARY KEY,
name VARCHAR(250) NOT NULL,
price INTEGER NOT NULL
);
CREATE TABLE users (
id SERIAL PRIMARY KEY,
firstName VARCHAR(250) NOT NULL,
lastName VARCHAR(250) NOT NULL,
username VARCHAR(250) NOT NULL,
password VARCHAR(250) NOT NULL
);
CREATE TABLE orders (
id SERIAL PRIMARY KEY,
status VARCHAR(15),
user_id INTEGER NOT NULL REFERENCES users(id)
);
CREATE TABLE order_products (
id SERIAL PRIMARY KEY,
quantity INTEGER NOT NULL,
order_id INTEGER NOT NULL REFERENCES orders(id),
product_id INTEGER NOT NULL REFERENCES products(id)
)

@ -0,0 +1,5 @@
CREATE TABLE products (
id SERIAL PRIMARY KEY,
name VARCHAR(250) NOT NULL,
price INTEGER NOT NULL
);

@ -0,0 +1,7 @@
CREATE TABLE users (
id SERIAL PRIMARY KEY,
firstName VARCHAR(250) NOT NULL,
lastName VARCHAR(250) NOT NULL,
username VARCHAR(250) NOT NULL,
password VARCHAR(250) NOT NULL
);

@ -0,0 +1,5 @@
CREATE TABLE orders (
id SERIAL PRIMARY KEY,
status VARCHAR(15),
user_id INTEGER NOT NULL REFERENCES users(id)
);

@ -0,0 +1,6 @@
CREATE TABLE order_products (
id SERIAL PRIMARY KEY,
quantity INTEGER NOT NULL,
order_id INTEGER NOT NULL REFERENCES orders(id),
product_id INTEGER NOT NULL REFERENCES products(id)
)

@ -7,10 +7,10 @@
"start": "node dist/server.js",
"dev": "nodemon src/server.ts",
"watch": "tsc-watch --esModuleInterop src/server.ts --outDir ./dist --onSuccess \"node ./dist/server.js\"",
"test": "db-migrate --env test up && ENV=test jasmine-ts --config jasmine.json && db-migrate --env test down",
"test": "db-migrate --env test up && ENV=test jasmine-ts --config jasmine.json && db-migrate reset --env test",
"build": "npx tsc",
"up": "db-migrate up",
"down": "db-migrate down",
"down": "db-migrate reset",
"lint": "eslint --ext .ts",
"prettier": "prettier \"src/**/*.ts\" --write",
"tsc": "tsc"

@ -3,8 +3,8 @@ import { Order, OrderProduct, OrderStore } from "../models/order";
import { verifyAuthToken } from "./utils";
const orderRoutes = (app: express.Application) => {
app.get("/orders", index);
app.get("/orders/:id", read);
app.get("/orders", verifyAuthToken, index);
app.get("/orders/:id", verifyAuthToken, read);
app.post("/orders", verifyAuthToken, create);
app.post("/orders/:id/products", verifyAuthToken, addProduct);
app.delete("/orders/:id/products", verifyAuthToken, deleteProduct);

@ -56,13 +56,17 @@ describe("Order handler", () => {
});
it("Should index orders", async () => {
const response = await request.get("/orders");
const response = await request
.get("/orders")
.auth(token, { type: "bearer" });
expect(response.status).toBe(200);
});
it("Should get order by id", async () => {
const response = await request.get("/orders/2");
const response = await request
.get("/orders/2")
.auth(token, { type: "bearer" });
expect(response.status).toBe(200);
});

Loading…
Cancel
Save