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