-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
232 lines (215 loc) · 6.39 KB
/
index.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
const express = require("express");
const bodyParser = require("body-parser");
const argon2 = require("argon2");
const cors = require("cors");
const knex = require("knex");
const morgan = require("morgan");
const login = require("./controllers/login");
const register = require("./controllers/register");
const auth = require("./controllers/authorization");
let port = process.env.PORT || 4000;
let dbConnection;
if (process.env.DATABASE_URL) {
dbConnection = {
connectionString: process.env.DATABASE_URL,
ssl: {
rejectUnauthorized: false,
},
};
} else {
dbConnection = process.env.POSTGRES_URI;
}
const db = knex({
client: "pg",
connection: dbConnection,
});
const app = express();
app.use(morgan("combined"));
app.use(bodyParser.json());
app.use(cors());
app.post("/login", login.loginWithAuth(db, argon2));
app.post("/register", register.register(db, argon2));
app.get("/users/:id", auth.requireAuth, (req, res) => {
const { id } = req.params;
db.select(["id", "first_name", "last_name", "email", "joined"])
.from("users")
.where({ id: id })
.then((users) => {
if (users.length) {
const user = users[0];
res.json({
email: user.email,
firstName: user["first_name"],
lastName: user["last_name"],
id: user.id,
joined: user.joined,
});
} else {
res.status(400).json("Error getting user.");
}
})
.catch((err) => res.status(400).json("Error getting user."));
});
app.patch("/users/:userId", auth.requireAuth, (req, res) => {
const { userId } = req.params;
const { email, firstName, lastName } = req.body;
db("users")
.where({ id: userId })
.update({ email: email, first_name: firstName, last_name: lastName }, [
"id",
"first_name",
"last_name",
"email",
"joined",
])
.then((users) => {
if (users.length) {
const user = users[0];
res.json({
email: user.email,
firstName: user["first_name"],
lastName: user["last_name"],
id: user.id,
joined: user.joined,
});
} else {
res.status(400).json("Error updating user.");
}
})
.catch((err) => {
console.error(err);
res.status(400).json("Error updating user.");
});
});
app.post("/projects", auth.requireAuth, (req, res) => {
const { title, description } = req.body;
const { userId } = req;
db("projects")
.returning(["id", "title", "description"])
.insert({
title: title,
description: description,
})
.then((projects) => {
let project;
if (projects.length) project = projects[0];
db("project_users")
.returning(["id", "user_id", "project_id"])
.insert({
user_id: userId,
project_id: project.id,
})
.then((projectUsers) => {
let projectUser;
if (projectUsers.length) projectUser = projectUsers[0];
res.json({
project: project,
projectUser: {
id: projectUser.id,
userId: projectUser["user_id"],
projectId: projectUser["projectId"],
},
});
})
.catch((err) => res.status(400).json(err));
})
.catch((err) => res.status(400).json("Unable to create project."));
});
app.get("/projects", auth.requireAuth, (req, res) => {
db.select()
.table("projects")
.then((projects) => {
console.log(projects);
res.status(200).json(projects);
})
.catch((err) => res.status(400).json(err));
});
app.get("/projects/:projectId", auth.requireAuth, (req, res) => {
const { projectId } = req.params;
db.select()
.table("projects")
.where({ id: projectId })
.then((projects) => {
if (projects.length) {
const project = projects[0];
res.status(200).json(project);
} else {
res.status(404).json("Not found.");
}
})
.catch((err) => res.status(400).json(err));
});
app.get("/projects/:projectId/tickets", auth.requireAuth, (req, res) => {
const { projectId } = req.params;
db.select()
.from("tickets")
.where({ project_id: projectId })
.then((tickets) => {
res.status(200).json(tickets);
})
.catch((err) => res.status(400).json("Error getting tickets."));
});
app.get(
"/projects/:projectId/tickets/:ticketId/comments",
auth.requireAuth,
(req, res) => {
const { projectId } = req.params;
db.select([
"id",
"title",
"description",
"created_by",
"assigned_to",
"priority",
"status",
"project_id",
])
.from("tickets")
.where({ project_id: projectId })
.then((tickets) => {
res.status(200).json(tickets);
})
.catch((err) => res.status(400).json("Error getting tickets."));
}
);
app.get("/projects/:projectId/project-users", auth.requireAuth, (req, res) => {
const { projectId } = req.params;
db.select(["id", "user_id", "is_admin", "project_id"])
.from("project_users")
.where({ project_id: projectId })
.then((projectUsers) => {
res.status(200).json(projectUsers);
})
.catch((err) => res.status(400).json("Error getting project users."));
});
// WHERE user.id === recipient.id OR WHERE user.id === sender.id
app.get("/project-invitations", auth.requireAuth, (req, res) => {
const { projectId } = req.params;
db.select(["id", "user_id", "is_admin", "project_id"])
.from("project_users")
.where({ project_id: projectId })
.then((projectUsers) => {
res.status(200).json(projectUsers);
})
.catch((err) => res.status(400).json("Error getting project users."));
});
app.listen(port, () => {
console.log("App is running on port 4000");
});
/*
DONE /login --> POST = success/fail
DONE /register --> POST = user
DONE profile/:userId --> GET = user
/projects/:projectId --> GET = project
DONE /projects --> POST = project & project_user
DONE /projects --> GET = projects
/projects/:projectId/tickets/:ticketId --> GET = ticket
/projects/:projectId/tickets --> POST = ticket
/projects/:projectIdtickets --> GET = tickets
/projects/:projectId/tickets/:ticketId/comments --> POST = comment
/projects/:projectId/tickets/:ticketId/comments --> GET = comments
/projects/:projectId/project-users/:userId --> POST = project_user
/projects/:projectId/project-users/:userId --> GET =project_user
/projects/:projectId/project-users --> GET = project_users
/project-invitations
*/