-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
154 lines (127 loc) · 3.91 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
require("dotenv").config();
const express = require("express");
const mongoose = require("mongoose");
const cors = require("cors");
const app = express();
app.use(cors());
app.use(express.json());
mongoose.connect(process.env.MONGODB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
const projectSchema = new mongoose.Schema({
name: String,
id: String,
image: String,
link: String,
users: [String],
});
const Project = mongoose.model("Project", projectSchema);
const userSchema = new mongoose.Schema({
uid: String,
selected_project: String,
});
const User = mongoose.model("User", userSchema);
// ___________________________________________________________
// get all the projects on home-page
app.get("/api/v1/projects/getAll", async (req, res) => {
try {
const projects = await Project.find();
res.json(projects);
} catch (err) {
res.status(500).json({ message: err.message });
}
});
// ___________________________________________________________
// view the user selected project
app.get("/api/v1/projects/:userId", async (req, res) => {
try {
const user = await User.findOne({ uid: req.params.userId });
if (user) {
const project = await Project.findOne({ id: user.selected_project });
res.json(project);
} else {
res.status(404).json({ message: "User not found" });
}
} catch (err) {
res.status(500).json({ message: err.message });
}
});
// ___________________________________________________________
// View a particular project
app.get("/api/v1/get/project/:projectId", async (req, res) => {
try {
const project = await Project.findOne({ id: req.params.projectId });
if (project) {
res.json(project);
} else {
res.status(404).json({ message: "Project not found" });
}
} catch (err) {
res.status(500).json({ message: err.message });
}
});
// ___________________________________________________________
// Select a project
app.post("/api/v1/selectProject", async (req, res) => {
const { uid, projectId } = req.body;
try {
const user = await User.findOneAndUpdate(
{ uid },
{ selected_project: projectId },
{ new: true, upsert: true }
);
const project = await Project.findOneAndUpdate(
{ id: projectId },
{ $addToSet: { users: uid } },
{ new: true }
);
res.status(200).json({ user, project });
} catch (err) {
res.status(400).json({ message: err.message });
}
});
// ___________________________________________________________
// Check if a project has two users
app.get("/api/v1/checkUsers/:projectId", async (req, res) => {
try {
const project = await Project.findOne({ id: req.params.projectId });
if (project) {
const hasTwoUsers = project.users.length === 2;
res.json({ hasTwoUsers });
} else {
res.status(404).json({ message: "Project not found" });
}
} catch (err) {
res.status(500).json({ message: err.message });
}
});
// ___________________________________________________________
// Create a new user
app.post("/api/v1/users", async (req, res) => {
const { uid, selected_project } = req.body;
const user = new User({ uid, selected_project });
try {
const savedUser = await user.save();
res.status(201).json(savedUser);
} catch (err) {
res.status(400).json({ message: err.message });
}
});
// ___________________________________________________________
// Create a new project
app.post("/api/v1/projects", async (req, res) => {
const { name, id, image, link, users } = req.body;
const project = new Project({ name, id, image, link, users });
try {
const savedProject = await project.save();
res.status(201).json(savedProject);
} catch (err) {
res.status(400).json({ message: err.message });
}
});
// ___________________________________________________________
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});