forked from effect-app/boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlog.Controllers.ts
More file actions
76 lines (66 loc) · 2.19 KB
/
Blog.Controllers.ts
File metadata and controls
76 lines (66 loc) · 2.19 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import { BlogPost } from "@effect-app-boilerplate/models/Blog"
import { BlogRsc } from "@effect-app-boilerplate/resources"
import { BogusEvent } from "@effect-app-boilerplate/resources/Events"
import { PositiveInt } from "@effect-app/prelude/schema"
import { BlogPostRepo, Events, Operations } from "api/services.js"
const blog = matchFor(BlogRsc)
const FindPost = blog.matchFindPost(
{ BlogPostRepo },
(req, { blogPostRepo }) =>
blogPostRepo
.find(req.id)
.map((_) => _.getOrNull)
)
const GetPosts = blog.matchGetPosts(
{ BlogPostRepo },
(_, { blogPostRepo }) => blogPostRepo.all.map((items) => ({ items }))
)
const CreatePost = blog.matchCreatePost(
{ BlogPostRepo },
(req, { blogPostRepo }) =>
Effect(new BlogPost({ ...req }))
.tap(blogPostRepo.save)
.map((_) => _.id)
)
const PublishPost = blog.matchPublishPost(
{ BlogPostRepo, Events, Operations },
(req, { blogPostRepo, events, operations }) =>
Do(($) => {
const post = $(blogPostRepo.get(req.id))
console.log("publishing post", post)
const targets = [
"google",
"twitter",
"facebook"
]
const done: string[] = []
const operationId = $(
Effect.forkOperationWithEffect(
(opId) =>
operations.update(opId, {
total: PositiveInt(targets.length),
completed: PositiveInt(done.length)
})
> targets
.forEachEffect((_) =>
Effect(done.push(_))
.tap(() =>
operations.update(opId, {
total: PositiveInt(targets.length),
completed: PositiveInt(done.length)
})
)
.delay(Duration.seconds(4))
)
.map(() => "the answer to the universe is 42"),
// while operation is running...
(_opId) =>
Effect
.suspend(() => events.publish(new BogusEvent()))
.schedule(Schedule.spaced(Duration.seconds(1)))
)
)
return operationId
})
)
export default blog.controllers({ FindPost, GetPosts, CreatePost, PublishPost })