From 6a5285ca544aa9c2dacc60a1b0b2d5f2c2eabb53 Mon Sep 17 00:00:00 2001 From: "Daniel J. Summers" Date: Sun, 3 Mar 2024 22:23:43 -0500 Subject: [PATCH] WIP on saving chapters (#6) --- src/MyWebLog.Domain/ViewModels.fs | 1 + src/MyWebLog/Handlers/Post.fs | 31 +++++++++++++++++++++++++++++++ src/MyWebLog/Handlers/Routes.fs | 1 + 3 files changed, 33 insertions(+) diff --git a/src/MyWebLog.Domain/ViewModels.fs b/src/MyWebLog.Domain/ViewModels.fs index 098ee5c..afb672b 100644 --- a/src/MyWebLog.Domain/ViewModels.fs +++ b/src/MyWebLog.Domain/ViewModels.fs @@ -362,6 +362,7 @@ type EditCategoryModel = { /// View model to add/edit an episode chapter +[] type EditChapterModel = { /// The ID of the post to which the chapter belongs PostId: string diff --git a/src/MyWebLog/Handlers/Post.fs b/src/MyWebLog/Handlers/Post.fs index 0b1c670..282e9fb 100644 --- a/src/MyWebLog/Handlers/Post.fs +++ b/src/MyWebLog/Handlers/Post.fs @@ -409,6 +409,37 @@ let editChapter (postId, index) : HttpHandler = requireAccess Author >=> fun nex | Some _ | None -> return! Error.notFound next ctx } +// POST /admin/post/{id}/chapter/{idx} +let saveChapter (postId, index) : HttpHandler = requireAccess Author >=> fun next ctx -> task { + let data = ctx.Data + match! data.Post.FindById (PostId postId) ctx.WebLog.Id with + | Some post + when Option.isSome post.Episode + && Option.isSome post.Episode.Value.Chapters + && canEdit post.AuthorId ctx -> + let! form = ctx.BindFormAsync() + let chapters = post.Episode.Value.Chapters.Value + if index = -1 || (index >= 0 && index < List.length chapters) then + let updatedPost = + { post with + Episode = Some { + post.Episode.Value with + Chapters = + form.ToChapter() :: (if index = -1 then chapters else chapters |> List.removeAt index) + |> List.sortBy _.StartTime + |> Some } } + do! data.Post.Update updatedPost + do! addMessage ctx { UserMessage.Success with Message = "Chapter saved successfully" } + // TODO: handle "add another", only return chapter list vs. entire page with title + return! + hashForPage "Manage Chapters" + |> withAntiCsrf ctx + |> addToHash ViewContext.Model (ManageChaptersModel.Create updatedPost) + |> adminView "chapters" next ctx + else return! Error.notFound next ctx + | Some _ | None -> return! Error.notFound next ctx +} + // POST /admin/post/save let save : HttpHandler = requireAccess Author >=> fun next ctx -> task { let! model = ctx.BindFormAsync() diff --git a/src/MyWebLog/Handlers/Routes.fs b/src/MyWebLog/Handlers/Routes.fs index 5262fc2..8216f07 100644 --- a/src/MyWebLog/Handlers/Routes.fs +++ b/src/MyWebLog/Handlers/Routes.fs @@ -178,6 +178,7 @@ let router : HttpHandler = choose [ subRoute "/post" (choose [ route "/save" >=> Post.save route "/permalinks" >=> Post.savePermalinks + routef "/%s/chapter/%i" Post.saveChapter routef "/%s/delete" Post.delete routef "/%s/revision/%s/delete" Post.deleteRevision routef "/%s/revision/%s/restore" Post.restoreRevision