Commit b93f828b authored by Kulpybaev Ilyas's avatar Kulpybaev Ilyas

lesson 85

parent f3e040a8
This diff is collapsed.
......@@ -16,6 +16,7 @@
"class-transformer": "^0.5.1",
"cors": "^2.8.5",
"express": "^4.18.2",
"multer": "^1.4.5-lts.1",
"reflect-metadata": "^0.2.1",
"ts-node": "^10.9.1",
"tslib": "^2.6.0",
......@@ -24,6 +25,7 @@
"devDependencies": {
"@types/cors": "^2.8.17",
"@types/express": "^4.17.17",
"@types/multer": "^1.4.11",
"@types/node": "^20.4.2",
"@typescript-eslint/eslint-plugin": "^6.0.0",
"@typescript-eslint/parser": "^6.0.0",
......
*
\ No newline at end of file
......@@ -26,6 +26,7 @@ class App {
}
private initAssets() {
this.app.use(express.json());
this.app.use(express.static('public'));
}
public listen() {
this.app.listen(this.port, () => {
......
import path from 'path';
const rootPath = path.resolve(__dirname, '..', '..');
const config = {
rootPath,
uploadPath: path.join(rootPath, 'public/uploads'),
};
export default config;
......@@ -20,6 +20,7 @@ export class ProductController {
};
createProduct: RequestHandler = (req, res): void => {
const productDto = plainToInstance(ProductDto, req.body);
if (req.file) productDto.image = req.file.filename;
const product = this.service.createProduct(productDto);
res.send(product);
};
......
......@@ -15,5 +15,19 @@
"name": "New product",
"description": "Some new cool perfect super product",
"price": 1600
},
{
"id": "8dab8e5b-9c26-4a62-a6ad-94cd0a3b1597",
"name": "Watch",
"price": "12487",
"description": "The best watches",
"image": "99c3d062-9e5e-4151-b9e1-d68c70c66c25.avif"
},
{
"id": "db553f1e-635b-43b1-a9b6-53cb4b9076bf",
"name": "test2",
"price": "111",
"description": "test desc",
"image": "2f59f244-a60d-4879-8676-0a3baeaadcd8.png"
}
]
\ No newline at end of file
......@@ -4,4 +4,5 @@ export class ProductDto {
title!: string;
description!: string;
price!: number;
image!: string;
}
......@@ -3,4 +3,5 @@ export interface IProduct {
title: string;
price: number;
description?: string;
image?: string;
}
import multer from 'multer';
import path from 'path';
import { randomUUID } from 'crypto';
import config from '@/config';
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, config.uploadPath);
},
filename: (req, file, cb) => {
cb(null, randomUUID() + path.extname(file.originalname));
},
});
export const upload = multer({ storage });
import { Router } from 'express';
import { IRoute } from '@/interfaces/IRoute.interface';
import { ProductController } from '@/controllers/product.controller';
import { upload } from '@/middlewares/upload';
export class ProductRoute implements IRoute {
public path = '/products';
......@@ -15,6 +16,6 @@ export class ProductRoute implements IRoute {
private init() {
this.router.get('/', this.controller.getProducts);
this.router.get('/:id', this.controller.getProduct);
this.router.post('/', this.controller.createProduct);
this.router.post('/', upload.single('image'), this.controller.createProduct);
}
}
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