Add person add/edit/delete in settings (#17)

This commit is contained in:
2026-08-23 15:26:18 -04:00
parent 5c7d34ffc7
commit e17e5b79f0
10 changed files with 222 additions and 10 deletions
+69
View File
@@ -181,6 +181,75 @@ module Category =
}
/// ~~~ PERSON ~~~
module Person =
// GET /admin/settings/people
let all : HttpHandler =
requireAccess WebLogAdmin
>=> fun next ctx -> task {
let! ppl = ctx.Data.Person.FindByWebLog ctx.WebLog.Id
return! adminBarePage "People" next ctx (Views.WebLog.personList ppl)
}
// GET /admin/settings/person/[id]/edit
let edit psnId : HttpHandler =
requireAccess WebLogAdmin
>=> fun next ctx -> task {
let! result = task {
match psnId with
| "new" -> return Some ("Add a New Person", { Person.Empty with Id = PersonId "new" })
| _ ->
match! ctx.Data.Person.FindById (PersonId psnId) ctx.WebLog.Id with
| Some it -> return Some ("Edit Person", it)
| None -> return None
}
match result with
| Some (title, psn) ->
return! Views.WebLog.personEdit (EditPersonModel.FromPerson psn)
|> adminBarePage title next ctx
| None -> return! Error.notFound next ctx
}
// POST /admin/settings/person/save
let save : HttpHandler =
requireAccess WebLogAdmin
>=> validateCsrf
>=> fun next ctx -> task {
let data = ctx.Data
let! model = ctx.BindFormAsync<EditPersonModel>()
let isNew = model.Id = "new"
let person =
if isNew then someTask { Person.Empty with Id = PersonId.Create(); WebLogId = ctx.WebLog.Id }
else data.Person.FindById (PersonId model.Id) ctx.WebLog.Id
match! person with
| Some psn ->
let updatedPsn = model.UpdatePerson psn
do! (if isNew then data.Person.Add else data.Person.Update) updatedPsn
do! CategoryCache.update ctx
do! addMessage ctx { UserMessage.Success with Message = "Person saved successfully" }
return! all next ctx
| None -> return! Error.notFound next ctx
}
// DELETE /admin/settings/person/{id}
let delete personId : HttpHandler =
requireAccess WebLogAdmin
>=> fun next ctx -> task {
let data = ctx.Data
match! data.Person.FindById (PersonId personId) ctx.WebLog.Id with
| Some psn ->
match! data.Person.Delete psn.Id psn.WebLogId with
| true ->
do! addMessage ctx { UserMessage.Success with Message = $"Person {psn.Name} deleted successfully" }
return! all next ctx
| false ->
do! addMessage ctx { UserMessage.Error with Message = $"User {psn.Name} was not deleted" }
return! all next ctx
| None -> return! Error.notFound next ctx
}
/// ~~~ REDIRECT RULES ~~~
module RedirectRules =
-4
View File
@@ -275,10 +275,6 @@ open System.Threading.Tasks
/// Create a Task with a Some result for the given object
let someTask<'T> (it: 'T) = Task.FromResult(Some it)
/// Create an absolute URL from a string that may already be an absolute URL
let absoluteUrl (url: string) (ctx: HttpContext) =
if url.StartsWith "http" then url else ctx.WebLog.AbsoluteUrl(Permalink url)
open MyWebLog.Data
+3 -3
View File
@@ -222,8 +222,8 @@ let chapters (post: Post) : HttpHandler = fun next ctx ->
let dic = Dictionary<string, obj>()
dic["startTime"] <- Math.Round(it.StartTime.TotalSeconds, 2)
it.Title |> Option.iter (fun ttl -> dic["title"] <- ttl)
it.ImageUrl |> Option.iter (fun img -> dic["img"] <- absoluteUrl img ctx)
it.Url |> Option.iter (fun url -> dic["url"] <- absoluteUrl url ctx)
it.ImageUrl |> Option.iter (fun img -> dic["img"] <- absoluteUrl img ctx.WebLog)
it.Url |> Option.iter (fun url -> dic["url"] <- absoluteUrl url ctx.WebLog)
it.IsHidden |> Option.iter (fun toc -> dic["toc"] <- not toc)
it.EndTime |> Option.iter (fun ent -> dic["endTime"] <- Math.Round(ent.TotalSeconds, 2))
it.Location |> Option.iter (fun loc ->
@@ -237,7 +237,7 @@ let chapters (post: Post) : HttpHandler = fun next ctx ->
let jsonFile = Dictionary<string, obj>()
jsonFile["version"] <- "1.2.0"
jsonFile["title"] <- post.Title
jsonFile["fileName"] <- absoluteUrl ep.Media ctx
jsonFile["fileName"] <- absoluteUrl ep.Media ctx.WebLog
if defaultArg ep.ChapterWaypoints false then jsonFile["waypoints"] <- true
jsonFile["chapters"] <- chapterData
(setContentType JSON_CHAPTERS >=> json jsonFile) next ctx
+7 -1
View File
@@ -165,6 +165,10 @@ let endpoints = [
route "" Admin.RedirectRules.all
routef "/%i" Admin.RedirectRules.edit
]
subRoute "/pe" [
route "ople" Admin.Person.all
routef "rson/%s/edit" Admin.Person.edit
]
subRoute "/tag-mapping" [
route "s" Admin.TagMapping.all
routef "/%s/edit" Admin.TagMapping.edit
@@ -202,7 +206,8 @@ let endpoints = [
routef "/%s/revision/%s/restore" Post.restoreRevision
]
subRoute "/settings" [
route "" Admin.WebLog.saveSettings
route "" Admin.WebLog.saveSettings
route "/person/save" Admin.Person.save
subRoute "/rss" [
route "" Feed.saveSettings
route "/save" Feed.saveCustomFeed
@@ -235,6 +240,7 @@ let endpoints = [
routef "/%s/revisions" Post.purgeRevisions
]
subRoute "/settings" [
routef "/person/%s" Admin.Person.delete
routef "/redirect-rules/%i" Admin.RedirectRules.delete
routef "/rss/%s" Feed.deleteCustomFeed
routef "/tag-mapping/%s" Admin.TagMapping.delete