const getPostIfExist = (postId: number, authorId: number) => {
return Effect.succeed(postId).pipe(
Effect.filterOrFail(
(x) => !Number.isNaN(x),
() => new Err("PostIdIsNaNError")
),
Effect.tryMapPromise({
try: () => pg.select().from(posts).where(eq(posts.id, postId)),
catch: () => new Err("DbQueryConnectionError"),
}),
Effect.filterOrFail(
(x) => x.length === 1,
() => new Err("PostNotFound")
),
Effect.filterOrFail(
(x) => x[0].authorId === authorId,
() => new Err("PostNotBelongToUserError")
),
Effect.map((item) => ({ ok: true as const, post: item[0] })),
Effect.catchAll((err) => {
return Effect.succeed({ ok: false as const, err: err._tag as string });
}),
Effect.runPromise
);
};
const getPostIfExist = (postId: number, authorId: number) => {
return Effect.succeed(postId).pipe(
Effect.filterOrFail(
(x) => !Number.isNaN(x),
() => new Err("PostIdIsNaNError")
),
Effect.tryMapPromise({
try: () => pg.select().from(posts).where(eq(posts.id, postId)),
catch: () => new Err("DbQueryConnectionError"),
}),
Effect.filterOrFail(
(x) => x.length === 1,
() => new Err("PostNotFound")
),
Effect.filterOrFail(
(x) => x[0].authorId === authorId,
() => new Err("PostNotBelongToUserError")
),
Effect.map((item) => ({ ok: true as const, post: item[0] })),
Effect.catchAll((err) => {
return Effect.succeed({ ok: false as const, err: err._tag as string });
}),
Effect.runPromise
);
};