Skip to content

Commit

Permalink
Improved FormData parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
apfohl committed Dec 19, 2023
1 parent 0a774ca commit adf0878
Showing 1 changed file with 30 additions and 19 deletions.
49 changes: 30 additions & 19 deletions HotwiredBooks/Controllers/BooksController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Reactive;
using HotwiredBooks.Attributes;
using HotwiredBooks.Components;
using HotwiredBooks.Models;
Expand All @@ -12,6 +11,8 @@ namespace HotwiredBooks.Controllers;

public sealed class BooksController(IBooksRepository booksRepository) : Controller
{
private record FormData(string Title, string Author);

[HttpGet]
public async Task<ActionResult> Index() => View(new BooksIndexViewModel(
(await booksRepository.All()).OrderBy(book => book.Title)));
Expand All @@ -24,15 +25,11 @@ public async Task<ActionResult> Index() => View(new BooksIndexViewModel(
[TurboStreamResponse]
public async Task<ActionResult> Create(IFormCollection collection)
{
var formData =
from title in collection.TryGetValue("title", out var t) ? t.Just() : Nothing
from author in collection.TryGetValue("author", out var a) ? a.Just() : Nothing
select (Title: title, Author: author);

var book = await formData.Match(
fd => booksRepository.Create(fd.Title, fd.Author),
() => Task.FromResult<Maybe<Book>>(Nothing)
);
var book = await (await ParseFormData(collection))
.Match(
formData => booksRepository.Create(formData.Title, formData.Author),
() => Task.FromResult<Maybe<Book>>(Nothing)
);

return View(new BooksCreateViewModel(
book.Match(b => b, () => throw new ArgumentException()),
Expand All @@ -50,16 +47,23 @@ public async Task<ActionResult> Edit(Guid id) =>

[HttpPatch, HttpPut]
[ValidateAntiForgeryToken]
public ActionResult Update(Guid id, IFormCollection collection)
public async Task<ActionResult> Update(Guid id, IFormCollection collection)
{
try
{
return RedirectToAction(nameof(Index));
}
catch
{
throw new NotImplementedException();
}
var saveBook =
from formData in ParseFormData(collection)
from book in booksRepository.Lookup(id)
from updatedBook in booksRepository.Update(
book with
{
Title = formData.Title,
Author = formData.Author
}
)
select updatedBook;

var savedBook = await saveBook;

return Created();
}

[HttpPost]
Expand All @@ -78,4 +82,11 @@ public async Task<ActionResult> Delete(Guid id) =>
),
() => throw new ArgumentException()
);

private static Task<Maybe<FormData>> ParseFormData(IFormCollection collection) =>
Task.FromResult(
from title in collection.TryGetValue("title", out var t) ? t.Just() : Nothing
from author in collection.TryGetValue("author", out var a) ? a.Just() : Nothing
select new FormData(title, author)
);
}

0 comments on commit adf0878

Please sign in to comment.