diff --git a/migrations/sqls/20220520025053-shelf-up.sql b/migrations/sqls/20220520025053-shelf-up.sql index 6574e63..5e4f55f 100644 --- a/migrations/sqls/20220520025053-shelf-up.sql +++ b/migrations/sqls/20220520025053-shelf-up.sql @@ -11,4 +11,19 @@ CREATE TABLE users ( lastName VARCHAR(250) NOT NULL, username VARCHAR(250) NOT NULL, password_digest VARCHAR(250) NOT NULL -); \ No newline at end of file +); + +CREATE TABLE orders ( + id SERIAL PRIMARY KEY, + status VARCHAR(15), + user_id bigint REFERENCES users(id) +); + +CREATE TABLE order_products ( + id SERIAL PRIMARY KEY, + quantity INTEGER NOT NULL, + order_id bigint REFERENCES orders(id), + product_id bigint REFERENCES products(id) +) + + diff --git a/src/handlers/orders.ts b/src/handlers/orders.ts new file mode 100644 index 0000000..bb74b57 --- /dev/null +++ b/src/handlers/orders.ts @@ -0,0 +1,66 @@ +import express, { Request, Response } from 'express' +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.post('/orders', create) + app.post('/orders/:id/products', addProduct) +} + +const store = new OrderStore() + +const index = async (req: Request, res: Response) => { + try { + const orders = await store.index() + res.json(orders) + } catch (err) { + res.status(400) + res.json(err) + } +} + +const read = async (req: Request, res: Response) => { + try { + const order = await store.read(req.params.id) + res.json(order) + } catch (err) { + res.status(400) + res.json(err) + } + +} + +const create = async (req: Request, res: Response) => { + try { + const orderInfo: Order = { + status: req.body.status, + userId: req.body.userId + } + + const newOrder = await store.create(orderInfo) + res.json(newOrder) + } catch (err) { + res.status(400) + res.json(err) + } +} + +const addProduct = async (req: Request, res: Response) => { + try { + const orderProductInfo: OrderProduct = { + quantity: parseInt(req.body.quantity), + orderId: parseInt(req.params.id), + productId: req.body.productId + } + const addedProduct = await store.addProduct(orderProductInfo) + res.json(addedProduct) + } catch (err) { + res.status(400) + res.json(err) + } +} + + +export default orderRoutes \ No newline at end of file diff --git a/src/handlers/products.ts b/src/handlers/products.ts index 5e8fdde..0aef9f6 100644 --- a/src/handlers/products.ts +++ b/src/handlers/products.ts @@ -15,10 +15,8 @@ const store = new ProductStore() const index = async (req: Request, res: Response) => { try { const products = await store.index() - res.json(products); - console.log('working products index') + res.json(products) } catch (err) { - console.log('error products index') res.status(400) res.json(err) } diff --git a/src/handlers/users.ts b/src/handlers/users.ts index f43682e..fd2aa1b 100644 --- a/src/handlers/users.ts +++ b/src/handlers/users.ts @@ -1,4 +1,4 @@ -import express, { Request, Response, NextFunction } from 'express' +import express, { Request, Response } from 'express' import { User, UserStore } from '../models/user' import { verifyAuthToken, verifyUserToken} from './utils' @@ -77,7 +77,7 @@ const update = async (req: Request, res: Response) => { const destroy = async (req: Request, res: Response) => { try { - const deleted = await store.delete(req.body.id) + const deleted = await store.delete(parseInt(req.params.id)) res.json(deleted) } catch (err) { res.status(400) diff --git a/src/handlers/utils.ts b/src/handlers/utils.ts index 94f39de..6d0aca5 100644 --- a/src/handlers/utils.ts +++ b/src/handlers/utils.ts @@ -1,6 +1,6 @@ -import jwt, {Secret} from "jsonwebtoken" -import {User} from "../models/user" -import {NextFunction, Request, Response} from "express" +import jwt, { Secret } from "jsonwebtoken" +import { User } from "../models/user" +import { NextFunction, Request, Response } from "express" const SECRET = process.env.TOKEN_SECRET as Secret diff --git a/src/models/order.ts b/src/models/order.ts new file mode 100644 index 0000000..a25e243 --- /dev/null +++ b/src/models/order.ts @@ -0,0 +1,89 @@ +import client from '../database' + +export type Order = { + status: string + userId: number +} + +export type OrderProduct = { + quantity: number + orderId: number + productId: number +} + + +export class OrderStore { + async index(): Promise { + try { + + const conn = await client.connect() + const sql = 'SELECT * FROM orders' + + const result = await conn.query(sql) + const orders = result.rows + + conn.release() + + return orders + + } catch (err) { + throw new Error(`Cannot get any order ${err}`) + } + } + + async read(userId: string): Promise { + try { + + const conn = await client.connect() + const sql = 'SELECT * FROM orders WHERE user_id=($1)' + + const result = await conn.query(sql, [userId]) + const order = result.rows[0] + + conn.release() + + return order + + } catch (err) { + throw new Error(`Could not find order ${userId}. Error: ${err}`) + } + } + + async create(o: Order): Promise { + try { + + const conn = await client.connect() + const sql = 'INSERT INTO orders (status, user_id) VALUES ($1, $2) RETURNING *' + + const result = await conn.query(sql, [o.status, o.userId]) + const order = result.rows[0] + + conn.release() + + return order + + } catch (err) { + throw new Error(`Could not add a new order ${o.userId}. Error: ${err}`) + } + } + + async addProduct(o: OrderProduct): Promise { + try { + + const conn = await client.connect() + const sql = 'INSERT INTO order_products (quantity, order_id, product_id) VALUES ($1, $2) RETURNING *' + + const result = await conn.query(sql, [o.quantity, o.orderId, o.productId]) + const order = result.rows[0] + + conn.release() + + return order + + } catch (err) { + throw new Error(`Could not add a new orderProducts ${o.orderId}. Error: ${err}`) + } + } + + +} \ No newline at end of file diff --git a/src/models/product.ts b/src/models/product.ts index cf49392..4eb9938 100644 --- a/src/models/product.ts +++ b/src/models/product.ts @@ -29,7 +29,7 @@ export class ProductStore { try { const conn = await client.connect() - const sql = 'SELECT * FROM products where id=($1)' + const sql = 'SELECT * FROM products WHERE id=($1)' const result = await conn.query(sql, [id]) const product = result.rows[0]