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

refactor: Repositoryの型変換をEntity型からDomainに変換するように #157

Merged
merged 3 commits into from
Aug 8, 2023
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
44 changes: 32 additions & 12 deletions src/repository/prisma/media.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Media } from "../../domain/media.js";
import { Failure, Result, Success } from "../../helpers/result.js";
import { Snowflake } from "../../helpers/id_generator.js";
import { PrismaClient } from "@prisma/client";
import { PrismaErrorConverter } from "./error.js";

export class MediaRepository implements IMediaRepository {
private prisma: PrismaClient;
Expand Down Expand Up @@ -31,7 +32,7 @@ export class MediaRepository implements IMediaRepository {
});
return new Success(this.convertToDomain(res));
} catch (e: unknown) {
return new Failure(new Error("failed to create media", e as any));
return new Failure(PrismaErrorConverter(e));
}
}

Expand All @@ -42,9 +43,9 @@ export class MediaRepository implements IMediaRepository {
id: id,
},
});
return new Success(this.convertToDomain(res));
return new Success(this.convertToDomain(res as MediaEntity));
} catch (e: unknown) {
return new Failure(new Error(e as any));
return new Failure(PrismaErrorConverter(e));
}
}

Expand All @@ -55,9 +56,11 @@ export class MediaRepository implements IMediaRepository {
postID: id,
},
});
return new Success(res.map((v: any) => this.convertToDomain(v)));
return new Success(
(res as MediaEntity[]).map((v) => this.convertToDomain(v)),
);
} catch (e: unknown) {
return new Failure(new Error(e as any));
return new Failure(PrismaErrorConverter(e));
}
}

Expand All @@ -68,21 +71,23 @@ export class MediaRepository implements IMediaRepository {
authorID: id,
},
});
return new Success(res.map((v: any) => this.convertToDomain(v)));
return new Success(
(res as MediaEntity[]).map((v) => this.convertToDomain(v)),
);
} catch (e: unknown) {
return new Failure(new Error(e as any));
return new Failure(PrismaErrorConverter(e));
}
}

async Update(m: Media): Promise<Result<Media, Error>> {
async Update(): Promise<Result<Media, Error>> {
return new Failure(new Error(""));
}

private convertToDomain(v: any): Media {
private convertToDomain(v: MediaEntity): Media {
return new Media({
id: v.id,
authorID: v.authorID,
postID: v.postID,
id: v.id as Snowflake,
authorID: v.authorID as Snowflake,
postID: v.postID as Snowflake,
blurhash: v.blurhash,
cached: v.cached,
isSensitive: v.isSensitive,
Expand All @@ -95,3 +100,18 @@ export class MediaRepository implements IMediaRepository {
});
}
}

export type MediaEntity = {
id: string;
authorID: string;
postID: string;
blurhash: string;
cached: boolean;
isSensitive: boolean;
md5Sum: string;
name: string;
size: number;
thumbnailURL: string;
type: string;
url: string;
};
134 changes: 71 additions & 63 deletions src/repository/prisma/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import { Snowflake } from "../../helpers/id_generator.js";
import { Media } from "../../domain/media.js";
import { PrismaClient } from "@prisma/client";
import { User, UserAPData, UserFollowEvent } from "../../domain/user.js";
import { MediaEntity } from "./media.js";
import { PrismaErrorConverter } from "./error.js";
import { PostReactionEntity } from "./reaction.js";

export class PostRepository implements IPostRepository {
private prisma: PrismaClient;
Expand Down Expand Up @@ -37,10 +40,10 @@ export class PostRepository implements IPostRepository {
attachments: true,
},
});
return new Success(this.convertToDomain(res));
return new Success(this.convertToDomain(res as PostEntity));
} catch (e: unknown) {
console.log(e);
return new Failure(new Error(e as any));
return new Failure(PrismaErrorConverter(e));
}
}

Expand All @@ -56,7 +59,7 @@ export class PostRepository implements IPostRepository {
});
return new Success(void "");
} catch (e: unknown) {
return new Failure(new Error("failed to delete post", e as Error as any));
return new Failure(PrismaErrorConverter(e));
}
}

Expand All @@ -75,7 +78,7 @@ export class PostRepository implements IPostRepository {

return new Success(res.map((r) => this.convertToDomain(r)));
} catch (e: unknown) {
return new Failure(new Error(e as any));
return new Failure(PrismaErrorConverter(e));
}
}

Expand All @@ -92,20 +95,19 @@ export class PostRepository implements IPostRepository {
},
});

return new Success(this.convertToDomain(res));
return new Success(this.convertToDomain(res as PostEntity));
} catch (e: unknown) {
return new Failure(new Error(e as any));
return new Failure(PrismaErrorConverter(e));
}
}

async Update(p: Post): Promise<Result<Post, Error>> {
async Update(): Promise<Result<Post, Error>> {
return new Failure(new Error(""));
}

// 時系列順にフォローしているユーザーと自分自身の投稿を取得
async ChronologicalPosts(
userID: Snowflake,
cursor: number,
): AsyncResult<{ posts: Post; author: User }[], Error> {
try {
const posts = await this.prisma.post.findMany({
Expand Down Expand Up @@ -145,35 +147,31 @@ export class PostRepository implements IPostRepository {
posts.map((p) => {
return {
posts: new Post({
attachments: !p.attachments
? new Array<Media>()
: p.attachments.map((v: any) => {
return new Media({
authorID: v.authorID,
blurhash: v.blurhash,
cached: v.cached,
id: v.id,
isSensitive: v.isSensitive,
md5Sum: v.md5Sum,
name: v.name,
postID: v.postID,
size: v.size,
thumbnailURL: v.thumbnailURL,
type: v.type,
url: v.url,
});
}),
attachments: p.attachments?.map((v: MediaEntity) => {
return new Media({
id: v.id as Snowflake,
authorID: v.authorID as Snowflake,
postID: v.postID as Snowflake,
blurhash: v.blurhash,
cached: v.cached,
isSensitive: v.isSensitive,
md5Sum: v.md5Sum,
name: v.name,
size: v.size,
thumbnailURL: v.thumbnailURL,
type: v.type,
url: v.url,
});
}),
authorID: p.authorID as Snowflake,
createdAt: p.createdAt,
id: p.id as Snowflake,
reactions: !p.reactions
? new Array<PostReactionEvent>()
: p.reactions.map((v: any) => {
return new PostReactionEvent(
v.postId as Snowflake,
v.userId as Snowflake,
);
}),
reactions: p.reactions?.map((v: PostReactionEntity) => {
return new PostReactionEvent(
v.postId as Snowflake,
v.userId as Snowflake,
);
}),
text: p.text,
visibility: 0,
}),
Expand Down Expand Up @@ -206,48 +204,58 @@ export class PostRepository implements IPostRepository {
}),
);
} catch (e: unknown) {
return new Failure(new Error(e as Error as any));
return new Failure(PrismaErrorConverter(e));
}
}

private convertToDomain(i: any): Post {
private convertToDomain(i: PostEntity): Post {
try {
return new Post({
id: i.id as Snowflake,
authorID: i.authorID,
authorID: i.authorID as Snowflake,
createdAt: i.createdAt,
text: i.text,
visibility: i.visibility,
reactions: !i.reactions
? new Array<PostReactionEvent>()
: i.reactions.map((v: any) => {
return new PostReactionEvent(
v.postId as Snowflake,
v.userId as Snowflake,
);
}),
attachments: !i.attachments
? new Array<Media>()
: i.attachments.map((v: any) => {
return new Media({
authorID: v.authorID,
blurhash: v.blurhash,
cached: v.cached,
id: v.id,
isSensitive: v.isSensitive,
md5Sum: v.md5Sum,
name: v.name,
postID: v.postID,
size: v.size,
thumbnailURL: v.thumbnailURL,
type: v.type,
url: v.url,
});
}),
reactions:
i.reactions?.map((v: PostReactionEntity): PostReactionEvent => {
return new PostReactionEvent(
v.postId as Snowflake,
v.userId as Snowflake,
);
}) ?? new Array<PostReactionEvent>(),
attachments:
i.attachments?.map((v) => {
return new Media({
id: v.id as Snowflake,
authorID: v.authorID as Snowflake,
postID: v.postID as Snowflake,
blurhash: v.blurhash,
cached: v.cached,
isSensitive: v.isSensitive,
md5Sum: v.md5Sum,
name: v.name,
size: v.size,
thumbnailURL: v.thumbnailURL,
type: v.type,
url: v.url,
});
}) ?? new Array<Media>(),
});
} catch (e: unknown) {
console.log(i.reactions, i.attachments);
throw new Error(e as any);
}
}
}

type PostEntity = {
id: string;
authorID: string;
createdAt: Date;
text: string;
visibility: number;
reactions?: PostReactionEntity[];
attachments?: AttachmentEntity[];
};

type AttachmentEntity = MediaEntity;
21 changes: 9 additions & 12 deletions src/repository/prisma/reaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { PrismaClient } from "@prisma/client";
import { PostReactionEvent } from "../../domain/post.js";
import { AsyncResult, Failure, Success } from "../../helpers/result.js";
import { Snowflake } from "../../helpers/id_generator.js";
import { PrismaErrorConverter } from "./error.js";

export class ReactionRepository implements IReactionRepository {
private readonly prisma: PrismaClient;
Expand All @@ -28,11 +29,9 @@ export class ReactionRepository implements IReactionRepository {
},
});

return new Success(this.toDomain([res]));
return new Success(this.toDomain(res as PostReactionEntity));
} catch (e: unknown) {
return new Failure(
new Error("failed to create reaction", e as Error as any),
);
return new Failure(PrismaErrorConverter(e));
}
}

Expand All @@ -50,11 +49,9 @@ export class ReactionRepository implements IReactionRepository {
},
});

return new Success(this.toDomain(res));
return new Success(this.toDomain(res as PostReactionEntity));
} catch (e: unknown) {
return new Failure(
new Error("failed to find reaction", e as Error as any),
);
return new Failure(PrismaErrorConverter(e));
}
}

Expand All @@ -70,13 +67,13 @@ export class ReactionRepository implements IReactionRepository {
});
return new Success(void "");
} catch (e: unknown) {
return new Failure(
new Error("failed to undo reaction", e as Error as any),
);
return new Failure(PrismaErrorConverter(e));
}
}

private toDomain(v: any) {
private toDomain(v: PostReactionEntity) {
return new PostReactionEvent(v.postId as Snowflake, v.userId as Snowflake);
}
}

export type PostReactionEntity = { postId: string; userId: string };
Loading