Refactor database queries.

This commit is contained in:
Mike Cao 2020-08-11 22:24:41 -07:00
parent a248f35db2
commit f4ca353b5c
24 changed files with 371 additions and 329 deletions

View file

@ -1,4 +1,4 @@
import { deleteWebsite, getWebsite } from 'lib/db';
import { deleteWebsite, getWebsiteById } from 'lib/queries';
import { useAuth } from 'lib/middleware';
import { methodNotAllowed, ok, unauthorized } from 'lib/response';
@ -10,13 +10,13 @@ export default async (req, res) => {
const website_id = +id;
if (req.method === 'GET') {
const website = await getWebsite({ website_id });
const website = await getWebsiteById(website_id);
return ok(res, website);
}
if (req.method === 'DELETE') {
const website = await getWebsite({ website_id });
const website = await getWebsiteById(website_id);
if (website.user_id === user_id || is_admin) {
await deleteWebsite(website_id);

View file

@ -1,4 +1,4 @@
import { getMetrics } from 'lib/db';
import { getMetrics } from 'lib/queries';
import { useAuth } from 'lib/middleware';
import { ok } from 'lib/response';

View file

@ -1,5 +1,5 @@
import moment from 'moment-timezone';
import { getPageviewData } from 'lib/db';
import { getPageviews } from 'lib/queries';
import { useAuth } from 'lib/middleware';
import { ok, badRequest } from 'lib/response';
@ -18,8 +18,8 @@ export default async (req, res) => {
const end = new Date(+end_at);
const [pageviews, uniques] = await Promise.all([
getPageviewData(+id, start, end, tz, unit, '*'),
getPageviewData(+id, start, end, tz, unit, 'distinct session_id'),
getPageviews(+id, start, end, tz, unit, '*'),
getPageviews(+id, start, end, tz, unit, 'distinct session_id'),
]);
return ok(res, { pageviews, uniques });

View file

@ -1,4 +1,4 @@
import { getRankings } from 'lib/db';
import { getRankings } from 'lib/queries';
import { useAuth } from 'lib/middleware';
import { ok, badRequest } from 'lib/response';