1- import { BlogPostRepo } from "@/services.js"
1+ import { NotFoundError } from "@/errors.js"
2+ import { BlogPostRepo , Operations } from "@/services.js"
23import { BlogPost } from "@effect-app-boilerplate/models/Blog"
34import { BlogRsc } from "@effect-app-boilerplate/resources"
5+ import { PositiveInt } from "@effect-app/prelude/schema"
46
57const { controllers, matchWithServices } = matchFor ( BlogRsc )
68
@@ -24,4 +26,47 @@ const CreatePost = matchWithServices("CreatePost")(
2426 . map ( _ => _ . id )
2527)
2628
27- export const BlogControllers = controllers ( Effect . struct ( { FindPost, GetPosts, CreatePost } ) )
29+ const PublishPost = matchWithServices ( "PublishPost" ) (
30+ { BlogPostRepo, Operations } ,
31+ ( req , { BlogPostRepo, Operations } ) =>
32+ Do ( $ => {
33+ $ (
34+ BlogPostRepo . find ( req . id )
35+ . flatMap ( _ => _ . encaseInEffect ( ( ) => new NotFoundError ( "BlogPost" , req . id ) ) )
36+ )
37+
38+ const targets = [
39+ "google" ,
40+ "twitter" ,
41+ "facebook"
42+ ]
43+
44+ const done : string [ ] = [ ]
45+
46+ const operationId = $ (
47+ Effect . forkOperation (
48+ opId =>
49+ Operations . update ( opId , {
50+ total : PositiveInt ( targets . length ) ,
51+ completed : PositiveInt ( done . length )
52+ } ) >
53+ targets
54+ . forEachEffect ( _ =>
55+ Effect ( done . push ( _ ) )
56+ . tap ( ( ) =>
57+ Operations . update ( opId , {
58+ total : PositiveInt ( targets . length ) ,
59+ completed : PositiveInt ( done . length )
60+ } )
61+ )
62+ . delay ( Duration . seconds ( 4 ) )
63+ )
64+ . map ( ( ) => "the answer to the universe is 41" )
65+ )
66+ )
67+
68+ return operationId
69+ } )
70+ )
71+
72+ export const BlogControllers = controllers ( Effect . struct ( { FindPost, GetPosts, CreatePost, PublishPost } ) )
0 commit comments