|
| 1 | +import { Request, Response } from "express"; |
| 2 | +import * as mongodb from "mongodb"; |
| 3 | +import { MongoHelper } from "../../config/mongodb.config"; |
| 4 | +import Post from "./post.class"; |
| 5 | +import Config from "../../config/config"; |
| 6 | +import ErrorCodes from "../../config/error.codes"; |
| 7 | +import SuccessCodes from "../../config/success.codes"; |
| 8 | +import * as httpStatus from "http-status"; |
| 9 | +import * as responses from "../../helpers/responses.handler"; |
| 10 | +import { ObjectId } from "mongodb"; |
| 11 | + |
| 12 | +const getCollection = () => { |
| 13 | + return MongoHelper.client |
| 14 | + .db(Config.DB_NAME) |
| 15 | + .collection(Config.POST_COLLECTION_NAME); |
| 16 | +}; |
| 17 | + |
| 18 | +export default class PostController { |
| 19 | + public addPost = async (req: Request, res: Response): Promise<any> => { |
| 20 | + const requestData = req.body; |
| 21 | + const collection = getCollection(); |
| 22 | + const post = new Post(requestData); |
| 23 | + try { |
| 24 | + await collection.insertOne(post); |
| 25 | + |
| 26 | + res |
| 27 | + .status(httpStatus.CREATED) |
| 28 | + .send(responses.success(SuccessCodes.SUCCESSFULLY_DATA_ADDED)); |
| 29 | + res.end(); |
| 30 | + } catch (e) { |
| 31 | + console.error(e.message); |
| 32 | + res.send( |
| 33 | + responses.failed(ErrorCodes.INTERNAL_ERROR, httpStatus.BAD_REQUEST) |
| 34 | + ); |
| 35 | + } |
| 36 | + }; |
| 37 | + |
| 38 | + public getPost = async (req: Request, res: Response): Promise<any> => { |
| 39 | + const collection = getCollection(); |
| 40 | + const id = req.params.id; |
| 41 | + try { |
| 42 | + const post = await collection.findOne({ |
| 43 | + _id: new ObjectId(id), |
| 44 | + }); |
| 45 | + |
| 46 | + res.send( |
| 47 | + responses.successWithPayload( |
| 48 | + SuccessCodes.SUCCESSFULLY_DATA_RETRIVED, |
| 49 | + post |
| 50 | + ) |
| 51 | + ); |
| 52 | + } catch (e) { |
| 53 | + console.error(e.message); |
| 54 | + res.send( |
| 55 | + responses.failed(ErrorCodes.INTERNAL_ERROR, httpStatus.BAD_REQUEST) |
| 56 | + ); |
| 57 | + } |
| 58 | + }; |
| 59 | + |
| 60 | + public getAllPost = async (req: Request, res: Response): Promise<any> => { |
| 61 | + const collection: any = getCollection(); |
| 62 | + |
| 63 | + try { |
| 64 | + const data = await collection.find().toArray(); |
| 65 | + |
| 66 | + res.send( |
| 67 | + responses.successWithPayload( |
| 68 | + SuccessCodes.SUCCESSFULLY_DATA_RETRIVED, |
| 69 | + data |
| 70 | + ) |
| 71 | + ); |
| 72 | + } catch (e) { |
| 73 | + console.error(e.message); |
| 74 | + res.send( |
| 75 | + responses.failed(ErrorCodes.INTERNAL_ERROR, httpStatus.BAD_REQUEST) |
| 76 | + ); |
| 77 | + } |
| 78 | + }; |
| 79 | + |
| 80 | + public updatePost = async (req: Request, res: Response): Promise<any> => { |
| 81 | + const { _id, userId, description, imageURL, createdDate, categoryName } = |
| 82 | + req.body; |
| 83 | + |
| 84 | + const collection: any = getCollection(); |
| 85 | + |
| 86 | + try { |
| 87 | + await collection.findOneAndUpdate( |
| 88 | + { _id: new mongodb.ObjectId(_id) }, |
| 89 | + { $set: { userId, description, imageURL, createdDate, categoryName } } |
| 90 | + ); |
| 91 | + |
| 92 | + res.send(responses.success(SuccessCodes.SUCCESSFULLY_DATA_UPDATED)); |
| 93 | + } catch (e) { |
| 94 | + console.error(e.message); |
| 95 | + res.send( |
| 96 | + responses.failed(ErrorCodes.INTERNAL_ERROR, httpStatus.BAD_REQUEST) |
| 97 | + ); |
| 98 | + } |
| 99 | + }; |
| 100 | + |
| 101 | + public deletePost = async (req: Request, res: Response): Promise<any> => { |
| 102 | + const id = req.params.id; |
| 103 | + const collection: any = getCollection(); |
| 104 | + |
| 105 | + try { |
| 106 | + await collection.deleteOne({ _id: new mongodb.ObjectId(id) }); |
| 107 | + res.send(responses.success(SuccessCodes.SUCCESSFULLY_DATA_DELETED)); |
| 108 | + } catch (e) { |
| 109 | + console.error(e.message); |
| 110 | + res.send( |
| 111 | + responses.failed(ErrorCodes.INTERNAL_ERROR, httpStatus.BAD_REQUEST) |
| 112 | + ); |
| 113 | + } |
| 114 | + }; |
| 115 | +} |
0 commit comments