class Ticket { constructor( id, title, description, status, created_at, updated_at ) { this.id = id; this.title = title; this.description = description; this.status = status; this.created_at = created_at; this.updated_at = updated_at; } } const Status = { OPEN: "open", IN_PROGRESS: "in_progress", CLOSED: "closed" }; var tickets = [ new Ticket(0, "ticket 1", "some desc", Status.CLOSED, 1786438392, 1786438393), new Ticket(1, "ticket 2", "some other desc", Status.OPEN, 1786438394, 1786438395), new Ticket(2, "ticket 3", "some third desc", Status.OPEN, 1786438396, 1786438397) ]; function get_ticket_by_id(id) { target = null; tickets.forEach((ticket) => { if (ticket.id == id) { target = ticket; } }); return target; } function get_tickets_by_status(status) { target = []; tickets.forEach((ticket) => { if (ticket.status == status) { target.push(ticket); } }); return target; } const express = require('express'); const app = express(); app.use(express.json()); app.post('/v1/tickets', (req, res) => { if (req.body.title == null) { res.status(400).send("error: title not provided"); return; } if (req.body.description == null) { res.status(400).send("error: description not provided"); return; } const new_title = req.body.title; const new_description = req.body.description; const creation_time = Date.now(); const ticket = new Ticket( tickets.length, new_title, new_description, Status.OPEN, creation_time, creation_time ); tickets.push(ticket); res.send(ticket); }); app.patch('/v1/tickets/:id', (req, res) => { const ticket_idx = tickets.indexOf(get_ticket_by_id(req.params.id)); const status = req.body.status; if (status == null) { res.send(tickets); return; } if (status != Status.OPEN && status != Status.CLOSED && status != Status.IN_PROGRESS) { res.status(400).send("error: no such status (only 'open', 'in_progress' or 'closed')"); } else { tickets[ticket_idx].status = status; res.send(tickets[ticket_idx]); } }); app.get('/v1/tickets', (req, res) => { const status = req.query.status; if (status == null) { res.send(tickets); return; } if (status != Status.OPEN && status != Status.CLOSED && status != Status.IN_PROGRESS) { res.status(400).send("error: no such status (only 'open', 'in_progress' or 'closed')"); } else { const tickets_with_status = get_tickets_by_status(status); res.send(tickets_with_status); } }); app.get('/v1/tickets/:id', (req, res) => { const ticket_to_send = get_ticket_by_id(req.params.id); if (ticket_to_send == null) { res.status(400).send("error: no ticket with that id exists :("); } else { res.send(ticket_to_send); } }); const port = 3000; app.listen(port, () => { console.log(`Server listening on port ${port}`); });