75 lines
1.6 KiB
JavaScript
75 lines
1.6 KiB
JavaScript
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;
|
|
|
|
const site = await siteModel.getById(siteId);
|
|
|
|
if (!site) {
|
|
return res.status(404).send('Site not found');
|
|
} else {
|
|
res.json(site);
|
|
}
|
|
});
|
|
|
|
/**
|
|
* 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, 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);
|
|
}
|
|
});
|
|
|
|
/**
|
|
* 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; |