Skip to content

Commit 8290e22

Browse files
Post Get For User Added
1 parent 7761d30 commit 8290e22

File tree

4 files changed

+160
-111
lines changed

4 files changed

+160
-111
lines changed

src/api/post/post.class.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import * as mongoose from "mongoose";
22
import { IPost } from "./post.interface";
33

44
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 },
5+
description: { type: String, required: true },
6+
imageURL: { type: String, required: true },
7+
createdDate: { type: String, required: true },
8+
userId: { type: String, required: true, ref: "User" },
9+
categoryName: { type: String, required: true },
1010
});
1111

1212
const Post = mongoose.model<IPost>("Post", PostSchema);

src/api/post/post.controller.ts

Lines changed: 138 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -10,106 +10,145 @@ import * as responses from "../../helpers/responses.handler";
1010
import { ObjectId } from "mongodb";
1111

1212
const getCollection = () => {
13-
return MongoHelper.client
14-
.db(Config.DB_NAME)
15-
.collection(Config.POST_COLLECTION_NAME);
13+
return MongoHelper.client
14+
.db(Config.DB_NAME)
15+
.collection(Config.POST_COLLECTION_NAME);
1616
};
1717

1818
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-
};
19+
public addPost = async (req: Request, res: Response): Promise<any> => {
20+
const requestData = req.body;
21+
const collection = getCollection();
22+
23+
requestData.createdDate = new Date().toString();
24+
const post = new Post(requestData);
25+
try {
26+
await collection.insertOne(post);
27+
28+
res
29+
.status(httpStatus.CREATED)
30+
.send(responses.success(SuccessCodes.SUCCESSFULLY_DATA_ADDED));
31+
res.end();
32+
} catch (e) {
33+
console.error(e.message);
34+
res.send(
35+
responses.failed(ErrorCodes.INTERNAL_ERROR, httpStatus.BAD_REQUEST)
36+
);
37+
}
38+
};
39+
40+
public getPost = async (req: Request, res: Response): Promise<any> => {
41+
const collection = getCollection();
42+
const id = req.params.id;
43+
try {
44+
const post = await collection.findOne({
45+
_id: new ObjectId(id),
46+
});
47+
48+
res.send(
49+
responses.successWithPayload(
50+
SuccessCodes.SUCCESSFULLY_DATA_RETRIVED,
51+
post
52+
)
53+
);
54+
} catch (e) {
55+
console.error(e.message);
56+
res.send(
57+
responses.failed(ErrorCodes.INTERNAL_ERROR, httpStatus.BAD_REQUEST)
58+
);
59+
}
60+
};
61+
62+
public getAllPost = async (req: Request, res: Response): Promise<any> => {
63+
const collection: any = getCollection();
64+
65+
try {
66+
const data = await collection.find().toArray();
67+
68+
res.send(
69+
responses.successWithPayload(
70+
SuccessCodes.SUCCESSFULLY_DATA_RETRIVED,
71+
data
72+
)
73+
);
74+
} catch (e) {
75+
console.error(e.message);
76+
res.send(
77+
responses.failed(ErrorCodes.INTERNAL_ERROR, httpStatus.BAD_REQUEST)
78+
);
79+
}
80+
};
81+
82+
public updatePost = async (req: Request, res: Response): Promise<any> => {
83+
const { _id, userId, description, imageURL, createdDate, categoryName } =
84+
req.body;
85+
86+
const collection: any = getCollection();
87+
88+
try {
89+
await collection.findOneAndUpdate(
90+
{ _id: new mongodb.ObjectId(_id) },
91+
{ $set: { userId, description, imageURL, createdDate, categoryName } }
92+
);
93+
94+
res.send(responses.success(SuccessCodes.SUCCESSFULLY_DATA_UPDATED));
95+
} catch (e) {
96+
console.error(e.message);
97+
res.send(
98+
responses.failed(ErrorCodes.INTERNAL_ERROR, httpStatus.BAD_REQUEST)
99+
);
100+
}
101+
};
102+
103+
public deletePost = async (req: Request, res: Response): Promise<any> => {
104+
const id = req.params.id;
105+
const collection: any = getCollection();
106+
107+
try {
108+
await collection.deleteOne({ _id: new mongodb.ObjectId(id) });
109+
res.send(responses.success(SuccessCodes.SUCCESSFULLY_DATA_DELETED));
110+
} catch (e) {
111+
console.error(e.message);
112+
res.send(
113+
responses.failed(ErrorCodes.INTERNAL_ERROR, httpStatus.BAD_REQUEST)
114+
);
115+
}
116+
};
117+
118+
public getAllPostsWithUsers = async (
119+
req: Request,
120+
res: Response
121+
): Promise<any> => {
122+
try {
123+
const collection = getCollection();
124+
125+
const data = await collection
126+
.aggregate([
127+
{
128+
$lookup: {
129+
from: "user",
130+
localField: "userId",
131+
foreignField: "uid",
132+
as: "user",
133+
},
134+
},
135+
{
136+
$unwind: "$user",
137+
},
138+
])
139+
.toArray();
140+
141+
res.send(
142+
responses.successWithPayload(
143+
SuccessCodes.SUCCESSFULLY_DATA_RETRIVED,
144+
data
145+
)
146+
);
147+
} catch (e) {
148+
console.error(e.message);
149+
res.send(
150+
responses.failed(ErrorCodes.INTERNAL_ERROR, httpStatus.BAD_REQUEST)
151+
);
152+
}
153+
};
115154
}

src/api/post/post.route.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ post.get("/:id", controller.getPost);
99
post.get("/", controller.getAllPost);
1010
post.put("/", controller.updatePost);
1111
post.delete("/:id", controller.deletePost);
12+
post.get("/users/all", controller.getAllPostsWithUsers);
1213

1314
export default post;

src/api/user/user.controller.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,19 @@ export default class UserController {
1818
public addUser = async (req: Request, res: Response): Promise<any> => {
1919
const requestData = req.body;
2020
const collection = getCollection();
21-
const user = new User(requestData);
21+
const userData = new User(requestData);
2222
try {
23-
await collection.insertOne(user);
23+
const { uid } = requestData;
24+
const user = await collection.findOne({ uid });
25+
26+
if (user) {
27+
res
28+
.status(httpStatus.OK)
29+
.send(responses.success(ErrorCodes.USER_ALREADY_EXISTS));
30+
res.end();
31+
return;
32+
}
33+
await collection.insertOne(userData);
2434

2535
res
2636
.status(httpStatus.CREATED)
@@ -38,9 +48,8 @@ export default class UserController {
3848
const collection = getCollection();
3949

4050
try {
41-
console.log(req.params.id);
4251
const user = await collection.findOne({
43-
_id: req.params.id,
52+
uid: req.params.id,
4453
});
4554

4655
res.send(
@@ -78,13 +87,13 @@ export default class UserController {
7887
};
7988

8089
public updateUser = async (req: Request, res: Response): Promise<any> => {
81-
const { _id, displayName, email, photoURL } = req.body;
90+
const { uid, displayName, email, photoURL } = req.body;
8291

8392
const collection: any = getCollection();
8493

8594
try {
8695
await collection.findOneAndUpdate(
87-
{ _id: new mongodb.ObjectId(_id) },
96+
{ uid: new mongodb.ObjectId(uid) },
8897
{ $set: { displayName, email, photoURL } }
8998
);
9099

@@ -102,7 +111,7 @@ export default class UserController {
102111
const collection: any = getCollection();
103112

104113
try {
105-
await collection.deleteOne({ _id: new mongodb.ObjectId(id) });
114+
await collection.deleteOne({ uid: new mongodb.ObjectId(id) });
106115
res.send(responses.success(SuccessCodes.SUCCESSFULLY_DATA_DELETED));
107116
} catch (e) {
108117
console.error(e.message);

0 commit comments

Comments
 (0)