Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(notes): implement bookmark API handler #400

Merged
merged 6 commits into from
Jun 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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';

Check warning on line 2 in pkg/notes/adaptor/controller/bookmark.ts

View check run for this annotation

Codecov / codecov/patch

pkg/notes/adaptor/controller/bookmark.ts#L1-L2

Added lines #L1 - L2 were not covered by tests

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';

Check warning on line 7 in pkg/notes/adaptor/controller/bookmark.ts

View check run for this annotation

Codecov / codecov/patch

pkg/notes/adaptor/controller/bookmark.ts#L7

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

Check warning on line 11 in pkg/notes/adaptor/controller/bookmark.ts

View check run for this annotation

Codecov / codecov/patch

pkg/notes/adaptor/controller/bookmark.ts#L11

Added line #L11 was not covered by tests

export class BookmarkController {
constructor(
Expand All @@ -18,13 +20,26 @@
async createBookmark(
noteID: string,
accountID: string,
): Promise<Result.Result<Error, Note>> {
): Promise<
Result.Result<Error, z.infer<typeof CreateBookmarkResponseSchema>>
> {

Check warning on line 25 in pkg/notes/adaptor/controller/bookmark.ts

View check run for this annotation

Codecov / codecov/patch

pkg/notes/adaptor/controller/bookmark.ts#L23-L25

Added lines #L23 - L25 were not covered by tests
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(),
});

Check warning on line 42 in pkg/notes/adaptor/controller/bookmark.ts

View check run for this annotation

Codecov / codecov/patch

pkg/notes/adaptor/controller/bookmark.ts#L31-L42

Added lines #L31 - L42 were not covered by tests
}

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';

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

View check run for this annotation

Codecov / codecov/patch

pkg/notes/mod.ts#L4

Added line #L4 was not covered by tests
import { SnowflakeIDGenerator } from '../id/mod.js';
import type { ID } from '../id/type.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 { AccountModule } from '../intermodule/account.js';
import { BookmarkController } from './adaptor/controller/bookmark.js';

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

View check run for this annotation

Codecov / codecov/patch

pkg/notes/mod.ts#L8

Added line #L8 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 20 in pkg/notes/mod.ts

View check run for this annotation

Codecov / codecov/patch

pkg/notes/mod.ts#L10-L20

Added lines #L10 - L20 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 23 in pkg/notes/mod.ts

View check run for this annotation

Codecov / codecov/patch

pkg/notes/mod.ts#L22-L23

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

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

View check run for this annotation

Codecov / codecov/patch

pkg/notes/mod.ts#L25

Added line #L25 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 30 in pkg/notes/mod.ts

View check run for this annotation

Codecov / codecov/patch

pkg/notes/mod.ts#L30

Added line #L30 was not covered by tests
const idGenerator = new SnowflakeIDGenerator(0, {
now: () => BigInt(Date.now()),
});
Expand All @@ -30,6 +46,19 @@
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 61 in pkg/notes/mod.ts

View check run for this annotation

Codecov / codecov/patch

pkg/notes/mod.ts#L49-L61

Added lines #L49 - L61 were not covered by tests
noteHandlers.doc('/notes/doc.json', {
openapi: '3.0.0',
info: {
Expand Down Expand Up @@ -82,3 +111,33 @@

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 });
});

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

View check run for this annotation

Codecov / codecov/patch

pkg/notes/mod.ts#L114-L143

Added lines #L114 - L143 were not covered by tests