small fixes

Vic
Vic 2 years ago
parent ec732ace8b
commit 56441dac01

@ -1,17 +1,21 @@
{
"name": "storefront_backend",
"name": "Shelf_backend",
"version": "0.1.0",
"description": "",
"main": "server.ts",
"scripts": {
"start": "nodemon src/server.ts",
"dev": "db-migrate --env dev up && nodemon src/server.ts",
"watch": "tsc-watch --esModuleInterop src/server.ts --outDir ./dist --onSuccess \"node ./dist/server.js\"",
"test": "db-migrate --env test up && ENV=test jasmine-ts --config jasmine.json && db-migrate --env test down",
"build": "npx tsc",
"up": "db-migrate up",
"down": "db-migrate down",
"lint": "eslint --ext .ts",
"prettier": "prettier \"src/**/*.ts\" --write",
"tsc": "tsc"
},
"author": "AnisB",
"license": "ISC",
"dependencies": {

@ -4,28 +4,43 @@ 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.post('/products/create', create)
app.put('/products/:id', update)
app.delete('/products/:id', destroy)
}
const store = new ProductStore()
const index = async (_req: Request, res: Response) => {
const index = async (req: Request, res: Response) => {
try {
const products = await store.index();
res.json(products);
console.log('working products index')
} catch (err) {
console.log('error products index')
res.status(400)
res.json(err)
}
}
const show = async (req: Request, res: Response) => {
const product = await store.show(req.params.id)
try {
const product = await store.show(parseInt(req.params.id))
res.json(product)
} catch (err) {
res.status(400)
res.json(err)
}
}
const create = async (req: Request, res: Response) => {
try {
const productInfo: Product = {
name: request.body.name,
price: request.body.price,
name: req.body.name,
price: req.body.price,
}
const newProduct = await store.create(productInfo);
@ -41,17 +56,24 @@ const update = async (req: Request, res: Response) => {
try {
const product = await store.update(req.body);
res.json({
name: request.body.name,
price: request.body.price,
name: req.body.name,
price: req.body.price,
})
} catch (err) {
throw new Error(`Can't update product. ${err}`)
res.status(400)
res.json(err)
}
}
const destroy = async (req: Request, res: Response) => {
const deleted = await store.delete(req.body.id)
res.json(deleted)
try {
const deleted = await store.delete(req.body.id)
res.json(deleted)
} catch (err) {
res.status(400)
res.json(err)
}
}

@ -1,5 +1,6 @@
import express, { Request, Response } from 'express'
import bodyParser from 'body-parser'
import productRoutes from './handlers/products'
const app: express.Application = express()
@ -20,4 +21,6 @@ app.listen(port, function () {
console.log(`starting app on: ${address}`)
})
export default app;

@ -0,0 +1 @@
import sueprtest from "supertest"

@ -1,4 +1,4 @@
import { Product, ProductStore } from '../product'
import { Product, ProductStore } from '../../models/product'
const store = new ProductStore()
Loading…
Cancel
Save