Skip to content

Commit 8405efd

Browse files
Add delete_all action
1 parent 8b88c3f commit 8405efd

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

src/Saturn/Controller.fs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ module Controller =
1515
| Create
1616
| Update
1717
| Delete
18+
| DeleteAll
1819
| All
1920

2021
type ControllerState<'Key> = {
@@ -25,6 +26,7 @@ module Controller =
2526
Create: (HttpContext -> HttpFuncResult) option
2627
Update: (HttpContext * 'Key -> HttpFuncResult) option
2728
Delete: (HttpContext * 'Key -> HttpFuncResult) option
29+
DeleteAll: (HttpContext -> HttpFuncResult) option
2830
NotFoundHandler: HttpHandler option
2931
ErrorHandler: (HttpContext * Exception) -> HttpFuncResult
3032
SubControllers : (string * ('Key -> HttpHandler)) list
@@ -43,7 +45,7 @@ module Controller =
4345

4446
type ControllerBuilder<'Key> internal () =
4547
member __.Yield(_) : ControllerState<'Key> =
46-
{ Index = None; Show = None; Add = None; Edit = None; Create = None; Update = None; Delete = None; NotFoundHandler = None; Version = None; SubControllers = []; Plugs = Map.empty<_,_>; ErrorHandler = fun (_, ex) -> raise ex }
48+
{ Index = None; Show = None; Add = None; Edit = None; Create = None; Update = None; Delete = None; DeleteAll = None; NotFoundHandler = None; Version = None; SubControllers = []; Plugs = Map.empty<_,_>; ErrorHandler = fun (_, ex) -> raise ex }
4749

4850
member __.Run(state : ControllerState<'Key>) : HttpHandler =
4951
let typ =
@@ -89,7 +91,10 @@ module Controller =
8991
yield addPlugs Index (route "/" >=> (fun _ ctx -> state.Index.Value(ctx)))
9092
]
9193
yield POST >=> choose [
92-
if state.Create.IsSome then yield addPlugs Create (route "/" >=> (fun _ ctx -> state.Create.Value(ctx)))
94+
if state.Create.IsSome then
95+
yield addPlugs Create (route "" >=> (fun _ ctx -> ctx.Request.Path <- PathString(ctx.Request.Path.ToString() + "/"); state.Create.Value(ctx)))
96+
yield addPlugs Create (route "/" >=> (fun _ ctx -> state.Create.Value(ctx)))
97+
9398
if state.Update.IsSome then
9499
match typ with
95100
| Bool -> yield addPlugs Update (routef "/%b" (fun input _ ctx -> state.Update.Value(ctx, unbox<'Key> input)))
@@ -123,6 +128,9 @@ module Controller =
123128
| Guid -> yield addPlugs Update (routef "/%O" (fun input _ ctx -> state.Update.Value(ctx, unbox<'Key> input)))
124129
]
125130
yield DELETE >=> choose [
131+
if state.Delete.IsSome then
132+
yield addPlugs DeleteAll (route "" >=> (fun _ ctx -> ctx.Request.Path <- PathString(ctx.Request.Path.ToString() + "/"); state.DeleteAll.Value(ctx)))
133+
yield addPlugs DeleteAll (route "/" >=> (fun _ ctx -> state.DeleteAll.Value(ctx)))
126134
if state.Delete.IsSome then
127135
match typ with
128136
| Bool -> yield addPlugs Delete (routef "/%b" (fun input _ ctx -> state.Delete.Value(ctx, unbox<'Key> input)))
@@ -212,6 +220,11 @@ module Controller =
212220
member __.Delete (state : ControllerState<'Key>, handler) =
213221
{state with Delete = Some handler}
214222

223+
///Operation that deletes all items
224+
[<CustomOperation("delete_all")>]
225+
member __.DeleteAll (state : ControllerState<'Key>, handler) =
226+
{state with DeleteAll = Some handler}
227+
215228
///Define not-found handler for the controller
216229
[<CustomOperation("not_found_handler")>]
217230
member __.NotFoundHandler(state : ControllerState<'Key>, handler) =

0 commit comments

Comments
 (0)