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