summaryrefslogtreecommitdiff
path: root/api-routes.js
blob: 9f02edb2b65b0b414c3ea95037d6a93bc81bf7eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
const express = require('express');
const Url = require('./db/url-schema.js');
const utils = require('./utils.js');
const router = express.Router();

router.all('/', async (req, res) => {
    utils.sendBadRequestError(res, 'Nothing to be done at API root.');
});

router.put('/:slug', async (req, res) => {
    try {
        const {url} = req.body;
        const slug = req.params.slug;
        if (!utils.isUrlFormatted(url))
            return utils.sendBadRequestError(res, 'URL "'+url+'" is malformatted. Must be a valid URL.');
        if (!utils.isAlphaNumeric(slug))
            return utils.sendBadRequestError(res, 'Slug "'+slug+'" is malformatted. Must be alphanumeric.');
        if (await Url.exists({slug: slug}))
            return utils.sendBadRequestError(res, 'Slug "'+slug+'" already exists. Updates are not supported at this time.');
        
        await new Url({url, slug}).save();
        utils.sendSuccessfulSlugCreation(res, slug);
    } catch (err) {
        utils.sendInternalServerError(res);
    }
});

router.all('/*', async (req, res) => {
    utils.sendNotFoundError(res);
});

module.exports = router;