Commit 554cc75b authored by Pavel Mishakov's avatar Pavel Mishakov

lesson 82 ready

parents
/node_modules
\ No newline at end of file
[{"title":"Banana","price":100,"description":"This is a banana","id":"4984df1f-9cbb-49c4-8934-426233479c81"},{"title":"Apple","price":77,"description":"This is an apple","id":"5525dded-afcf-4a60-ab78-813b0d4a27c4"}]
\ No newline at end of file
This diff is collapsed.
{
"name": "classwork",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"dev": "ts-node-dev --respawn --trace-warnings --transpile-only src/index.ts",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.18.2",
"uuidv4": "^6.2.13"
},
"devDependencies": {
"@types/express": "^4.17.17",
"@types/uuidv4": "^5.0.0",
"ts-node-dev": "^2.0.0"
}
}
import express, { Router, Request, Response } from 'express'
import { EStatuses } from '../enums/EStatuses'
import IResponse from '../interfaces/IResponse'
const router: Router = express.Router()
router.get('/', (req: Request, res: Response) => {
try {
const response: IResponse = {
status: EStatuses.OK,
result: undefined,
message: 'Server is ok'
}
res.send(response)
} catch (err: unknown) {
const error = err as Error
const response: IResponse = {
status: EStatuses.NOT_OK,
result: undefined,
message: error.message
}
res.status(200).send(response)
}
})
export const healthCheckRouter = router
\ No newline at end of file
import express, { Router, Request, Response } from 'express'
import { productService } from '../services/products'
const router: Router = express.Router()
router.get('/', (req: Request, res: Response) => {
const response = productService.getProducts()
res.send(response)
})
router.get('/:id', (req: Request, res: Response) => {
const response = productService.getProductById(req.params.id)
res.send(response)
})
router.post('/', (req: Request, res: Response) => {
const response = productService.addProduct(req.body)
res.send(response)
})
export const productsRouter = router
\ No newline at end of file
export const enum EStatuses {
OK = 1,
NOT_OK = 0
}
\ No newline at end of file
export const config = {
port: 8000
}
\ No newline at end of file
import express, { Express } from 'express'
import { config } from './index.config'
import { healthCheckRouter } from './controllers/healthCheck'
import { productsRouter } from './controllers/products'
import { db } from './repository/fileDB'
const app: Express = express()
app.use(express.json())
const run = async () => {
db.init()
app.use('/health-check', healthCheckRouter)
app.use('/products', productsRouter)
app.listen(config.port, () => {
console.log(`Server is running on port ${config.port}`)
})
}
run()
.then(() => {
console.log('Everything is ok')
})
.catch((err: unknown) => {
const error = err as Error
console.log(error.message)
})
\ No newline at end of file
export default interface IProduct {
id: string
title: string
price: number
description: string
}
\ No newline at end of file
export default interface IRequest {
title: string
price: number
description: string
}
\ No newline at end of file
import { EStatuses } from "../enums/EStatuses";
import IProduct from "./IProduct";
export default interface IResponse {
result: IProduct[] | IProduct | undefined
message: string
status: EStatuses
}
\ No newline at end of file
import IProduct from "../interfaces/IProduct";
import fs from 'node:fs/promises'
import { uuid } from 'uuidv4';
import IRequest from "../interfaces/IRequest";
class FileDB {
data: IProduct[]
filename: string
constructor() {
this.data = []
this.filename = './db.json'
}
init = async () => {
try {
const fileContents = await fs.readFile(this.filename, {encoding: 'utf-8'});
this.data = JSON.parse(fileContents);
} catch (err) {
const error = err as Error
console.log(error.message)
this.data = [];
await fs.writeFile('./db.json', JSON.stringify(this.data), {encoding: 'utf-8'})
}
}
getItems = () => {
return this.data;
}
getItemById = (id: string) => {
return this.data.find(p => p.id === id);
}
addItem = (item: IRequest): IProduct => {
const id: string = uuid()
const product: IProduct = {...item, id}
this.data.push(product);
this.save();
return product
}
save = async () => {
try {
await fs.writeFile(this.filename, JSON.stringify(this.data));
} catch(err: unknown) {
const error = err as Error
console.log(error.message)
}
}
}
export const db = new FileDB()
\ No newline at end of file
import { EStatuses } from "../enums/EStatuses"
import IProduct from "../interfaces/IProduct"
import IRequest from "../interfaces/IRequest"
import IResponse from "../interfaces/IResponse"
import { db } from "../repository/fileDB"
class ProductService {
getProducts = (): IResponse => {
try {
const data = db.getItems()
const response: IResponse = {
status: EStatuses.OK,
result: data,
message: ''
}
return response
} catch (err: unknown) {
const error = err as Error
const response: IResponse = {
status: EStatuses.NOT_OK,
result: undefined,
message: error.message
}
return response
}
}
getProductById = (id: string): IResponse => {
try {
const data: IProduct | undefined = db.getItemById(id)
const response: IResponse = {
status: EStatuses.OK,
result: data,
message: ''
}
return response
} catch (err: unknown) {
const error = err as Error
const response: IResponse = {
status: EStatuses.NOT_OK,
result: undefined,
message: error.message
}
return response
}
}
addProduct = (product: IRequest): IResponse => {
try {
const data: IProduct | undefined = db.addItem(product)
const response: IResponse = {
status: EStatuses.OK,
result: data,
message: ''
}
return response
} catch (err: unknown) {
const error = err as Error
const response: IResponse = {
status: EStatuses.NOT_OK,
result: undefined,
message: error.message
}
return response
}
}
}
export const productService = new ProductService()
\ No newline at end of file
This diff is collapsed.
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