|
|
|
@ -2,10 +2,8 @@ import { Client, Connection } from 'pg';
|
|
|
|
|
import client from '../database';
|
|
|
|
|
|
|
|
|
|
export type Product = {
|
|
|
|
|
id?: string;
|
|
|
|
|
title: string;
|
|
|
|
|
author: string;
|
|
|
|
|
pages: number;
|
|
|
|
|
id?: number;
|
|
|
|
|
name: string;
|
|
|
|
|
price: number;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -20,7 +18,7 @@ export class ProductStore {
|
|
|
|
|
conn.release()
|
|
|
|
|
return result.rows
|
|
|
|
|
} catch (err) {
|
|
|
|
|
throw new Error(`Cannot get any product ${err}`)
|
|
|
|
|
throw new Error(`Cannot get products ${err}`)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -42,18 +40,18 @@ export class ProductStore {
|
|
|
|
|
|
|
|
|
|
async create(p: Product): Promise<Product> {
|
|
|
|
|
try {
|
|
|
|
|
const sql = 'INSERT INTO products (title, author, pages, price) VALUES ($1, $2, $3, $4) RETURNING *;'
|
|
|
|
|
const sql = 'INSERT INTO products (name, price) VALUES ($1, $2) RETURNING *;'
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
const conn = await client.connect()
|
|
|
|
|
|
|
|
|
|
const result = await conn.query(sql, [p.title, p.author, p.pages, p.price])
|
|
|
|
|
const result = await conn.query(sql, [p.name, 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}`)
|
|
|
|
|
throw new Error(`Could not add new product ${p.name}. Error: ${err}`)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
@ -62,8 +60,8 @@ export class ProductStore {
|
|
|
|
|
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]
|
|
|
|
|
'UPDATE products SET name=$1, price=$2 WHERE id=$3 returning *;',
|
|
|
|
|
[p.name, p.price, p.id]
|
|
|
|
|
);
|
|
|
|
|
conn.release()
|
|
|
|
|
return result.rows[0]
|
|
|
|
@ -72,7 +70,7 @@ export class ProductStore {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async delete(id: string): Promise<Product> {
|
|
|
|
|
async delete(id: number): Promise<Product> {
|
|
|
|
|
try {
|
|
|
|
|
const sql = 'DELETE FROM products WHERE id=(1$)'
|
|
|
|
|
// @ts-ignore
|
|
|
|
|