forked from effect-app/boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreatePost.ts
More file actions
32 lines (29 loc) · 1.03 KB
/
CreatePost.ts
File metadata and controls
32 lines (29 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import { UserRepo } from "#Accounts/UserRepo"
import { handlerFor } from "#api/lib/handler"
import { S } from "#resources/lib"
import { Effect } from "effect-app"
import { InvalidStateError, NotFoundError, OptimisticConcurrencyException } from "effect-app/client"
import { BlogPost, BlogPostId } from "./models.js"
import { BlogPostRepo } from "./Repo.js"
export class CreatePost extends S.Req<CreatePost>()("Blog.Create", BlogPost.pick("title", "body"), {
allowRoles: ["user"],
success: S.Struct({ id: BlogPostId }),
failure: S.Union(NotFoundError, InvalidStateError, OptimisticConcurrencyException)
}) {}
export default handlerFor(CreatePost)({
dependencies: [
BlogPostRepo.Default,
UserRepo.Default
],
effect: Effect.gen(function*() {
const blogPostRepo = yield* BlogPostRepo
const userRepo = yield* UserRepo
return (req) =>
userRepo
.getCurrentUser
.pipe(
Effect.andThen((author) => (new BlogPost({ ...req, author }, true))),
Effect.tap(blogPostRepo.save)
)
})
})