Commit 9eff7f53 authored by Nurasyl's avatar Nurasyl

update

parent 3aa42b7d
......@@ -7,6 +7,7 @@ import { NewProductForm } from './containers/NewProductForm';
function App() {
return (
<>
<a href=""></a>
<CssBaseline />
<header>
<AppToolbar />
......
This diff is collapsed.
......@@ -18,12 +18,15 @@
"cors": "^2.8.5",
"express": "^4.18.2",
"multer": "^1.4.5-lts.1",
"mysql": "^2.18.1",
"nanoid": "^3.3.6",
"pg": "^8.12.0",
"reflect-metadata": "^0.2.1",
"save": "^2.9.0",
"save-dev": "^0.0.1-security",
"ts-node": "^10.9.1",
"tslib": "^2.6.0",
"typeorm": "^0.3.20",
"typescript": "^5.1.6"
},
"devDependencies": {
......
......@@ -2,6 +2,7 @@ import express from 'express';
import { Application, RequestHandler } from 'express';
import { AppInit } from './interfaces/AppInit.interface';
import { IRoute } from './interfaces/IRoute.interface';
import { AppDataSource } from './appDataSource';
class App {
public app: Application;
......@@ -28,9 +29,20 @@ class App {
this.app.use(express.json());
this.app.use(express.static('public'))
}
public listen() {
public async listen() {
await AppDataSource.initialize()
.then(() => {
console.log("Data Source has been initialized!")
})
.catch((err) => {
console.error("Error during Data Source initialization", err)
})
this.app.listen(this.port, () => {
console.log(`App listening on the http://localhost:${this.port}`);
process.on('exit', () => {
AppDataSource.destroy();
})
});
}
}
......
import { DataSource } from "typeorm";
import { Product } from "./entities/product.entity";
export const AppDataSource = new DataSource({
type: "postgres",
host: "localhost",
port: 5432,
username: "postgres",
password: "root",
database: "classwork",
schema: "classwork",
synchronize: true,
entities: [Product]
})
\ No newline at end of file
......@@ -10,8 +10,8 @@ export class ProductController {
this.service = new ProductService();
}
getAllProducts: RequestHandler = (req, res): void => {
const products = this.service.getAllProducts();
getAllProducts: RequestHandler = async (req, res): Promise<void> => {
const products = await this.service.getAllProducts();
res.send(products);
};
......
import { Entity, PrimaryGeneratedColumn, Column } from "typeorm";
@Entity()
export class Product {
@PrimaryGeneratedColumn()
id!: number;
@Column()
title!: string
@Column()
description!: string
@Column()
price!: number
@Column({nullable: true})
image!: string
}
\ No newline at end of file
import { AppDataSource } from "@/appDataSource";
import { ProductDto } from "@/dto/product.dto";
import { Product } from "@/entities/product.entity";
import { Repository } from "typeorm";
class ProductRepo {
private repo: Repository<Product>
constructor() {
this.repo = AppDataSource.getRepository(Product)
}
async create(body: ProductDto) {
return await this.repo.save(body)
}
async getAll() {
return await this.repo.find()
}
async getOne(id: number) {
return await this.repo.findOne({where: {id: id}})
}
}
export const productRepo = new ProductRepo()
\ No newline at end of file
import { IProduct } from '@/interfaces/IProduct.interface';
import path from 'path';
import * as fs from 'fs';
import { ProductDto } from '@/dto/product.dto';
const filePath = path.join(__dirname, '../../data');
import { productRepo } from '@/repositories/product.repository';
import { Product } from '@/entities/product.entity';
export class ProductService {
private products: IProduct[] = [];
constructor() {
this.init();
}
init(): void {
try {
const fileContent = fs.readFileSync(`${filePath}/products.json`);
this.products = JSON.parse(fileContent.toString());
} catch (e) {
this.products = [];
}
}
save(): void {
fs.writeFileSync(`${filePath}/products.json`, JSON.stringify(this.products, null, 2));
}
getAllProducts = (): IProduct[] => {
return this.products;
getAllProducts = async (): Promise<Product[]> => {
return await productRepo.getAll()
};
getProduct = (id: string): IProduct => {
const product = this.products.find((product) => product.id === id);
if (product) {
return product;
}
throw new Error('Invalid id');
getProduct = async (id: string): Promise<Product | null> => {
return await productRepo.getOne(parseInt(id))
};
createProduct = (data: ProductDto): IProduct => {
const product = {
...data,
id: Math.random().toString()
};
this.products.push(product);
this.save();
return product;
createProduct = async (data: ProductDto): Promise<Product> => {
return productRepo.create(data)
};
}
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