Skip to content

Commit

Permalink
feat(notes): implement bookmark API handler (#400)
Browse files Browse the repository at this point in the history
  • Loading branch information
Allianaab2m authored Jun 2, 2024
1 parent 936656c commit 4e30b98
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 10 deletions.
23 changes: 19 additions & 4 deletions pkg/notes/adaptor/controller/bookmark.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { type Option, Result } from '@mikuroxina/mini-fn';
import type { z } from '@hono/zod-openapi';
import { Result, type Option } from '@mikuroxina/mini-fn';

import type { AccountID } from '../../../accounts/model/account.js';
import type { ID } from '../../../id/type.js';
import type { Bookmark } from '../../model/bookmark.js';
import type { Note, NoteID } from '../../model/note.js';
import type { NoteID } from '../../model/note.js';
import type { CreateBookmarkService } from '../../service/createBookmark.js';
import type { DeleteBookmarkService } from '../../service/deleteBookmark.js';
import type { FetchBookmarkService } from '../../service/fetchBookmark.js';
import type { CreateBookmarkResponseSchema } from '../validator/schema.js';

export class BookmarkController {
constructor(
Expand All @@ -18,13 +20,26 @@ export class BookmarkController {
async createBookmark(
noteID: string,
accountID: string,
): Promise<Result.Result<Error, Note>> {
): Promise<
Result.Result<Error, z.infer<typeof CreateBookmarkResponseSchema>>
> {
const res = await this.createBookmarkService.handle(
noteID as ID<NoteID>,
accountID as ID<AccountID>,
);

return res;
if (Result.isErr(res)) {
return res;
}

return Result.ok({
id: res[1].getID(),
content: res[1].getContent(),
visibility: res[1].getVisibility(),
contents_warning_comment: res[1].getCwComment(),
author_id: res[1].getAuthorID(),
created_at: res[1].getCreatedAt().toUTCString(),
});
}

async getBookmarkByID(
Expand Down
4 changes: 0 additions & 4 deletions pkg/notes/adaptor/validator/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,6 @@ export const CreateBookmarkResponseSchema = z.object({
example: 'PUBLIC',
description: 'Note visibility',
}),
original_note_id: z.string().openapi({
example: '38477395',
description: 'Original note ID',
}),
contents_warning_comment: z.string().openapi({
example: 'This note contains sensitive content',
description: 'Contents warning comment',
Expand Down
63 changes: 61 additions & 2 deletions pkg/notes/mod.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,33 @@
import { OpenAPIHono } from '@hono/zod-openapi';
import { Result } from '@mikuroxina/mini-fn';

import type { AccountID } from '../accounts/model/account.js';
import { SnowflakeIDGenerator } from '../id/mod.js';
import type { ID } from '../id/type.js';
import { AccountModule } from '../intermodule/account.js';
import { BookmarkController } from './adaptor/controller/bookmark.js';
import { NoteController } from './adaptor/controller/note.js';
import { InMemoryNoteRepository } from './adaptor/repository/dummy.js';
import { CreateNoteRoute, GetNoteRoute, RenoteRoute } from './router.js';
import {
InMemoryBookmarkRepository,
InMemoryNoteRepository,
} from './adaptor/repository/dummy.js';
import {
CreateBookmarkRoute,
CreateNoteRoute,
DeleteBookmarkRoute,
GetNoteRoute,
RenoteRoute,
} from './router.js';
import { CreateService } from './service/create.js';
import { CreateBookmarkService } from './service/createBookmark.js';
import { DeleteBookmarkService } from './service/deleteBookmark.js';
import { FetchService } from './service/fetch.js';
import { FetchBookmarkService } from './service/fetchBookmark.js';
import { RenoteService } from './service/renote.js';

export const noteHandlers = new OpenAPIHono();
const noteRepository = new InMemoryNoteRepository();
const bookmarkRepository = new InMemoryBookmarkRepository();
const idGenerator = new SnowflakeIDGenerator(0, {
now: () => BigInt(Date.now()),
});
Expand All @@ -30,6 +46,19 @@ const controller = new NoteController(
accountModule,
);

// Bookmark
const createBookmarkService = new CreateBookmarkService(
bookmarkRepository,
noteRepository,
);
const fetchBookmarkService = new FetchBookmarkService(bookmarkRepository);
const deleteBookmarkService = new DeleteBookmarkService(bookmarkRepository);
const bookmarkController = new BookmarkController(
createBookmarkService,
fetchBookmarkService,
deleteBookmarkService,
);

noteHandlers.doc('/notes/doc.json', {
openapi: '3.0.0',
info: {
Expand Down Expand Up @@ -82,3 +111,33 @@ noteHandlers.openapi(RenoteRoute, async (c) => {

return c.json(res[1]);
});

noteHandlers.openapi(CreateBookmarkRoute, async (c) => {
const { id: noteID } = c.req.valid('param');
// ToDo: read AccountID from token
const res = await bookmarkController.createBookmark(
noteID,
'' as ID<AccountID>,
);

if (Result.isErr(res)) {
return c.json({ error: res[1].message }, 400);
}

return c.json(res[1]);
});

noteHandlers.openapi(DeleteBookmarkRoute, async (c) => {
const { id: noteID } = c.req.valid('param');
// ToDo: read AccountID from token
const res = await bookmarkController.deleteBookmark(
noteID,
'' as ID<AccountID>,
);

if (Result.isErr(res)) {
return c.json({ error: res[1].message }, 400);
}

return new Response(null, { status: 204 });
});

0 comments on commit 4e30b98

Please sign in to comment.