fixes + added order model + order handler

Vic
Vic 2 years ago
parent 54fa9ac7fa
commit 49ec6577f9

@ -11,4 +11,19 @@ CREATE TABLE users (
lastName VARCHAR(250) NOT NULL,
username VARCHAR(250) NOT NULL,
password_digest VARCHAR(250) NOT NULL
);
);
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)
)

@ -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

@ -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)
}

@ -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)

@ -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

@ -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<Order[]> {
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<Order> {
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<Order> {
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<Order> {
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}`)
}
}
}

@ -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]

Loading…
Cancel
Save