mirror of
https://github.com/Mueller-Patrick/Betterzon.git
synced 2026-04-26 23:30:11 +00:00
BETTERZON-38: Added products example API endpoint
This commit is contained in:
@@ -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);
|
||||
|
||||
|
||||
/**
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
import { Product } from "./product.interface";
|
||||
export interface Products {
|
||||
[key: number]: Product;
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
});
|
||||
@@ -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");
|
||||
};
|
||||
Reference in New Issue
Block a user