mirror of
https://github.com/Mueller-Patrick/Betterzon.git
synced 2024-11-22 06:13:57 +00:00
BETTERZON-38: Added products example API endpoint
This commit is contained in:
parent
00baf5939e
commit
5b04b810dc
5062
Backend/package-lock.json
generated
5062
Backend/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
|
@ -22,9 +22,10 @@
|
|||
"@types/express": "^4.17.9",
|
||||
"@types/helmet": "^4.0.0",
|
||||
"@types/node": "^14.14.9",
|
||||
"@types/webpack": "^4.41.25",
|
||||
"ts-loader": "^6.2.2",
|
||||
"typescript": "^4.1.2",
|
||||
"webpack": "^4.44.2",
|
||||
"webpack": "^5.6.0",
|
||||
"webpack-cli": "^3.3.12",
|
||||
"webpack-node-externals": "^1.7.2"
|
||||
}
|
||||
|
|
|
@ -6,6 +6,7 @@ import * as dotenv from "dotenv";
|
|||
import express from "express";
|
||||
import cors from "cors";
|
||||
import helmet from "helmet";
|
||||
import { productsRouter } from "./products/products.router";
|
||||
|
||||
dotenv.config();
|
||||
|
||||
|
@ -30,6 +31,7 @@ const app = express();
|
|||
app.use(helmet());
|
||||
app.use(cors());
|
||||
app.use(express.json());
|
||||
app.use("/products", productsRouter);
|
||||
|
||||
|
||||
/**
|
||||
|
|
14
Backend/src/products/product.interface.ts
Normal file
14
Backend/src/products/product.interface.ts
Normal file
|
@ -0,0 +1,14 @@
|
|||
export interface Product {
|
||||
product_id: number;
|
||||
asin: string;
|
||||
is_active: boolean;
|
||||
name: string;
|
||||
short_description: string;
|
||||
long_description: string;
|
||||
image_guid: string;
|
||||
date_added: Date;
|
||||
last_modified: Date;
|
||||
manufacturer_id: number;
|
||||
selling_rank: string;
|
||||
category_id: number;
|
||||
}
|
4
Backend/src/products/products.interface.ts
Normal file
4
Backend/src/products/products.interface.ts
Normal file
|
@ -0,0 +1,4 @@
|
|||
import { Product } from "./product.interface";
|
||||
export interface Products {
|
||||
[key: number]: Product;
|
||||
}
|
88
Backend/src/products/products.router.ts
Normal file
88
Backend/src/products/products.router.ts
Normal file
|
@ -0,0 +1,88 @@
|
|||
/**
|
||||
* Required External Modules and Interfaces
|
||||
*/
|
||||
|
||||
import express, {Request, Response} from 'express';
|
||||
import * as ProductService from './products.service';
|
||||
import {Product} from './product.interface';
|
||||
import {Products} from './products.interface';
|
||||
|
||||
|
||||
/**
|
||||
* Router Definition
|
||||
*/
|
||||
|
||||
export const productsRouter = express.Router();
|
||||
|
||||
|
||||
/**
|
||||
* Controller Definitions
|
||||
*/
|
||||
|
||||
// GET items/
|
||||
|
||||
productsRouter.get('/', async (req: Request, res: Response) => {
|
||||
try {
|
||||
const products: Products = await ProductService.findAll();
|
||||
|
||||
res.status(200).send(products);
|
||||
} catch (e) {
|
||||
res.status(404).send(e.message);
|
||||
}
|
||||
});
|
||||
|
||||
// GET items/:id
|
||||
|
||||
productsRouter.get('/:id', async (req: Request, res: Response) => {
|
||||
const id: number = parseInt(req.params.id, 10);
|
||||
|
||||
try {
|
||||
const product: Product = await ProductService.find(id);
|
||||
|
||||
res.status(200).send(product);
|
||||
} catch (e) {
|
||||
res.status(404).send(e.message);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
// POST items/
|
||||
|
||||
productsRouter.post('/', async (req: Request, res: Response) => {
|
||||
try {
|
||||
const product: Product = req.body.product;
|
||||
|
||||
await ProductService.create(product);
|
||||
|
||||
res.sendStatus(201);
|
||||
} catch (e) {
|
||||
res.status(404).send(e.message);
|
||||
}
|
||||
});
|
||||
|
||||
// PUT items/
|
||||
|
||||
productsRouter.put('/', async (req: Request, res: Response) => {
|
||||
try {
|
||||
const product: Product = req.body.product;
|
||||
|
||||
await ProductService.update(product);
|
||||
|
||||
res.sendStatus(200);
|
||||
} catch (e) {
|
||||
res.status(500).send(e.message);
|
||||
}
|
||||
});
|
||||
|
||||
// DELETE items/:id
|
||||
|
||||
productsRouter.delete('/:id', async (req: Request, res: Response) => {
|
||||
try {
|
||||
const id: number = parseInt(req.params.id, 10);
|
||||
await ProductService.remove(id);
|
||||
|
||||
res.sendStatus(200);
|
||||
} catch (e) {
|
||||
res.status(500).send(e.message);
|
||||
}
|
||||
});
|
103
Backend/src/products/products.service.ts
Normal file
103
Backend/src/products/products.service.ts
Normal file
|
@ -0,0 +1,103 @@
|
|||
/**
|
||||
* Data Model Interfaces
|
||||
*/
|
||||
|
||||
import { Product } from "./product.interface";
|
||||
import { Products } from "./products.interface";
|
||||
|
||||
|
||||
/**
|
||||
* In-Memory Store
|
||||
*/
|
||||
|
||||
const products: Products = {
|
||||
1: {
|
||||
product_id: 1,
|
||||
asin: "",
|
||||
is_active: true,
|
||||
name: "Burger",
|
||||
short_description: "",
|
||||
long_description: "",
|
||||
image_guid: "",
|
||||
date_added: new Date(),
|
||||
last_modified: new Date(),
|
||||
manufacturer_id: 1,
|
||||
selling_rank: "",
|
||||
category_id: 1
|
||||
},
|
||||
2: {
|
||||
product_id: 2,
|
||||
asin: "",
|
||||
is_active: true,
|
||||
name: "Pizza",
|
||||
short_description: "",
|
||||
long_description: "",
|
||||
image_guid: "",
|
||||
date_added: new Date(),
|
||||
last_modified: new Date(),
|
||||
manufacturer_id: 2,
|
||||
selling_rank: "",
|
||||
category_id: 1
|
||||
},
|
||||
3: {
|
||||
product_id: 3,
|
||||
asin: "",
|
||||
is_active: true,
|
||||
name: "Tea",
|
||||
short_description: "",
|
||||
long_description: "",
|
||||
image_guid: "",
|
||||
date_added: new Date(),
|
||||
last_modified: new Date(),
|
||||
manufacturer_id: 3,
|
||||
selling_rank: "",
|
||||
category_id: 2
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Service Methods
|
||||
*/
|
||||
|
||||
export const findAll = async (): Promise<Products> => {
|
||||
return products;
|
||||
};
|
||||
|
||||
export const find = async (id: number): Promise<Product> => {
|
||||
const record: Product = products[id];
|
||||
|
||||
if (record) {
|
||||
return record;
|
||||
}
|
||||
|
||||
throw new Error("No record found");
|
||||
};
|
||||
|
||||
export const create = async (newItem: Product): Promise<void> => {
|
||||
const product_id = new Date().valueOf();
|
||||
products[product_id] = {
|
||||
...newItem,
|
||||
product_id
|
||||
};
|
||||
};
|
||||
|
||||
export const update = async (updatedItem: Product): Promise<void> => {
|
||||
if (products[updatedItem.product_id]) {
|
||||
products[updatedItem.product_id] = updatedItem;
|
||||
return;
|
||||
}
|
||||
|
||||
throw new Error("No record found to update");
|
||||
};
|
||||
|
||||
export const remove = async (id: number): Promise<void> => {
|
||||
const record: Product = products[id];
|
||||
|
||||
if (record) {
|
||||
delete products[id];
|
||||
return;
|
||||
}
|
||||
|
||||
throw new Error("No record found to delete");
|
||||
};
|
Loading…
Reference in New Issue
Block a user