diff --git a/migrations/sqls/20220514223735-books-table-down.sql b/migrations/sqls/20220514223735-books-table-down.sql index 42b45ed..0e933b9 100644 --- a/migrations/sqls/20220514223735-books-table-down.sql +++ b/migrations/sqls/20220514223735-books-table-down.sql @@ -1,2 +1,2 @@ /* Replace with your SQL commands */ -drop TABLE books \ No newline at end of file +drop TABLE products \ No newline at end of file diff --git a/migrations/sqls/20220514223735-books-table-up.sql b/migrations/sqls/20220514223735-books-table-up.sql index 1ec5520..169de36 100644 --- a/migrations/sqls/20220514223735-books-table-up.sql +++ b/migrations/sqls/20220514223735-books-table-up.sql @@ -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), diff --git a/src/handlers/products.ts b/src/handlers/products.ts new file mode 100644 index 0000000..d3c8a0d --- /dev/null +++ b/src/handlers/products.ts @@ -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 \ No newline at end of file diff --git a/src/models/book.ts b/src/models/book.ts deleted file mode 100644 index 988cb29..0000000 --- a/src/models/book.ts +++ /dev/null @@ -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 { - 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 { - 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 { - 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 { - 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}`) - } - } -} diff --git a/src/models/product.ts b/src/models/product.ts new file mode 100644 index 0000000..ade0c00 --- /dev/null +++ b/src/models/product.ts @@ -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 { + 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 { + 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 { + 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 { + 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 { + 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}`) + } + } +} diff --git a/src/models/tests/book_spec.ts b/src/models/tests/product_spec.ts similarity index 80% rename from src/models/tests/book_spec.ts rename to src/models/tests/product_spec.ts index 79c7dca..e133cc1 100644 --- a/src/models/tests/book_spec.ts +++ b/src/models/tests/product_spec.ts @@ -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() diff --git a/src/server.ts b/src/server.ts index 1ee33aa..39089b2 100644 --- a/src/server.ts +++ b/src/server.ts @@ -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}`) })