Commit 94041cfe authored by Zhanara's avatar Zhanara

репозитории поменяли на классовый компонент

parent fefb3368
export default interface IDataBase {
init: () => Promise<void>
close: () => Promise<void> | void
}
\ No newline at end of file
import {connect, connection} from 'mongoose';
import { Post } from '../models/Post'; import { Post } from '../models/Post';
import { EStatuses } from '../enums/Estatuses'; import { EStatuses } from '../enums/Estatuses';
import IPost from '../interfaces/IPost'; import IPost from '../interfaces/IPost';
import IResponse from '../interfaces/IResponse'; import IResponse from '../interfaces/IResponse';
import IPostDto from '../interfaces/IPostDto'; import IPostDto from '../interfaces/IPostDto';
import mongoose, { Mongoose } from 'mongoose'
import IDataBase from '../interfaces/IDataBase';
export const mongoose = { export class Mongo implements IDataBase {
run: async () => { private client: Mongoose | null = null
try {
return await connect(`${process.env.MONGO_URL}/hw92`); public close = async(): Promise<void> => {
if (!this.client) return
} catch (error) { await this.client.disconnect()
console.log(error);
} }
},
public async init(): Promise<void> {
stop: async () => { this.client = await mongoose.connect(process.env.MONGO_CLIENT_URL || 'mongodb://localhost/myStore')
try { console.log('Mongo mongoose is connected')
return await connection.destroy();
} catch (error) {
console.log(error);
} }
},
getPosts: async(): Promise<IResponse<IPost[] | undefined>>=> { public getPosts= async(): Promise<IResponse<IPost[] | undefined>>=> {
try{ try{
const data = await Post.find() const data = await Post.find()
const response: IResponse<IPost[]> = { const response: IResponse<IPost[]> = {
...@@ -41,8 +38,8 @@ export const mongoose = { ...@@ -41,8 +38,8 @@ export const mongoose = {
return response return response
} }
}, }
getPostById: async(id: string): Promise<IResponse<IPost | undefined>>=> { public getPostById= async(id: string): Promise<IResponse<IPost | undefined>>=> {
try{ try{
const data = await Post.findById(id) const data = await Post.findById(id)
const response: IResponse<IPost> = { const response: IResponse<IPost> = {
...@@ -60,8 +57,8 @@ export const mongoose = { ...@@ -60,8 +57,8 @@ export const mongoose = {
} }
return response return response
} }
}, }
addPost: async(postDto: IPostDto): Promise<IResponse<IPost | undefined>>=> { public addPost = async(postDto: IPostDto): Promise<IResponse<IPost | undefined>>=> {
try{ try{
const post = new Post(postDto) const post = new Post(postDto)
const data = await post.save() const data = await post.save()
...@@ -80,8 +77,8 @@ export const mongoose = { ...@@ -80,8 +77,8 @@ export const mongoose = {
} }
return response return response
} }
}, }
deletePostById: async(id: string): Promise<IResponse<IPost | undefined>>=> { public deletePostById = async(id: string): Promise<IResponse<IPost | undefined>>=> {
try{ try{
const data = await Post.findOneAndDelete({_id:id}) const data = await Post.findOneAndDelete({_id:id})
const response: IResponse<IPost> = { const response: IResponse<IPost> = {
...@@ -102,5 +99,5 @@ export const mongoose = { ...@@ -102,5 +99,5 @@ export const mongoose = {
} }
}; };
export const mongo = new Mongo()
import IPost from "../interfaces/IPost" import IPost from "../interfaces/IPost"
import IPostDto from "../interfaces/IPostDto" import IPostDto from "../interfaces/IPostDto"
import IResponse from "../interfaces/IResponse" import IResponse from "../interfaces/IResponse"
import {mongoose} from "../repository/mongoose" import { Mongo, mongo } from "../repository/mongoose"
export class PostService { export class PostService {
private repository private repository : Mongo
constructor() { constructor() {
this.repository = mongoose this.repository = mongo
} }
public getPosts = async (): Promise<IResponse<IPost[] | undefined>> => { public getPosts = async (): Promise<IResponse<IPost[] | undefined>> => {
return await this.repository.getPosts() return await this.repository.getPosts()
......
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