Commit fefb3368 authored by Zhanara's avatar Zhanara

Для поста созданы сервис, контроллер

parent c6afdce8
import express, { Router, Request, Response } from 'express'
import multer from 'multer'
import { config } from '../index.config'
import { PostService, postService } from '../services/posts'
const storage = multer.diskStorage({
destination(req, file, callback) {
......@@ -17,13 +18,40 @@ const upload = multer({storage})
export class PostsController {
private router: Router
private service: PostService
constructor() {
this.router = express.Router()
this.router.get('/', this.getPosts)
this.router.get('/:id', this.getPostById)
this.router.post('/', upload.single('image'), this.addPost)
this.router.delete('/:id', this.deletePostById)
this.service = postService
}
public getRouter = (): Router => {
return this.router
}
private getPosts = async (req: Request, res: Response): Promise<void> => {
const response = await this.service.getPosts()
res.send(response)
}
private getPostById = async (req: Request, res: Response): Promise<void> => {
const response = await this.service.getPostById(req.params.id)
res.send(response)
}
private addPost = async (req: Request, res: Response): Promise<void> => {
const post = req.body
post.image = req.file ? req.file.filename : ''
const response = await this.service.addPost(post)
res.send(response)
}
private deletePostById = async (req: Request, res: Response): Promise<void> => {
const response = await this.service.deletePostById(req.params.id)
res.send(response)
}
}
\ No newline at end of file
import { EStatuses } from "../enums/Estatuses"
export default interface IResponse<T> {
result: T
message: string
status: EStatuses
}
\ 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';
export const mongoose = {
run: async () => {
try {
return await connect(`${process.env.MONGO_URL}/hw92`);
} catch (error) {
console.log(error);
}
......@@ -16,4 +22,85 @@ export const mongoose = {
console.log(error);
}
},
getPosts: async(): Promise<IResponse<IPost[] | undefined>>=> {
try{
const data = await Post.find()
const response: IResponse<IPost[]> = {
status: EStatuses.OK,
result: data as any,
message: 'Posts found'
}
return response
} catch (err: unknown) {
const error = err as Error
const response: IResponse<undefined> = {
status: EStatuses.NOT_OK,
result: undefined,
message: error.message
}
return response
}
},
getPostById: async(id: string): Promise<IResponse<IPost | undefined>>=> {
try{
const data = await Post.findById(id)
const response: IResponse<IPost> = {
status: EStatuses.OK,
result: data as any,
message: 'Post found'
}
return response
} catch (err: unknown) {
const error = err as Error
const response: IResponse<undefined> = {
status: EStatuses.NOT_OK,
result: undefined,
message: error.message
}
return response
}
},
addPost: async(postDto: IPostDto): Promise<IResponse<IPost | undefined>>=> {
try{
const post = new Post(postDto)
const data = await post.save()
const response: IResponse<IPost> = {
status: EStatuses.OK,
result: data ,
message: 'Post added'
}
return response
} catch (err: unknown) {
const error = err as Error
const response: IResponse<undefined> = {
status: EStatuses.NOT_OK,
result: undefined,
message: error.message
}
return response
}
},
deletePostById: async(id: string): Promise<IResponse<IPost | undefined>>=> {
try{
const data = await Post.findOneAndDelete({_id:id})
const response: IResponse<IPost> = {
status: EStatuses.OK,
result: data as any,
message: 'Post deleted'
}
return response
} catch (err: unknown) {
const error = err as Error
const response: IResponse<undefined> = {
status: EStatuses.NOT_OK,
result: undefined,
message: error.message
}
return response
}
}
};
import IPost from "../interfaces/IPost"
import IPostDto from "../interfaces/IPostDto"
import IResponse from "../interfaces/IResponse"
import {mongoose} from "../repository/mongoose"
export class PostService {
private repository
constructor() {
this.repository = mongoose
}
public getPosts = async (): Promise<IResponse<IPost[] | undefined>> => {
return await this.repository.getPosts()
}
public getPostById = async (id: string): Promise<IResponse<IPost | undefined>> => {
return await this.repository.getPostById(id)
}
public addPost = async (postDto: IPostDto): Promise<IResponse<IPost | undefined>> => {
return await this.repository.addPost(postDto)
}
public deletePostById = async (id: string): Promise<IResponse<IPost | undefined>> => {
return await this.repository.deletePostById(id)
}
}
export const postService = new PostService()
\ 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