Skip to content

Commit

Permalink
feat(notes): implement bookmark API handler
Browse files Browse the repository at this point in the history
  • Loading branch information
Allianaab2m committed May 29, 2024
1 parent 8a2fd25 commit 45510ab
Showing 1 changed file with 53 additions and 2 deletions.
55 changes: 53 additions & 2 deletions pkg/notes/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,29 @@ import { Result } from '@mikuroxina/mini-fn';

import { SnowflakeIDGenerator } from '../id/mod.js';
import { AccountModule } from '../intermodule/account.js';
import { BookmarkController } from './adaptor/controller/bookmark.js';

Check warning on line 6 in pkg/notes/mod.ts

View check run for this annotation

Codecov / codecov/patch

pkg/notes/mod.ts#L6

Added line #L6 was not covered by tests
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';

Check warning on line 18 in pkg/notes/mod.ts

View check run for this annotation

Codecov / codecov/patch

pkg/notes/mod.ts#L8-L18

Added lines #L8 - L18 were not covered by tests
import { CreateService } from './service/create.js';
import { CreateBookmarkService } from './service/createBookmark.js';
import { DeleteBookmarkService } from './service/deleteBookmark.js';

Check warning on line 21 in pkg/notes/mod.ts

View check run for this annotation

Codecov / codecov/patch

pkg/notes/mod.ts#L20-L21

Added lines #L20 - L21 were not covered by tests
import { FetchService } from './service/fetch.js';
import { FetchBookmarkService } from './service/fetchBookmark.js';

Check warning on line 23 in pkg/notes/mod.ts

View check run for this annotation

Codecov / codecov/patch

pkg/notes/mod.ts#L23

Added line #L23 was not covered by tests
import { RenoteService } from './service/renote.js';

export const noteHandlers = new OpenAPIHono();
const noteRepository = new InMemoryNoteRepository();
const bookmarkRepository = new InMemoryBookmarkRepository();

Check warning on line 28 in pkg/notes/mod.ts

View check run for this annotation

Codecov / codecov/patch

pkg/notes/mod.ts#L28

Added line #L28 was not covered by tests
const idGenerator = new SnowflakeIDGenerator(0, {
now: () => BigInt(Date.now()),
});
Expand All @@ -30,6 +44,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,
);

Check warning on line 59 in pkg/notes/mod.ts

View check run for this annotation

Codecov / codecov/patch

pkg/notes/mod.ts#L47-L59

Added lines #L47 - L59 were not covered by tests
noteHandlers.doc('/notes/doc.json', {
openapi: '3.0.0',
info: {
Expand Down Expand Up @@ -82,3 +109,27 @@ noteHandlers.openapi(RenoteRoute, async (c) => {

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

noteHandlers.openapi(CreateBookmarkRoute, async (c) => {
const { id: noteID } = c.req.valid('param');
const { id: accountID } = c.req.valid('json');
const res = await bookmarkController.createBookmark(noteID, 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');
const { id: accountID } = c.req.valid('json');
const res = await bookmarkController.deleteBookmark(noteID, accountID);

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

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

Check warning on line 135 in pkg/notes/mod.ts

View check run for this annotation

Codecov / codecov/patch

pkg/notes/mod.ts#L112-L135

Added lines #L112 - L135 were not covered by tests

0 comments on commit 45510ab

Please sign in to comment.