Skip to content

Commit 3f20111

Browse files
Merge pull request #10 from Sewvandiii/main
Post Endpoint Done
2 parents de05052 + 8f9b626 commit 3f20111

File tree

6 files changed

+153
-0
lines changed

6 files changed

+153
-0
lines changed

src/api/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Router } from "express";
22
import user from "./user/user.route";
33
import category from "./category/category.route";
4+
import post from "./post/post.route"
45

56
const router = Router();
67

@@ -10,5 +11,6 @@ router.get("/", (req, res) => {
1011

1112
router.use("/user", user);
1213
router.use("/category", category);
14+
router.use("/post", post);
1315

1416
export default router;

src/api/post/post.class.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import * as mongoose from "mongoose";
2+
import { IPost } from "./post.interface";
3+
4+
export const PostSchema = new mongoose.Schema({
5+
description: { type: String, required: true },
6+
imageURL: { type: String, required: true },
7+
createdDate: { type: String, required: true },
8+
userId: { type: String, required: true },
9+
categoryName: { type: String, required: true },
10+
});
11+
12+
const Post = mongoose.model<IPost>("Post", PostSchema);
13+
export default Post;

src/api/post/post.controller.ts

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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+
}

src/api/post/post.interface.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import * as mongoose from "mongoose";
2+
3+
export interface IPost extends mongoose.Document {
4+
description: string;
5+
imageURL: string;
6+
createdDate: string;
7+
userId: string;
8+
categoryName: string;
9+
}

src/api/post/post.route.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Router } from "express";
2+
import Controller from "./post.controller";
3+
4+
const post: Router = Router();
5+
const controller = new Controller();
6+
7+
post.post("/", controller.addPost);
8+
post.get("/:id", controller.getPost);
9+
post.get("/", controller.getAllPost);
10+
post.put("/", controller.updatePost);
11+
post.delete("/:id", controller.deletePost);
12+
13+
export default post;

src/config/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ export default {
1111
DB_NAME: "CodeKitty",
1212
USER_COLLECTION_NAME: "user",
1313
CATEGORY_COLLECTION_NAME: "category",
14+
POST_COLLECTION_NAME: "post"
1415
};

0 commit comments

Comments
 (0)