|
| 1 | +import {Db} from './db.js'; |
| 2 | +import {Program} from '@code-differently/types'; |
1 | 3 | import cors from 'cors';
|
2 |
| -import fs from 'fs'; |
3 | 4 | import express, {Express, Request, Response} from 'express';
|
4 |
| -import programs from './data/programs.json'; |
5 |
| -import { randomUUID, UUID } from 'crypto'; |
6 |
| -import {Program} from '../../types'; |
7 | 5 |
|
8 |
| -const PROGRAMS_FILE = './data/programs.json'; |
9 |
| -const UUID_PATTERN = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}$/i |
| 6 | +const UUID_PATTERN = |
| 7 | + /^[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}$/i; |
10 | 8 |
|
11 |
| -const app: Express = express(); |
| 9 | +export const createServer = (db: Db): Express => { |
| 10 | + const app: Express = express(); |
12 | 11 |
|
13 |
| -app.use(express.static('public')); |
14 |
| -app.use(express.json()) |
15 |
| -app.use(express.urlencoded({extended: true})); |
16 |
| -app.use(cors()); |
| 12 | + app.use(express.static('public')); |
| 13 | + app.use(express.json()); |
| 14 | + app.use(express.urlencoded({extended: true})); |
| 15 | + app.use(cors()); |
17 | 16 |
|
18 |
| -app.get('/programs/:id', async (req: Request, res: Response<Program>) => { |
19 |
| - if (!isUuidValid(req.params.id)) { |
20 |
| - res.status(400).send(); |
21 |
| - return; |
22 |
| - } |
23 |
| - const program = programs.find(p => p.id === req.params.id); |
| 17 | + app.get('/programs/:id', async (req: Request, res: Response<Program>) => { |
| 18 | + if (!isUuidValid(req.params.id)) { |
| 19 | + res.status(400).send(); |
| 20 | + return; |
| 21 | + } |
| 22 | + const program = await db.getProgram(req.params.id); |
24 | 23 |
|
25 |
| - if (!program) { |
26 |
| - res.status(404).send(); |
27 |
| - return; |
28 |
| - } |
| 24 | + if (!program) { |
| 25 | + res.status(404).send(); |
| 26 | + return; |
| 27 | + } |
29 | 28 |
|
30 |
| - res.status(200).send(program); |
31 |
| -}); |
| 29 | + res.status(200).send(program); |
| 30 | + }); |
32 | 31 |
|
33 |
| -function isUuidValid(uuid: string): boolean { |
34 |
| - return !!uuid && !!uuid.match(UUID_PATTERN); |
35 |
| -} |
| 32 | + function isUuidValid(uuid: string): boolean { |
| 33 | + return !!uuid && !!uuid.match(UUID_PATTERN); |
| 34 | + } |
36 | 35 |
|
37 |
| -app.get('/programs', async (req: Request, res: Response<Program[]>) => { |
38 |
| - // Send the raw data back to the client as JSON. |
39 |
| - res.status(200).send(programs); |
40 |
| -}); |
| 36 | + app.get('/programs', async (req: Request, res: Response<Program[]>) => { |
| 37 | + // Send the raw data back to the client as JSON. |
| 38 | + const programs = await db.getPrograms(); |
| 39 | + res.status(200).send(programs); |
| 40 | + }); |
41 | 41 |
|
42 |
| -app.post('/programs', async (req: Request<Partial<Program>>, res: Response) => { |
43 |
| - const newProgram = req.body; |
44 |
| - programs.push({id: randomUUID(), ...newProgram}); |
45 |
| - fs.writeFile(PROGRAMS_FILE, JSON.stringify(programs, null, 2), (err) => { |
46 |
| - if (err) return console.log(err); |
47 |
| - console.log(`Updated ${PROGRAMS_FILE}`); |
| 42 | + app.post( |
| 43 | + '/programs', |
| 44 | + async (req: Request<Partial<Program>>, res: Response) => { |
| 45 | + const newProgram = req.body; |
| 46 | + try { |
| 47 | + db.addProgram(newProgram as Program); |
| 48 | + } catch (error: unknown) { |
| 49 | + res.status(500).send({error: 'Failed to add program.'}); |
| 50 | + return; |
| 51 | + } |
| 52 | + console.log('Added new program'); |
| 53 | + res.status(201).send(); |
| 54 | + } |
| 55 | + ); |
| 56 | + |
| 57 | + const port = process.env.port || 4000; |
| 58 | + app.listen(port, () => { |
| 59 | + console.log(`Server is running on http://localhost:${port}`); |
48 | 60 | });
|
49 |
| -}); |
50 | 61 |
|
51 |
| -const port = 4000; |
52 |
| -app.listen(port, () => { |
53 |
| - console.log(`Server is running on http://localhost:${port}`); |
54 |
| -}); |
| 62 | + return app; |
| 63 | +}; |
0 commit comments