Skip to content

Commit c9ded4a

Browse files
authored
Merge pull request #6 from oslabs-beta/lorenc-server
refactor the users routes
2 parents 50bc5ed + c19e062 commit c9ded4a

3 files changed

Lines changed: 62 additions & 50 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ AutoDeploy/
4545
│ └── github-oauth.js # helper functions for GitHub API
4646
├── routes/
4747
│ │ └── auth.github.js # all GitHub OAuth + /me routes
48+
└── usersRoutes
4849
│ ├── server.js # Express bootstrap & route mounting
4950
│ ├── db.js # pg Pool + query() + healthCheck()
5051

server/routes/usersRoutes.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { Router } from 'express';
2+
import { z } from 'zod';
3+
import { query } from '../db.js';
4+
5+
const router = Router();
6+
7+
/** Users */
8+
const UserBody = z.object({
9+
email: z.string().email(),
10+
github_username: z.string().min(1).optional(),
11+
});
12+
13+
// Create or upsert user by email
14+
router.post('/users', async (req, res) => {
15+
const parse = UserBody.safeParse(req.body);
16+
if (!parse.success)
17+
return res.status(400).json({ error: parse.error.message });
18+
const { email, github_username } = parse.data;
19+
20+
// upsert on email; requires a unique index on users.email
21+
try {
22+
const rows = await query(
23+
`
24+
insert into users (email, github_username)
25+
values ($1, $2)
26+
on conflict (email) do update set github_username = excluded.github_username
27+
returning *;
28+
`,
29+
[email, github_username ?? null]
30+
);
31+
res.status(201).json({ user: rows[0] });
32+
} catch (e) {
33+
res.status(500).json({ error: e.message });
34+
}
35+
});
36+
37+
router.get('/users', async (_req, res) => {
38+
try {
39+
const rows = await query(
40+
`select * from users order by created_at desc limit 100;`
41+
);
42+
res.json({ users: rows });
43+
} catch (e) {
44+
res.status(500).json({ error: e.message });
45+
}
46+
});
47+
48+
export default router;

server/server.js

Lines changed: 13 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ import express from 'express';
33
import cors from 'cors';
44
import helmet from 'helmet';
55
import morgan from 'morgan';
6-
import { healthCheck, query } from './db.js';
6+
import { healthCheck } from './db.js';
77
import githubAuthRouter from './routes/auth.github.js';
8-
import { z } from 'zod';
9-
import mcpRoutes from './routes/mcp.js';
8+
import userRouter from './routes/usersRoutes.js';
109

1110
const app = express();
1211
app.use(express.json());
@@ -27,69 +26,33 @@ app.get('/db/ping', async (_req, res) => {
2726
}
2827
});
2928

30-
/** Users */
31-
const UserBody = z.object({
32-
email: z.string().email(),
33-
github_username: z.string().min(1).optional(),
34-
});
35-
36-
// Create or upsert user by email
37-
app.post('/users', async (req, res) => {
38-
const parse = UserBody.safeParse(req.body);
39-
if (!parse.success)
40-
return res.status(400).json({ error: parse.error.message });
41-
const { email, github_username } = parse.data;
42-
43-
// upsert on email; requires a unique index on users.email
44-
try {
45-
const rows = await query(
46-
`
47-
insert into users (email, github_username)
48-
values ($1, $2)
49-
on conflict (email) do update set github_username = excluded.github_username
50-
returning *;
51-
`,
52-
[email, github_username ?? null]
53-
);
54-
res.status(201).json({ user: rows[0] });
55-
} catch (e) {
56-
res.status(500).json({ error: e.message });
57-
}
58-
});
59-
60-
app.get('/users', async (_req, res) => {
61-
try {
62-
const rows = await query(
63-
`select * from users order by created_at desc limit 100;`
64-
);
65-
res.json({ users: rows });
66-
} catch (e) {
67-
res.status(500).json({ error: e.message });
68-
}
69-
});
29+
// Mount users route at /users
30+
app.use('/', userRouter);
7031

7132
// --- Request Logging Middleware ---
7233
app.use((req, res, next) => {
73-
const user = req.headers["x-user-id"] || "anonymous";
74-
console.log(`[${new Date().toISOString()}] ${req.method} ${req.originalUrl} | user=${user}`);
34+
const user = req.headers['x-user-id'] || 'anonymous';
35+
console.log(
36+
`[${new Date().toISOString()}] ${req.method} ${
37+
req.originalUrl
38+
} | user=${user}`
39+
);
7540
next();
7641
});
7742

78-
// -- Agent entry point
43+
// -- Agent entry point
7944
app.use('/mcp/v1', mcpRoutes);
8045

8146
// --- Global Error Handler ---
8247
app.use((err, req, res, next) => {
83-
console.error("Global Error:", err);
48+
console.error('Global Error:', err);
8449
res.status(500).json({
8550
success: false,
86-
error: "Internal Server Error",
51+
error: 'Internal Server Error',
8752
message: err.message,
8853
});
8954
});
9055

91-
92-
9356
// Mount GitHub OAuth routes at /auth/github
9457
app.use('/auth/github', githubAuthRouter);
9558

0 commit comments

Comments
 (0)