Handle website delete. Added response helper functions.

This commit is contained in:
Mike Cao 2020-08-07 17:19:42 -07:00
parent 0a411a9ad6
commit c4b75e4aec
31 changed files with 314 additions and 96 deletions

View file

@ -1,26 +1,40 @@
import { getWebsites, updateWebsite } from 'lib/db';
import { getWebsites, updateWebsite, createWebsite, getWebsite } from 'lib/db';
import { useAuth } from 'lib/middleware';
import { uuid } from 'lib/crypto';
import { ok, unauthorized, methodNotAllowed } from 'lib/response';
export default async (req, res) => {
await useAuth(req, res);
const { user_id } = req.auth;
const { user_id, is_admin } = req.auth;
const { website_id } = req.body;
if (req.method === 'GET') {
const websites = await getWebsites(user_id);
return res.status(200).json(websites);
return ok(res, websites);
}
if (req.method === 'POST') {
if (website_id) {
const { name, domain } = req.body;
const website = await updateWebsite(website_id, { name, domain });
const { name, domain } = req.body;
return res.status(200).json(website);
if (website_id) {
const website = getWebsite(website_id);
if (website.user_id === user_id || is_admin) {
await updateWebsite(website_id, { name, domain });
return ok(res);
}
return unauthorized(res);
} else {
const website_uuid = uuid();
const website = await createWebsite(user_id, { website_uuid, name, domain });
return ok(res, website);
}
}
return res.status(405).end();
return methodNotAllowed(res);
};