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 4 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
18 changes: 14 additions & 4 deletions pkg/notes/adaptor/validator/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,20 @@
}),
});

export const CreateBookmarkRequestSchema = z.object({
id: z.string().openapi({
example: '38477395',
description: 'Account ID',
}),
});

export const DeleteBookmarkRequestSchema = z.object({
id: z.string().openapi({
example: '38477395',
description: 'Account ID',
}),
});

Check warning on line 172 in pkg/notes/adaptor/validator/schema.ts

View check run for this annotation

Codecov / codecov/patch

pkg/notes/adaptor/validator/schema.ts#L159-L172

Added lines #L159 - L172 were not covered by tests
export const CreateBookmarkResponseSchema = z.object({
id: z.string().openapi({
example: '38477395',
Expand All @@ -169,10 +183,6 @@
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
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 { 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 @@
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 @@

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');
laminne marked this conversation as resolved.
Show resolved Hide resolved
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
16 changes: 16 additions & 0 deletions pkg/notes/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import { CommonErrorResponseSchema } from '../accounts/adaptor/validator/schema.js';
import {
CreateBookmarkRequestSchema,

Check warning on line 5 in pkg/notes/router.ts

View check run for this annotation

Codecov / codecov/patch

pkg/notes/router.ts#L5

Added line #L5 was not covered by tests
CreateBookmarkResponseSchema,
CreateNoteRequestSchema,
CreateNoteResponseSchema,
DeleteBookmarkRequestSchema,

Check warning on line 9 in pkg/notes/router.ts

View check run for this annotation

Codecov / codecov/patch

pkg/notes/router.ts#L9

Added line #L9 was not covered by tests
GetNoteResponseSchema,
RenoteRequestSchema,
RenoteResponseSchema,
Expand Down Expand Up @@ -150,6 +152,13 @@
example: '1',
}),
}),
body: {
content: {
'application/json': {
schema: CreateBookmarkRequestSchema,
},
},
},

Check warning on line 161 in pkg/notes/router.ts

View check run for this annotation

Codecov / codecov/patch

pkg/notes/router.ts#L155-L161

Added lines #L155 - L161 were not covered by tests
},
responses: {
200: {
Expand Down Expand Up @@ -182,6 +191,13 @@
example: '1',
}),
}),
body: {
content: {
'application/json': {
schema: DeleteBookmarkRequestSchema,
},
},
},

Check warning on line 200 in pkg/notes/router.ts

View check run for this annotation

Codecov / codecov/patch

pkg/notes/router.ts#L194-L200

Added lines #L194 - L200 were not covered by tests
},
responses: {
204: {
Expand Down