Commit da51e85b authored by Zhanara's avatar Zhanara

Создан сервис и контроллер для comment

parent e59e054a
import express, { Router, Request, Response } from 'express'
import { CommentService, commentService } from '../services/comments'
import IUser from '../interfaces/IUser'
export class CommentController {
private router: Router
private service: CommentService
constructor() {
this.router = express.Router()
this.service = commentService
this.router.get('/', this.getComments)
this.router.get('/:id', this.getCommentById)
this.router.post('/', this.addComment)
this.router.delete('/:id', this.deleteCommentById)
}
public getRouter = (): Router => {
return this.router
}
private getComments = async (req: Request, res: Response): Promise<void> => {
const response = await this.service.getComments()
res.send(response)
}
private getCommentById = async (req: Request, res: Response): Promise<void> => {
const response = await this.service.getCommentById(req.params.id)
res.send(response)
}
private addComment = async (req: Request, res: Response): Promise<void> => {
const response = await this.service.addComment(req.body)
res.send(response)
}
private deleteCommentById = async (req: Request, res: Response): Promise<void> => {
const user = req.user as IUser;
const id = req.params.id;
const response = await this.service.deleteCommentById(user,id)
res.send(response)
}
}
\ No newline at end of file
import { Document, ObjectId } from "mongoose";
import IUser from "./IUser";
import IPost from "./IPost";
export default interface IComment extends Document {
_id: ObjectId
user: IUser
post: IPost
comment: string
datetime: Date
}
\ No newline at end of file
import IComment from "./IComment"
export default interface ICommentDto {
comment: IComment['comment']
}
\ No newline at end of file
import { ObjectId } from "mongoose";
export default interface IUser { export default interface IUser {
_id: ObjectId;
username: string; username: string;
password: string; password: string;
token: string; token: string;
......
import mongoose, { Schema } from "mongoose";
import IComment from "../interfaces/IComment";
const CommentSchema: Schema = new Schema<IComment>({
comment: {
type: String,
required: true
},
datetime: {
type: Date
},
user: [
{
type: Schema.Types.ObjectId,
ref: 'User'
}
],
post: [
{
type: Schema.Types.ObjectId,
ref: 'Post'
}
],
}, {
versionKey: false
})
export const Comment = mongoose.model<IComment>('Comment', CommentSchema)
\ No newline at end of file
...@@ -5,11 +5,15 @@ import IResponse from '../interfaces/IResponse'; ...@@ -5,11 +5,15 @@ import IResponse from '../interfaces/IResponse';
import IPostDto from '../interfaces/IPostDto'; import IPostDto from '../interfaces/IPostDto';
import mongoose, { Mongoose } from 'mongoose' import mongoose, { Mongoose } from 'mongoose'
import IDataBase from '../interfaces/IDataBase'; import IDataBase from '../interfaces/IDataBase';
import IComment from '../interfaces/IComment';
import ICommentDto from '../interfaces/ICommentDto';
import { Comment } from '../models/Comments';
import IUser from '../interfaces/IUser';
export class Mongo implements IDataBase { export class Mongo implements IDataBase {
private client: Mongoose | null = null private client: Mongoose | null = null
public close = async(): Promise<void> => { public close = async (): Promise<void> => {
if (!this.client) return if (!this.client) return
await this.client.disconnect() await this.client.disconnect()
} }
...@@ -19,8 +23,8 @@ import IDataBase from '../interfaces/IDataBase'; ...@@ -19,8 +23,8 @@ import IDataBase from '../interfaces/IDataBase';
console.log('Mongo mongoose is connected') console.log('Mongo mongoose is connected')
} }
public 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[]> = {
status: EStatuses.OK, status: EStatuses.OK,
...@@ -38,8 +42,8 @@ import IDataBase from '../interfaces/IDataBase'; ...@@ -38,8 +42,8 @@ import IDataBase from '../interfaces/IDataBase';
return response return response
} }
} }
public 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> = {
status: EStatuses.OK, status: EStatuses.OK,
...@@ -57,13 +61,13 @@ import IDataBase from '../interfaces/IDataBase'; ...@@ -57,13 +61,13 @@ import IDataBase from '../interfaces/IDataBase';
return response return response
} }
} }
public 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()
const response: IResponse<IPost> = { const response: IResponse<IPost> = {
status: EStatuses.OK, status: EStatuses.OK,
result: data , result: data,
message: 'Post added' message: 'Post added'
} }
return response return response
...@@ -77,9 +81,9 @@ import IDataBase from '../interfaces/IDataBase'; ...@@ -77,9 +81,9 @@ import IDataBase from '../interfaces/IDataBase';
return response return response
} }
} }
public 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> = {
status: EStatuses.OK, status: EStatuses.OK,
result: data as any, result: data as any,
...@@ -97,6 +101,92 @@ import IDataBase from '../interfaces/IDataBase'; ...@@ -97,6 +101,92 @@ import IDataBase from '../interfaces/IDataBase';
} }
} }
public getComments = async (): Promise<IResponse<IComment[] | undefined>> => {
try {
const data = await Comment.find().populate('user').populate('post')
const response: IResponse<IComment[]> = {
status: EStatuses.OK,
result: data as any,
message: 'Comments 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
}
}
public getCommentById = async (id: string): Promise<IResponse<IComment | undefined>> => {
try {
const data = await Comment.findById(id).populate('user').populate('post');
const response: IResponse<IComment> = {
status: EStatuses.OK,
result: data as any,
message: 'Comment 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
}
}
public addComment = async (commentDto: ICommentDto): Promise<IResponse<IComment | undefined>> => {
try {
const comment = new Comment(commentDto)
const data = await comment.save()
const response: IResponse<IComment> = {
status: EStatuses.OK,
result: data,
message: 'Comment 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
}
}
public deleteCommentById = async (user: IUser, id: string): Promise<IResponse<IComment | undefined>> => {
try {
const comment = await Comment.findById(id);
if (!comment) {
throw new Error('Comment not found');
}
if (comment.user.toString() !== user._id.toString()) {
throw new Error('You are not authorized to delete this comment!');
}
const data = await Comment.findOneAndDelete({ _id: id })
const response: IResponse<IComment> = {
status: EStatuses.OK,
result: data as any,
message: 'Comment 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
}
}
}; };
export const mongo = new Mongo() export const mongo = new Mongo()
import IComment from "../interfaces/IComment"
import ICommentDto from "../interfaces/ICommentDto"
import IResponse from "../interfaces/IResponse"
import IUser from "../interfaces/IUser"
import { Mongo, mongo } from "../repository/mongoose"
export class CommentService {
private repository : Mongo
constructor() {
this.repository = mongo
}
public getComments = async (): Promise<IResponse<IComment[] | undefined>> => {
return await this.repository.getComments()
}
public getCommentById = async (id: string): Promise<IResponse<IComment | undefined>> => {
return await this.repository.getCommentById(id)
}
public addComment = async (commentDto: ICommentDto): Promise<IResponse<IComment | undefined>> => {
return await this.repository.addComment(commentDto)
}
public deleteCommentById = async (user: IUser, id: string): Promise<IResponse<IComment | undefined>> => {
return await this.repository.deleteCommentById(user, id)
}
}
export const commentService = new CommentService()
\ 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