2020-08-12 07:24:41 +02:00
|
|
|
import { deleteWebsite, getWebsiteById } from 'lib/queries';
|
2020-08-01 04:05:14 +02:00
|
|
|
import { useAuth } from 'lib/middleware';
|
2020-08-08 02:19:42 +02:00
|
|
|
import { methodNotAllowed, ok, unauthorized } from 'lib/response';
|
2020-08-01 04:05:14 +02:00
|
|
|
|
|
|
|
export default async (req, res) => {
|
|
|
|
const { id } = req.query;
|
2020-08-08 02:19:42 +02:00
|
|
|
const website_id = +id;
|
2020-08-01 04:05:14 +02:00
|
|
|
|
2020-08-08 02:19:42 +02:00
|
|
|
if (req.method === 'GET') {
|
2020-08-12 07:24:41 +02:00
|
|
|
const website = await getWebsiteById(website_id);
|
2020-08-01 04:05:14 +02:00
|
|
|
|
2020-08-08 02:19:42 +02:00
|
|
|
return ok(res, website);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req.method === 'DELETE') {
|
2020-08-15 10:17:15 +02:00
|
|
|
await useAuth(req, res);
|
|
|
|
const { user_id, is_admin } = req.auth;
|
|
|
|
|
2020-08-12 07:24:41 +02:00
|
|
|
const website = await getWebsiteById(website_id);
|
2020-08-08 02:19:42 +02:00
|
|
|
|
|
|
|
if (website.user_id === user_id || is_admin) {
|
|
|
|
await deleteWebsite(website_id);
|
|
|
|
|
|
|
|
return ok(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
return unauthorized(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
return methodNotAllowed(res);
|
2020-08-01 04:05:14 +02:00
|
|
|
};
|