Commit 30c55e7f authored by Pavel Mishakov's avatar Pavel Mishakov

lesson 85 done

parent 3468fa69
This diff is collapsed.
......@@ -13,6 +13,8 @@
"cors": "^2.8.5",
"dotenv": "^16.0.3",
"express": "^4.18.2",
"multer": "^1.4.5-lts.1",
"nanoid": "^4.0.1",
"pg": "^8.9.0",
"pg-hstore": "^2.3.4",
"sequelize": "^6.29.0",
......@@ -23,6 +25,8 @@
"@types/cors": "^2.8.13",
"@types/dotenv": "^8.2.0",
"@types/express": "^4.17.17",
"@types/multer": "^1.4.7",
"@types/nanoid": "^3.0.0",
"@types/uuidv4": "^5.0.0",
"@types/validator": "^13.7.12",
"ts-node-dev": "^2.0.0"
......
*
!.gitignore
\ No newline at end of file
import express, { Router, Request, Response } from 'express'
import { ProductService, productService } from '../services/products'
import { ProductServicePg, productServicePg } from '../services/productsPg'
import multer from 'multer'
// import {nanoid} from 'nanoid'
// const {nanoid} = require('nanoid')
import { config } from '../index.config'
// import path from 'path'
const storage = multer.diskStorage({
destination(req, file, callback) {
callback(null,
config.filePath)
},
filename(req, file, callback) {
callback(null,
file.originalname)
},
})
const upload = multer({storage})
export class ProductsController {
private router: Router
private service: ProductService | ProductServicePg
......@@ -9,7 +28,7 @@ export class ProductsController {
this.router = express.Router()
this.router.get('/', this.getProducts)
this.router.get('/:id', this.getProductById)
this.router.post('/', this.addProduct)
this.router.post('/', upload.single('image'), this.addProduct)
switch(process.env.DB) {
case 'file':
this.service = productService
......@@ -37,7 +56,10 @@ export class ProductsController {
}
private addProduct = async (req: Request, res: Response): Promise<void> => {
const response = await this.service.addProduct(req.body)
console.log('REQ FILE****: ', req.file)
const product = req.body
product.image = req.file ? req.file.filename : ''
const response = await this.service.addProduct(product)
res.send(response)
}
}
\ No newline at end of file
import path from 'path'
export const config = {
port: 8000
port: 8000,
filePath: path.join(__dirname, '../public/uploads')
}
\ No newline at end of file
......@@ -13,6 +13,7 @@ class App {
constructor() {
this.app = express()
this.app.use(express.json())
this.app.use(express.static('public'))
this.app.use(cors())
}
......
......@@ -8,4 +8,5 @@ export default interface IProduct {
brand_id: string
description: string
suppliers?: ISupplier[]
image: string
}
\ No newline at end of file
......@@ -4,4 +4,5 @@ export default interface IProductDto {
description: string
category_id: string
brand_id: string
image?: File
}
\ No newline at end of file
......@@ -46,6 +46,11 @@ export class Product extends Model {
})
description!: string
@Column({
allowNull: true
})
image!: string
@NotNull
@Column({
type: DataType.DECIMAL,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment