WIP on saving chapters (#6)

This commit is contained in:
Daniel J. Summers 2024-03-03 22:23:43 -05:00
parent d3c89dae5f
commit 6a5285ca54
3 changed files with 33 additions and 0 deletions

View File

@ -362,6 +362,7 @@ type EditCategoryModel = {
/// View model to add/edit an episode chapter
[<CLIMutable; NoComparison; NoEquality>]
type EditChapterModel = {
/// The ID of the post to which the chapter belongs
PostId: string

View File

@ -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<EditChapterModel>()
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<EditPostModel>()

View File

@ -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