renamed book to product + added handler products ( express )
This commit is contained in:
parent
8325982227
commit
7e29c42080
@ -1,2 +1,2 @@
|
||||
/* Replace with your SQL commands */
|
||||
drop TABLE books
|
||||
drop TABLE products
|
@ -1,5 +1,5 @@
|
||||
/* Replace with your SQL commands */
|
||||
CREATE TABLE books (
|
||||
CREATE TABLE products (
|
||||
id SERIAL PRIMARY KEY,
|
||||
title VARCHAR(150),
|
||||
author VARCHAR(255),
|
||||
|
63
src/handlers/products.ts
Normal file
63
src/handlers/products.ts
Normal file
@ -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}`)
|
||||
}
|
||||
}
|
||||
}
|
92
src/models/product.ts
Normal file
92
src/models/product.ts
Normal file
@ -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}`)
|
||||
}
|
||||
}
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
import { Book, BookStore } from '../book'
|
||||
import { Product, ProductStore } from '../product'
|
||||
|
||||
const store = new BookStore()
|
||||
const store = new ProductStore()
|
||||
|
||||
describe("Book store Model", () => {
|
||||
it('should have an index method', () => {
|
||||
@ -20,7 +20,7 @@ describe("Book store Model", () => {
|
||||
expect(store.index).toBeDefined;
|
||||
});
|
||||
|
||||
it('create method should add a book', async () => {
|
||||
it('create method should add a product', async () => {
|
||||
const result = await store.create({
|
||||
title: '1984',
|
||||
author: 'George Orwell',
|
||||
@ -37,7 +37,7 @@ describe("Book store Model", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it('index method should return a list of books', async () => {
|
||||
it('index method should return a list of products', async () => {
|
||||
const result = await store.index();
|
||||
expect(result).toEqual([{
|
||||
id: "1",
|
||||
@ -49,7 +49,7 @@ describe("Book store Model", () => {
|
||||
}]);
|
||||
});
|
||||
|
||||
it('show method should return a book', async () => {
|
||||
it('show method should return a product', async () => {
|
||||
const result = await store.show("1");
|
||||
expect(result).toEqual({
|
||||
id: "1",
|
||||
@ -60,7 +60,7 @@ describe("Book store Model", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it('delete method should remove the book', async () => {
|
||||
it('delete method should remove the product', async () => {
|
||||
store.delete("1");
|
||||
const result = await store.index()
|
||||
|
@ -1,16 +1,22 @@
|
||||
import express, { Request, Response } from 'express'
|
||||
import bodyParser from 'body-parser'
|
||||
import productRoutes from './handlers/products'
|
||||
|
||||
const app: express.Application = express()
|
||||
const address: string = "0.0.0.0:3000"
|
||||
const port = 3000;
|
||||
|
||||
app.use(bodyParser.json())
|
||||
|
||||
app.get('/', function (req: Request, res: Response) {
|
||||
res.send('Hello World!')
|
||||
res.send('Main API')
|
||||
})
|
||||
|
||||
app.listen(3000, function () {
|
||||
productRoutes(app)
|
||||
|
||||
|
||||
// Start express server
|
||||
app.listen(port, function () {
|
||||
console.log(`starting app on: ${address}`)
|
||||
})
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user