const express = require('express'); const router = express.Router(); const SiteModel = require('../models/SiteModel'); const siteModel = new SiteModel(); /** * GET sites listing. * * Returns a list of all sites in JSON format. */ router.get('/', async function (req, res, next) { const sites = await siteModel.getAll(); res.json(sites); }); /** * GET site by ID. * * Returns a specific site by its ID in JSON format. */ router.get('/:id', async function (req, res, next) { const siteId = req.params.id; let site = null; try { site = await siteModel.getById(siteId); } catch (error) { console.error('Error fetching site by ID:', error); return res.status(400).send('Error fetching site: ' + error.message); } if (!site) { return res.status(404).send('Site not found'); } else { res.json(site); } }); /** * GET site by domain name. * * Returns a specific site by its domain name in JSON format. */ router.get('/domain/:domain', async function (req, res, next) { const domainName = req.params.domain; let sites = null; try { sites = await siteModel.getByDomainName(domainName); } catch (error) { console.error('Error fetching site by domain name:', error); return res.status(400).send('Error fetching site: ' + error.message); } if (!sites || sites.length === 0) { return res.status(404).send('Site not found'); } else { res.render('sites', { title: 'Playwright Testing Dashboard', sites: sites }); } }); /** * POST create a new site. * * Creates a new site with the provided domain name. * * // TODO: Ability to add additional site properties (e.g., name, description) */ router.post('/add', async function (req, res, next) { const domain = req.body.domain; let newSite = null; // insert method passes a lot of validation errors to the caller try { newSite = await siteModel.insert(domain); } catch (error) { console.error('Error creating site:', error); return res.status(400).send('Error creating site: ' + error.message); } if (!newSite) { res.status(400).send('Error creating site'); } else { // res.status(201).json(newSite); const sites = await siteModel.getAll(); res.render('index', { title: 'Playwright Testing Dashboard', sites: sites, msg: 'Site created successfully!' }); } }); /** * PUT update an existing site. * * Updates an existing site with the provided ID and new data. */ // TODO: Implement update functionality /** * DELETE remove a site by ID. */ // TODO: Implement delete functionality module.exports = router;