Build sites resource routes

This commit is contained in:
Aarish
2025-05-23 23:50:27 -05:00
parent a1387db6b1
commit c1eabb9dac

67
routes/sites.js Normal file
View File

@@ -0,0 +1,67 @@
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('/', function(req, res, next) {
const sites = siteModel.getAllSites();
res.json(sites);
});
/**
* GET site by ID.
*
* Returns a specific site by its ID in JSON format.
*/
router.get('/:id', function(req, res, next) {
const siteId = req.params.id;
const site = siteModel.getSiteById(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: Implement validation for domain name format
* // TODO: Implement error handling for duplicate domains
* // TODO: Ability to add additional site properties (e.g., name, description)
*/
router.post('/add/:domain', function(req, res, next) {
const domain = req.params.domain;
const newSite = siteModel.createSite(domain);
if (!newSite) {
return 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