renamed book to product + added handler products ( express )
parent
8325982227
commit
7e29c42080
@ -1,2 +1,2 @@
|
|||||||
/* Replace with your SQL commands */
|
/* Replace with your SQL commands */
|
||||||
drop TABLE books
|
drop TABLE products
|
@ -0,0 +1,63 @@
|
|||||||
|
import express, {request, Request, response, Response} from 'express'
|
||||||
|
import { Product, ProductStore } from '../models/product'
|
||||||
|
|
||||||
|
const productRoutes = (app: express.Application) => {
|
||||||
|
app.get('/products', index)
|
||||||
|
app.get('/products/:id', show)
|
||||||
|
app.post('/products', create)
|
||||||
|
app.delete('/products/:id', destroy)
|
||||||
|
app.post('/products/:id/')
|
||||||
|
}
|
||||||
|
|
||||||
|
const store = new ProductStore()
|
||||||
|
|
||||||
|
const index = async (_req: Request, res: Response) => {
|
||||||
|
const products = await store.index();
|
||||||
|
res.json(products);
|
||||||
|
}
|
||||||
|
|
||||||
|
const show = async (req: Request, res: Response) => {
|
||||||
|
const product = await store.show(req.params.id)
|
||||||
|
res.json(product)
|
||||||
|
}
|
||||||
|
|
||||||
|
const create = async (req: Request, res: Response) => {
|
||||||
|
|
||||||
|
try {
|
||||||
|
const productInfo: Product = {
|
||||||
|
title: request.body.title,
|
||||||
|
author: request.body.author,
|
||||||
|
pages: request.body.pages,
|
||||||
|
price: request.body.price
|
||||||
|
}
|
||||||
|
|
||||||
|
const newProduct = await store.create(productInfo);
|
||||||
|
res.json(newProduct)
|
||||||
|
} catch (err) {
|
||||||
|
res.status(400)
|
||||||
|
res.json(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
const update = async (req: Request, res: Response) => {
|
||||||
|
try {
|
||||||
|
const product = await store.update(req.body);
|
||||||
|
res.json({
|
||||||
|
title: request.body.title,
|
||||||
|
author: request.body.author,
|
||||||
|
pages: request.body.pages,
|
||||||
|
price: request.body.price
|
||||||
|
})
|
||||||
|
} catch (err) {
|
||||||
|
throw new Error(`Can't update product. ${err}`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const destroy = async (req: Request, res: Response) => {
|
||||||
|
const deleted = await store.delete(req.body.id)
|
||||||
|
res.json(deleted)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export default productRoutes
|
@ -1,79 +0,0 @@
|
|||||||
import { Client } from 'pg';
|
|
||||||
import client from '../database';
|
|
||||||
//import { Book } from '../types';
|
|
||||||
|
|
||||||
export type Book = {
|
|
||||||
id?: string;
|
|
||||||
title: string;
|
|
||||||
author: string;
|
|
||||||
pages: number;
|
|
||||||
price: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
export class BookStore {
|
|
||||||
async index(): Promise<Book[]> {
|
|
||||||
try {
|
|
||||||
// @ts-ignore
|
|
||||||
const conn = await client.connect()
|
|
||||||
const sql = 'SELECT * FROM books'
|
|
||||||
|
|
||||||
const result = await conn.query(sql)
|
|
||||||
conn.release()
|
|
||||||
return result.rows
|
|
||||||
} catch (err) {
|
|
||||||
throw new Error(`Cannot get any books ${err}`)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async show(id: string): Promise<Book> {
|
|
||||||
try {
|
|
||||||
const sql = 'SELECT * FROM books where id=($1)'
|
|
||||||
// @ts-ignore
|
|
||||||
const conn = await client.connect()
|
|
||||||
|
|
||||||
const result = await conn.query(sql, [id])
|
|
||||||
conn.release()
|
|
||||||
return result.rows[0]
|
|
||||||
|
|
||||||
} catch (err) {
|
|
||||||
throw new Error(`Could not find book ${id}. Error: ${err}`)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
async create(b: Book): Promise<Book> {
|
|
||||||
try {
|
|
||||||
const sql = 'INSERT INTO books (title, author, pages, price) VALUES ($1, $2, $3, $4) RETURNING *'
|
|
||||||
// @ts-ignore
|
|
||||||
const conn = await client.connect()
|
|
||||||
|
|
||||||
const result = await conn.query(sql, [b.title, b.author, b.pages, b.price])
|
|
||||||
const book = result.rows[0]
|
|
||||||
|
|
||||||
conn.release()
|
|
||||||
|
|
||||||
return book
|
|
||||||
} catch (err) {
|
|
||||||
throw new Error(`Could not add new book ${b.title}. Error: ${err}`)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
async delete(id: string): Promise<Book> {
|
|
||||||
try {
|
|
||||||
const sql = 'DELETE FROM books WHERE id=(1$)'
|
|
||||||
// @ts-ignore
|
|
||||||
const conn = await client.connect()
|
|
||||||
|
|
||||||
const result = await conn.query(sql, [id])
|
|
||||||
const book = result.rows[0]
|
|
||||||
|
|
||||||
conn.release()
|
|
||||||
|
|
||||||
return book
|
|
||||||
|
|
||||||
} catch (err) {
|
|
||||||
throw new Error(`Could not delete book ${id}. Error: ${err}`)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,92 @@
|
|||||||
|
import { Client, Connection } from 'pg';
|
||||||
|
import client from '../database';
|
||||||
|
|
||||||
|
export type Product = {
|
||||||
|
id?: string;
|
||||||
|
title: string;
|
||||||
|
author: string;
|
||||||
|
pages: number;
|
||||||
|
price: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class ProductStore {
|
||||||
|
async index(): Promise<Product[]> {
|
||||||
|
try {
|
||||||
|
// @ts-ignore
|
||||||
|
const conn = await client.connect()
|
||||||
|
const sql = 'SELECT * FROM products;'
|
||||||
|
|
||||||
|
const result = await conn.query(sql)
|
||||||
|
conn.release()
|
||||||
|
return result.rows
|
||||||
|
} catch (err) {
|
||||||
|
throw new Error(`Cannot get any product ${err}`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async show(id: string): Promise<Product> {
|
||||||
|
try {
|
||||||
|
const sql = 'SELECT * FROM products where id=($1);'
|
||||||
|
// @ts-ignore
|
||||||
|
const conn = await client.connect()
|
||||||
|
|
||||||
|
const result = await conn.query(sql, [id])
|
||||||
|
conn.release()
|
||||||
|
return result.rows[0]
|
||||||
|
|
||||||
|
} catch (err) {
|
||||||
|
throw new Error(`Could not find product ${id}. Error: ${err}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
async create(p: Product): Promise<Product> {
|
||||||
|
try {
|
||||||
|
const sql = 'INSERT INTO products (title, author, pages, price) VALUES ($1, $2, $3, $4) RETURNING *;'
|
||||||
|
// @ts-ignore
|
||||||
|
const conn = await client.connect()
|
||||||
|
|
||||||
|
const result = await conn.query(sql, [p.title, p.author, p.pages, p.price])
|
||||||
|
const product = result.rows[0]
|
||||||
|
|
||||||
|
conn.release()
|
||||||
|
|
||||||
|
return product
|
||||||
|
} catch (err) {
|
||||||
|
throw new Error(`Could not add new product ${p.title}. Error: ${err}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
async update(p: Product): Promise<Product> {
|
||||||
|
try {
|
||||||
|
const conn = await client.connect();
|
||||||
|
const result = await conn.query(
|
||||||
|
'UPDATE products SET title=$1, author=$2, pages=$3, price=$4 where id=$5 returning *;',
|
||||||
|
[p.title, p.author, p.pages, p.id]
|
||||||
|
);
|
||||||
|
conn.release()
|
||||||
|
return result.rows[0]
|
||||||
|
} catch (err) {
|
||||||
|
throw new Error(`could not update product ${p.id}. Error: ${err}`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async delete(id: string): Promise<Product> {
|
||||||
|
try {
|
||||||
|
const sql = 'DELETE FROM products WHERE id=(1$)'
|
||||||
|
// @ts-ignore
|
||||||
|
const conn = await client.connect()
|
||||||
|
|
||||||
|
const result = await conn.query(sql, [id])
|
||||||
|
const product = result.rows[0]
|
||||||
|
|
||||||
|
conn.release()
|
||||||
|
|
||||||
|
return product
|
||||||
|
|
||||||
|
} catch (err) {
|
||||||
|
throw new Error(`Could not delete product ${id}. Error: ${err}`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue