Skip to content

Commit

Permalink
fix: Replace ID<NoteID> to ID<Note>
Browse files Browse the repository at this point in the history
  • Loading branch information
laminne committed Jun 11, 2024
1 parent c534f14 commit 38ea128
Show file tree
Hide file tree
Showing 25 changed files with 85 additions and 111 deletions.
7 changes: 3 additions & 4 deletions pkg/notes/adaptor/controller/bookmark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import type { z } from '@hono/zod-openapi';
import { type Option, Result } 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 { NoteID } from '../../model/note.js';
import type { CreateBookmarkService } from '../../service/createBookmark.js';
Expand All @@ -24,7 +23,7 @@ export class BookmarkController {
Result.Result<Error, z.infer<typeof CreateBookmarkResponseSchema>>
> {
const res = await this.createBookmarkService.handle(
noteID as ID<NoteID>,
noteID as NoteID,

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L26 was not covered by tests
accountID as AccountID,
);

Expand All @@ -47,7 +46,7 @@ export class BookmarkController {
accountID: string,
): Promise<Option.Option<Bookmark>> {
const res = await this.fetchBookmarkService.fetchBookmarkByID(
noteID as ID<NoteID>,
noteID as NoteID,

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L49 was not covered by tests
accountID as AccountID,
);

Expand All @@ -69,7 +68,7 @@ export class BookmarkController {
accountID: string,
): Promise<Result.Result<Error, void>> {
const res = await this.deleteBookmarkService.handle(
noteID as ID<NoteID>,
noteID as NoteID,

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L71 was not covered by tests
accountID as AccountID,
);

Expand Down
5 changes: 2 additions & 3 deletions pkg/notes/adaptor/controller/note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { type z } from '@hono/zod-openapi';
import { Option, Result } from '@mikuroxina/mini-fn';

import type { AccountID } from '../../../accounts/model/account.js';
import type { ID } from '../../../id/type.js';
import type { AccountModule } from '../../../intermodule/account.js';
import type { NoteID, NoteVisibility } from '../../model/note.js';
import type { CreateService } from '../../service/create.js';
Expand Down Expand Up @@ -56,7 +55,7 @@ export class NoteController {
async getNoteByID(
noteID: string,
): Promise<Result.Result<Error, z.infer<typeof GetNoteResponseSchema>>> {
const res = await this.fetchService.fetchNoteByID(noteID as ID<NoteID>);
const res = await this.fetchService.fetchNoteByID(noteID as NoteID);

Check warning on line 58 in pkg/notes/adaptor/controller/note.ts

View check run for this annotation

Codecov / codecov/patch

pkg/notes/adaptor/controller/note.ts#L58

Added line #L58 was not covered by tests
if (Option.isNone(res)) {
return Result.err(new Error('Note not found'));
}
Expand Down Expand Up @@ -99,7 +98,7 @@ export class NoteController {
contentsWarningComment: string,
): Promise<Result.Result<Error, z.infer<typeof RenoteResponseSchema>>> {
const res = await this.renoteService.handle(
originalNoteID as ID<NoteID>,
originalNoteID as NoteID,

Check warning on line 101 in pkg/notes/adaptor/controller/note.ts

View check run for this annotation

Codecov / codecov/patch

pkg/notes/adaptor/controller/note.ts#L101

Added line #L101 was not covered by tests
content,
contentsWarningComment,
authorID as AccountID,
Expand Down
20 changes: 8 additions & 12 deletions pkg/notes/adaptor/repository/dummy.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Option, Result } from '@mikuroxina/mini-fn';

import type { AccountID } from '../../../accounts/model/account.js';
import type { ID } from '../../../id/type.js';
import { Bookmark } from '../../model/bookmark.js';
import { type Note, type NoteID } from '../../model/note.js';
import type {
Expand All @@ -10,7 +9,7 @@ import type {
} from '../../model/repository.js';

export class InMemoryNoteRepository implements NoteRepository {
private readonly notes: Map<ID<NoteID>, Note>;
private readonly notes: Map<NoteID, Note>;

constructor(notes: Note[] = []) {
this.notes = new Map(notes.map((note) => [note.getID(), note]));
Expand All @@ -21,7 +20,7 @@ export class InMemoryNoteRepository implements NoteRepository {
return Result.ok(undefined);
}

async deleteByID(id: ID<NoteID>): Promise<Result.Result<Error, void>> {
async deleteByID(id: NoteID): Promise<Result.Result<Error, void>> {
const target = await this.findByID(id);
if (Option.isNone(target)) {
return Result.err(new Error('note not found'));
Expand Down Expand Up @@ -50,7 +49,7 @@ export class InMemoryNoteRepository implements NoteRepository {
);
}

findByID(id: ID<NoteID>): Promise<Option.Option<Note>> {
findByID(id: NoteID): Promise<Option.Option<Note>> {
const res = this.notes.get(id);
if (!res) {
return Promise.resolve(Option.none());
Expand All @@ -60,12 +59,9 @@ export class InMemoryNoteRepository implements NoteRepository {
}

export class InMemoryBookmarkRepository implements BookmarkRepository {
private readonly bookmarks: Map<[ID<NoteID>, AccountID], Bookmark>;
private readonly bookmarks: Map<[NoteID, AccountID], Bookmark>;

private equalID(
a: [ID<NoteID>, AccountID],
b: [ID<NoteID>, AccountID],
): boolean {
private equalID(a: [NoteID, AccountID], b: [NoteID, AccountID]): boolean {
return a[0] === b[0] && a[1] === b[1];
}

Expand All @@ -79,7 +75,7 @@ export class InMemoryBookmarkRepository implements BookmarkRepository {
}

async create(id: {
noteID: ID<NoteID>;
noteID: NoteID;
accountID: AccountID;
}): Promise<Result.Result<Error, void>> {
const bookmark = Bookmark.new(id);
Expand All @@ -88,7 +84,7 @@ export class InMemoryBookmarkRepository implements BookmarkRepository {
}

async deleteByID(id: {
noteID: ID<NoteID>;
noteID: NoteID;
accountID: AccountID;
}): Promise<Result.Result<Error, void>> {
const key = Array.from(this.bookmarks.keys()).find((k) =>
Expand All @@ -104,7 +100,7 @@ export class InMemoryBookmarkRepository implements BookmarkRepository {
}

async findByID(id: {
noteID: ID<NoteID>;
noteID: NoteID;
accountID: AccountID;
}): Promise<Option.Option<Bookmark>> {
const bookmark = Array.from(this.bookmarks.entries()).find((v) =>
Expand Down
19 changes: 9 additions & 10 deletions pkg/notes/adaptor/repository/prisma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { type Prisma, type PrismaClient } from '@prisma/client';

import type { AccountID } from '../../../accounts/model/account.js';
import type { prismaClient } from '../../../adaptors/prisma.js';
import type { ID } from '../../../id/type.js';
import { Bookmark } from '../../model/bookmark.js';
import { Note, type NoteID, type NoteVisibility } from '../../model/note.js';
import type {
Expand Down Expand Up @@ -67,14 +66,14 @@ export class PrismaNoteRepository implements NoteRepository {
}
};
return Note.reconstruct({
id: data.id as ID<NoteID>,
id: data.id as NoteID,

Check warning on line 69 in pkg/notes/adaptor/repository/prisma.ts

View check run for this annotation

Codecov / codecov/patch

pkg/notes/adaptor/repository/prisma.ts#L69

Added line #L69 was not covered by tests
content: data.text,
authorID: data.authorId as AccountID,
createdAt: data.createdAt,
deletedAt: !data.deletedAt ? Option.none() : Option.some(data.deletedAt),
contentsWarningComment: '',
originalNoteID: !data.renoteId
? Option.some(data.renoteId as ID<NoteID>)
? Option.some(data.renoteId as NoteID)

Check warning on line 76 in pkg/notes/adaptor/repository/prisma.ts

View check run for this annotation

Codecov / codecov/patch

pkg/notes/adaptor/repository/prisma.ts#L76

Added line #L76 was not covered by tests
: Option.none(),
// ToDo: add SendTo field to schema
sendTo: Option.none(),
Expand All @@ -95,7 +94,7 @@ export class PrismaNoteRepository implements NoteRepository {
return Result.ok(undefined);
}

async deleteByID(id: ID<NoteID>): Promise<Result.Result<Error, void>> {
async deleteByID(id: NoteID): Promise<Result.Result<Error, void>> {

Check warning on line 97 in pkg/notes/adaptor/repository/prisma.ts

View check run for this annotation

Codecov / codecov/patch

pkg/notes/adaptor/repository/prisma.ts#L97

Added line #L97 was not covered by tests
try {
// NOTE: logical delete
await this.client.note.update({
Expand Down Expand Up @@ -135,7 +134,7 @@ export class PrismaNoteRepository implements NoteRepository {
}
}

async findByID(id: ID<NoteID>): Promise<Option.Option<Note>> {
async findByID(id: NoteID): Promise<Option.Option<Note>> {

Check warning on line 137 in pkg/notes/adaptor/repository/prisma.ts

View check run for this annotation

Codecov / codecov/patch

pkg/notes/adaptor/repository/prisma.ts#L137

Added line #L137 was not covered by tests
try {
const res = await this.client.note.findUniqueOrThrow({
where: {
Expand All @@ -155,7 +154,7 @@ export class PrismaBookmarkRepository implements BookmarkRepository {
constructor(private readonly client: PrismaClient) {}

async create(id: {
noteID: ID<NoteID>;
noteID: NoteID;

Check warning on line 157 in pkg/notes/adaptor/repository/prisma.ts

View check run for this annotation

Codecov / codecov/patch

pkg/notes/adaptor/repository/prisma.ts#L157

Added line #L157 was not covered by tests
accountID: AccountID;
}): Promise<Result.Result<Error, void>> {
try {
Expand All @@ -172,7 +171,7 @@ export class PrismaBookmarkRepository implements BookmarkRepository {
}

async deleteByID(id: {
noteID: ID<NoteID>;
noteID: NoteID;

Check warning on line 174 in pkg/notes/adaptor/repository/prisma.ts

View check run for this annotation

Codecov / codecov/patch

pkg/notes/adaptor/repository/prisma.ts#L174

Added line #L174 was not covered by tests
accountID: AccountID;
}): Promise<Result.Result<Error, void>> {
try {
Expand Down Expand Up @@ -204,7 +203,7 @@ export class PrismaBookmarkRepository implements BookmarkRepository {
return Option.some(
res.map((v) =>
Bookmark.new({
noteID: v.noteId as ID<NoteID>,
noteID: v.noteId as NoteID,

Check warning on line 206 in pkg/notes/adaptor/repository/prisma.ts

View check run for this annotation

Codecov / codecov/patch

pkg/notes/adaptor/repository/prisma.ts#L206

Added line #L206 was not covered by tests
accountID: v.accountId as AccountID,
}),
),
Expand All @@ -215,7 +214,7 @@ export class PrismaBookmarkRepository implements BookmarkRepository {
}

async findByID(id: {
noteID: ID<NoteID>;
noteID: NoteID;

Check warning on line 217 in pkg/notes/adaptor/repository/prisma.ts

View check run for this annotation

Codecov / codecov/patch

pkg/notes/adaptor/repository/prisma.ts#L217

Added line #L217 was not covered by tests
accountID: AccountID;
}): Promise<Option.Option<Bookmark>> {
try {
Expand All @@ -230,7 +229,7 @@ export class PrismaBookmarkRepository implements BookmarkRepository {
});
return Option.some(
Bookmark.new({
noteID: res.noteId as ID<NoteID>,
noteID: res.noteId as NoteID,

Check warning on line 232 in pkg/notes/adaptor/repository/prisma.ts

View check run for this annotation

Codecov / codecov/patch

pkg/notes/adaptor/repository/prisma.ts#L232

Added line #L232 was not covered by tests
accountID: res.accountId as AccountID,
}),
);
Expand Down
3 changes: 1 addition & 2 deletions pkg/notes/model/bookmark.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { describe, expect, it } from 'vitest';

import type { AccountID } from '../../accounts/model/account.js';
import type { ID } from '../../id/type.js';
import { Bookmark, type CreateBookmarkArgs } from './bookmark.js';
import { type NoteID } from './note.js';

const exampleInput: CreateBookmarkArgs = {
noteID: '1' as ID<NoteID>,
noteID: '1' as NoteID,
accountID: '2' as AccountID,
};

Expand Down
7 changes: 3 additions & 4 deletions pkg/notes/model/bookmark.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import type { AccountID } from '../../accounts/model/account.js';
import type { ID } from '../../id/type.js';
import type { NoteID } from './note.js';

export interface CreateBookmarkArgs {
noteID: ID<NoteID>;
noteID: NoteID;
accountID: AccountID;
}

Expand All @@ -17,9 +16,9 @@ export class Bookmark {
return new Bookmark(arg);
}

private readonly noteID: ID<NoteID>;
private readonly noteID: NoteID;

getNoteID(): ID<NoteID> {
getNoteID(): NoteID {
return this.noteID;
}

Expand Down
3 changes: 1 addition & 2 deletions pkg/notes/model/note.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ import { Option } from '@mikuroxina/mini-fn';
import { describe, expect, it } from 'vitest';

import type { AccountID } from '../../accounts/model/account.js';
import type { ID } from '../../id/type.js';
import { type CreateNoteArgs, Note, type NoteID } from './note.js';

const exampleInput: CreateNoteArgs = {
id: '1' as ID<NoteID>,
id: '1' as NoteID,
authorID: '2' as AccountID,
content: 'hello world!',
createdAt: new Date('2023-09-10T00:00:00.000Z'),
Expand Down
14 changes: 7 additions & 7 deletions pkg/notes/model/note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ import { Option } from '@mikuroxina/mini-fn';
import type { AccountID } from '../../accounts/model/account.js';
import type { ID } from '../../id/type.js';

export type NoteID = string;
export type NoteID = ID<Note>;
export type NoteVisibility = 'PUBLIC' | 'HOME' | 'FOLLOWERS' | 'DIRECT';

export interface CreateNoteArgs {
id: ID<NoteID>;
id: NoteID;
authorID: AccountID;
content: string;
visibility: NoteVisibility;
contentsWarningComment: string;
sendTo: Option.Option<AccountID>;
originalNoteID: Option.Option<ID<NoteID>>;
originalNoteID: Option.Option<NoteID>;
createdAt: Date;
updatedAt: Option.Option<Date>;
deletedAt: Option.Option<Date>;
Expand Down Expand Up @@ -52,8 +52,8 @@ export class Note {
return new Note(arg);
}

private readonly id: ID<NoteID>;
getID(): ID<NoteID> {
private readonly id: NoteID;
getID(): NoteID {
return this.id;
}

Expand Down Expand Up @@ -82,8 +82,8 @@ export class Note {
return this.sendTo;
}

private readonly originalNoteID: Option.Option<ID<NoteID>>;
getOriginalNoteID(): Option.Option<ID<NoteID>> {
private readonly originalNoteID: Option.Option<NoteID>;
getOriginalNoteID(): Option.Option<NoteID> {
return this.originalNoteID;
}

Expand Down
11 changes: 5 additions & 6 deletions pkg/notes/model/repository.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { Option, Result } from '@mikuroxina/mini-fn';

import type { AccountID } from '../../accounts/model/account.js';
import type { ID } from '../../id/type.js';
import type { Bookmark } from './bookmark.js';
import type { Note, NoteID } from './note.js';

Expand All @@ -11,22 +10,22 @@ export interface NoteRepository {
authorID: AccountID,
limit: number,
): Promise<Option.Option<Note[]>>;
findByID(id: ID<NoteID>): Promise<Option.Option<Note>>;
deleteByID(id: ID<NoteID>): Promise<Result.Result<Error, void>>;
findByID(id: NoteID): Promise<Option.Option<Note>>;
deleteByID(id: NoteID): Promise<Result.Result<Error, void>>;
}

export interface BookmarkRepository {
create(id: {
noteID: ID<NoteID>;
noteID: NoteID;
accountID: AccountID;
}): Promise<Result.Result<Error, void>>;
findByID(id: {
noteID: ID<NoteID>;
noteID: NoteID;
accountID: AccountID;
}): Promise<Option.Option<Bookmark>>;
findByAccountID(id: AccountID): Promise<Option.Option<Bookmark[]>>;
deleteByID(id: {
noteID: ID<NoteID>;
noteID: NoteID;
accountID: AccountID;
}): Promise<Result.Result<Error, void>>;
}
5 changes: 2 additions & 3 deletions pkg/notes/service/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Option, Result } from '@mikuroxina/mini-fn';

import type { AccountID } from '../../accounts/model/account.js';
import type { SnowflakeIDGenerator } from '../../id/mod.js';
import type { ID } from '../../id/type.js';
import { Note, type NoteID, type NoteVisibility } from '../model/note.js';
import type { NoteRepository } from '../model/repository.js';

Expand All @@ -14,13 +13,13 @@ export class CreateService {
authorID: AccountID,
visibility: NoteVisibility,
): Promise<Result.Result<Error, Note>> {
const id = this.idGenerator.generate<NoteID>();
const id = this.idGenerator.generate<Note>();
if (Result.isErr(id)) {
return id;
}
try {
const note = Note.new({
id: id[1] as ID<NoteID>,
id: id[1] as NoteID,
content: content,
contentsWarningComment: contentsWarningComment,
createdAt: new Date(),
Expand Down
Loading

0 comments on commit 38ea128

Please sign in to comment.