Skip to content

Commit

Permalink
refactor: barks => memes (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
Xunnamius committed Jul 3, 2021
1 parent ff6bd6b commit c48a1c6
Show file tree
Hide file tree
Showing 11 changed files with 121 additions and 108 deletions.
3 changes: 2 additions & 1 deletion src/backend/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,15 @@ export async function initializeDb(db: Db) {
// ]);
}

// TODO: XXX: turn this into a package of some sort (and abstract away key type)
/**
* Checks if an item identified by some `key` (default identifier is `"_id"`)
* exists within `collection`.
*/
export async function itemExists(
collection: Collection,
id: ObjectId,
key?: '_id' | 'owner' | 'rebarkOf' | 'barkbackTo'
key?: '_id' | 'owner' | 'receiver' | 'replyTo'
): Promise<boolean>;
export async function itemExists(
collection: Collection,
Expand Down
26 changes: 14 additions & 12 deletions src/pages/api/v1/memes/[...meme_ids].ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,45 @@
import { wrapHandler } from 'universe/backend/middleware';
import { getBarks, deleteBarks } from 'universe/backend';
import { getMemes, updateMemes } from 'universe/backend';
import { itemToObjectId } from 'universe/backend/db';
import { sendHttpOk } from 'multiverse/next-respond';
import { NotFoundError, ValidationError } from 'universe/backend/error';
import { ObjectId } from 'mongodb';

import type { NextApiResponse, NextApiRequest } from 'next';
import type { MemeId } from 'types/global';

// ? This is a NextJS special "config" export
export { defaultConfig as config } from 'universe/backend/middleware';

export default async function (req: NextApiRequest, res: NextApiResponse) {
await wrapHandler(
async ({ req, res }) => {
let bark_ids: ObjectId[] | undefined = undefined;
let meme_ids: MemeId[] | undefined = undefined;

try {
bark_ids = itemToObjectId(
Array.from(new Set<string>(req.query.bark_ids as string[]))
meme_ids = itemToObjectId(
Array.from(new Set<string>(req.query.meme_ids as string[]))
);
} catch {
throw new ValidationError('invalid bark_id(s)');
throw new ValidationError('invalid meme_id(s)');
}

if (req.method == 'GET') {
const barks = await getBarks({ bark_ids });
const memes = await getMemes({ meme_ids });

if (barks.length != bark_ids.length) {
throw new NotFoundError('duplicate bark_id(s)');
} else sendHttpOk(res, { barks });
if (memes.length != meme_ids.length) {
throw new NotFoundError('duplicate meme_id(s)');
} else sendHttpOk(res, { memes });
} else {
await deleteBarks({ bark_ids });
// * PUT
// TODO: validation
await updateMemes({ meme_ids, data: {} });
sendHttpOk(res);
}
},
{
req,
res,
methods: ['GET', 'DELETE'],
methods: ['GET', 'PUT'],
apiVersion: 1
}
);
Expand Down
18 changes: 10 additions & 8 deletions src/pages/api/v1/memes/[meme_id]/likes/[user_id].ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
import { wrapHandler } from 'universe/backend/middleware';
import { isBarkLiked, likeBark, unlikeBark } from 'universe/backend';
import { isMemeLiked, addLikedMeme, removeLikedMeme } from 'universe/backend';
import { sendHttpOk, sendHttpNotFound } from 'multiverse/next-respond';
import { ValidationError } from 'universe/backend/error';
import { ObjectId } from 'mongodb';

import type { NextApiResponse, NextApiRequest } from 'next';
import type { MemeId, UserId } from 'types/global';

// ? This is a NextJS special "config" export
export { defaultConfig as config } from 'universe/backend/middleware';

export default async function (req: NextApiRequest, res: NextApiResponse) {
await wrapHandler(
async ({ req, res }) => {
let bark_id: ObjectId | undefined = undefined;
let user_id: ObjectId | undefined = undefined;
let meme_id: MemeId | undefined = undefined;
let user_id: UserId | undefined = undefined;

try {
bark_id = new ObjectId(req.query.bark_id.toString());
meme_id = new ObjectId(req.query.meme_id.toString());
} catch {
throw new ValidationError(`invalid bark_id "${req.query.bark_id.toString()}"`);
throw new ValidationError(`invalid meme_id "${req.query.meme_id.toString()}"`);
}

try {
Expand All @@ -28,14 +29,15 @@ export default async function (req: NextApiRequest, res: NextApiResponse) {
}

if (req.method == 'GET') {
(await isBarkLiked({ bark_id, user_id }))
(await isMemeLiked({ meme_id, user_id }))
? sendHttpOk(res)
: sendHttpNotFound(res);
} else if (req.method == 'DELETE') {
await unlikeBark({ bark_id, user_id });
await removeLikedMeme({ meme_id, user_id });
sendHttpOk(res);
} else {
await likeBark({ bark_id, user_id });
// * PUT
await addLikedMeme({ meme_id, user_id });
sendHttpOk(res);
}
},
Expand Down
22 changes: 13 additions & 9 deletions src/pages/api/v1/memes/[meme_id]/likes/index.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,38 @@
import { getBarkLikesUserIds } from 'universe/backend';
import { getMemeLikesUserIds } from 'universe/backend';
import { sendHttpOk } from 'multiverse/next-respond';
import { wrapHandler } from 'universe/backend/middleware';
import { ObjectId } from 'mongodb';

import { ValidationError } from 'universe/backend/error';

import type { NextApiResponse, NextApiRequest } from 'next';
import type { MemeId, UserId } from 'types/global';

// ? This is a NextJS special "config" export
export { defaultConfig as config } from 'universe/backend/middleware';

export default async function (req: NextApiRequest, res: NextApiResponse) {
await wrapHandler(
async ({ req, res }) => {
let after: ObjectId | null | undefined = undefined;
let bark_id: ObjectId | undefined = undefined;
let after: UserId | null | undefined = undefined;
let meme_id: MemeId | undefined = undefined;

try {
after = req.query.after ? new ObjectId(req.query.after.toString()) : null;
} catch {
throw new ValidationError(`invalid bark_id "${req.query.after.toString()}"`);
throw new ValidationError(`invalid meme_id "${req.query.after.toString()}"`);
}

try {
bark_id = new ObjectId(req.query.bark_id.toString());
meme_id = new ObjectId(req.query.meme_id.toString());
} catch {
throw new ValidationError(`invalid bark_id "${req.query.bark_id.toString()}"`);
throw new ValidationError(`invalid meme_id "${req.query.meme_id.toString()}"`);
}

if (bark_id !== undefined) {
sendHttpOk(res, { users: await getBarkLikesUserIds({ bark_id, after }) });
// * GET
if (meme_id !== undefined) {
sendHttpOk(res, {
users: await getMemeLikesUserIds({ meme_id, after })
});
}
},
{
Expand Down
12 changes: 7 additions & 5 deletions src/pages/api/v1/memes/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { wrapHandler } from 'universe/backend/middleware';
import { createBark, searchBarks } from 'universe/backend';
import { createMeme, searchMemes } from 'universe/backend';
import { sendHttpOk } from 'multiverse/next-respond';
import { ValidationError } from 'universe/backend/error';
import { ObjectId } from 'mongodb';

import type { NextApiResponse, NextApiRequest } from 'next';
import type { MemeId } from 'types/global';

// ? This is a NextJS special "config" export
export { defaultConfig as config } from 'universe/backend/middleware';
Expand All @@ -13,23 +14,24 @@ export default async function (req: NextApiRequest, res: NextApiResponse) {
await wrapHandler(
async ({ req, res }) => {
const key = req.headers.key?.toString() || '';
let after: ObjectId | null | undefined = undefined;
let after: MemeId | null | undefined = undefined;

try {
after = req.query.after ? new ObjectId(req.query.after.toString()) : null;
} catch {
throw new ValidationError(`invalid bark_id "${req.query.after.toString()}"`);
throw new ValidationError(`invalid meme_id "${req.query.after.toString()}"`);
}

if (req.method == 'GET') {
sendHttpOk(res, {
barks: await searchBarks({
memes: await searchMemes({
after,
match: {},
regexMatch: {}
})
});
} else sendHttpOk(res, { bark: await createBark({ key, data: req.body }) });
} else
sendHttpOk(res, { meme: await createMeme({ creatorKey: key, data: req.body }) });
},
{ req, res, methods: ['GET', 'POST'], apiVersion: 1 }
);
Expand Down
9 changes: 5 additions & 4 deletions src/pages/api/v1/memes/search.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
import { sendHttpOk } from 'multiverse/next-respond';
import { searchBarks } from 'universe/backend';
import { searchMemes } from 'universe/backend';
import { wrapHandler } from 'universe/backend/middleware';
import { ValidationError } from 'universe/backend/error';
import { ObjectId } from 'mongodb';

import type { NextApiResponse, NextApiRequest } from 'next';
import type { MemeId } from 'types/global';

// ? This is a NextJS special "config" export
export { defaultConfig as config } from 'universe/backend/middleware';

export default async function (req: NextApiRequest, res: NextApiResponse) {
await wrapHandler(
async ({ req, res }) => {
let after: ObjectId | null | undefined = undefined;
let after: MemeId | null | undefined = undefined;
let match: Record<string, unknown> | undefined = undefined;
let regexMatch: Record<string, unknown> | undefined = undefined;

try {
after = req.query.after ? new ObjectId(req.query.after.toString()) : null;
} catch {
throw new ValidationError(`invalid bark_id "${req.query.after.toString()}"`);
throw new ValidationError(`invalid meme_id "${req.query.after.toString()}"`);
}

try {
Expand All @@ -31,7 +32,7 @@ export default async function (req: NextApiRequest, res: NextApiResponse) {

if (match && regexMatch) {
sendHttpOk(res, {
barks: await searchBarks({
memes: await searchMemes({
after,
// @ts-expect-error: validation is handled
match,
Expand Down
13 changes: 7 additions & 6 deletions src/pages/api/v1/users/[user_id]/liked/[meme_id].ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
import { wrapHandler } from 'universe/backend/middleware';
import { isBarkLiked } from 'universe/backend';
import { isMemeLiked } from 'universe/backend';
import { sendHttpNotFound, sendHttpOk } from 'multiverse/next-respond';
import { ValidationError } from 'universe/backend/error';
import { ObjectId } from 'mongodb';

import type { NextApiResponse, NextApiRequest } from 'next';
import type { MemeId, UserId } from 'types/global';

// ? This is a NextJS special "config" export
export { defaultConfig as config } from 'universe/backend/middleware';

export default async function (req: NextApiRequest, res: NextApiResponse) {
await wrapHandler(
async ({ req, res }) => {
let bark_id: ObjectId | undefined = undefined;
let user_id: ObjectId | undefined = undefined;
let meme_id: MemeId | undefined = undefined;
let user_id: UserId | undefined = undefined;

try {
bark_id = new ObjectId(req.query.bark_id.toString());
meme_id = new ObjectId(req.query.meme_id.toString());
} catch {
throw new ValidationError(`invalid bark_id "${req.query.bark_id.toString()}"`);
throw new ValidationError(`invalid meme_id "${req.query.meme_id.toString()}"`);
}

try {
Expand All @@ -28,7 +29,7 @@ export default async function (req: NextApiRequest, res: NextApiResponse) {
}

if (user_id !== undefined) {
(await isBarkLiked({ bark_id, user_id }))
(await isMemeLiked({ meme_id, user_id }))
? sendHttpOk(res)
: sendHttpNotFound(res);
}
Expand Down
6 changes: 3 additions & 3 deletions src/pages/api/v1/users/[user_id]/liked/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { wrapHandler } from 'universe/backend/middleware';
import { getUserLikedBarkIds } from 'universe/backend';
import { getUserLikedMemeIds } from 'universe/backend';
import { sendHttpOk } from 'multiverse/next-respond';
import { ValidationError } from 'universe/backend/error';
import { ObjectId } from 'mongodb';
Expand All @@ -18,7 +18,7 @@ export default async function (req: NextApiRequest, res: NextApiResponse) {
try {
after = req.query.after ? new ObjectId(req.query.after.toString()) : null;
} catch {
throw new ValidationError(`invalid bark_id "${req.query.after.toString()}"`);
throw new ValidationError(`invalid meme_id "${req.query.after.toString()}"`);
}

try {
Expand All @@ -27,7 +27,7 @@ export default async function (req: NextApiRequest, res: NextApiResponse) {
throw new ValidationError(`invalid user_id "${req.query.user_id.toString()}"`);
}

sendHttpOk(res, { barks: await getUserLikedBarkIds({ user_id, after }) });
sendHttpOk(res, { memes: await getUserLikedMemeIds({ user_id, after }) });
},
{
req,
Expand Down
Loading

0 comments on commit c48a1c6

Please sign in to comment.