Commit 8ac0a8fb authored by Цой Данил's avatar Цой Данил 💬

Wrote services + added db connection to mongoose db. Check back for working + added fixtures file

parent 1f83bb7e
This diff is collapsed.
{ {
"name": "backend", "name": "homework",
"version": "1.0.0", "version": "1.0.0",
"description": "", "description": "",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "ts-node-dev --respawn --trace-warnings --transpile-only src/index.ts", "dev": "ts-node-dev --respawn --trace-warnings --transpile-only src/index.ts",
"seed": "ts-node-dev --trace-warnings --transpile-only src/fixtures.ts" "seed": "ts-node-dev --trace-warnings --transpile-only src/fixtures.ts",
"test": "echo \"Error: no test specified\" && exit 1"
}, },
"author": "", "author": "",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"@types/cors": "^2.8.13", "@types/cors": "^2.8.13",
"@types/mongoose": "^5.11.97", "@types/jsonwebtoken": "^9.0.1",
"@types/shortid": "^0.0.29", "@types/shortid": "^0.0.29",
"bcrypt": "^5.1.0", "bcrypt": "^5.1.0",
"cors": "^2.8.5", "cors": "^2.8.5",
"dotenv": "^16.0.3", "dotenv": "^16.0.3",
"express": "^4.18.2", "express": "^4.18.2",
"jsonwebtoken": "^9.0.0", "jsonwebtoken": "^9.0.0",
"mongodb": "^5.1.0",
"mongoose": "^7.0.3", "mongoose": "^7.0.3",
"multer": "^1.4.5-lts.1", "multer": "^1.4.5-lts.1",
"shortid": "^2.2.16" "pg": "^8.10.0",
"pg-hstore": "^2.3.4",
"sequelize": "^6.29.3",
"sequelize-typescript": "^2.1.5",
"shortid": "^2.2.16",
"uuid": "^9.0.0"
}, },
"devDependencies": { "devDependencies": {
"@types/bcrypt": "^5.0.0", "@types/bcrypt": "^5.0.0",
"@types/dotenv": "^8.2.0", "@types/dotenv": "^8.2.0",
"@types/express": "^4.17.17", "@types/express": "^4.17.17",
"@types/jsonwebtoken": "^9.0.1", "@types/mongodb": "^4.0.7",
"@types/multer": "^1.4.7" "@types/mongoose": "^5.11.97",
"@types/multer": "^1.4.7",
"@types/uuid": "^9.0.1",
"@types/validator": "^13.7.14",
"ts-node-dev": "^2.0.0"
} }
} }
...@@ -2,6 +2,7 @@ import express, { Express } from "express"; ...@@ -2,6 +2,7 @@ import express, { Express } from "express";
import dotenv from 'dotenv'; import dotenv from 'dotenv';
import cors from 'cors'; import cors from 'cors';
import { healthCheckController } from "./controllers/healthCheckController"; import { healthCheckController } from "./controllers/healthCheckController";
import { mongooseDB } from "./repository/mongooseDB";
dotenv.config() dotenv.config()
...@@ -20,8 +21,16 @@ class App { ...@@ -20,8 +21,16 @@ class App {
this.app.listen(process.env.APP_PORT, () => { this.app.listen(process.env.APP_PORT, () => {
console.log(`Server is running on http://localhost:${process.env.APP_PORT}`); console.log(`Server is running on http://localhost:${process.env.APP_PORT}`);
}) })
await mongooseDB.init()
process.on('exit', () => {
mongooseDB.close()
})
} catch(err: unknown){ } catch(err: unknown){
console.log(err); console.log(err);
} }
} }
} }
\ No newline at end of file
const app = new App()
app.init()
\ No newline at end of file
import IPhoto from "../interfaces/IPhoto";
import IPhotoDto from "../interfaces/IPhotoDto";
import IResponse from "../interfaces/IResponse";
import { MongooseDB, mongooseDB } from "../repository/mongooseDB";
export class PhotosService {
private repository: MongooseDB
constructor(){
this.repository = mongooseDB
}
public getAllPhotos = async(): Promise<IResponse<IPhoto[] | null>> => {
return await this.repository.getAllPhotos()
}
public deletePhotoById = async (id: string, user: string): Promise<IResponse<IPhoto | null>> => {
return await this.repository.deletePhotoById(id, user)
}
public getPhotosByUserId = async(id: string): Promise<IResponse<IPhoto[] | null>> => {
return await this.repository.getPhotosByUserId(id)
}
public addPhoto = async(photoDto: IPhotoDto): Promise<IResponse<IPhoto | null>> => {
return await this.repository.addPhoto(photoDto)
}
}
export const photosService = new PhotosService()
\ No newline at end of file
import { EStatuses } from "../enum/EStatuses";
import IPhoto from "../interfaces/IPhoto";
import IPhotoDto from "../interfaces/IPhotoDto";
import IResponse from "../interfaces/IResponse";
import IUserCreateDto from "../interfaces/IUserCreateDto";
import IUserGetDto from "../interfaces/IUserGetDto";
import { mongooseDB, MongooseDB } from "../repository/mongooseDB";
export class UsersService {
private repository: MongooseDB
constructor(){
this.repository = mongooseDB
}
public createUser = async (userDto: IUserCreateDto): Promise<IResponse<IUserGetDto | null>> => {
return await this.repository.createUser(userDto)
}
public loginUser = async (userDto: IUserCreateDto): Promise<IResponse<IUserGetDto | null>> => {
return await this.repository.loginUser(userDto)
}
}
export const usersService = new UsersService()
\ No newline at end of file
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