Skip to content

Commit

Permalink
Implemented book edit form
Browse files Browse the repository at this point in the history
  • Loading branch information
apfohl committed Dec 19, 2023
1 parent fc7a054 commit 0a774ca
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 5 deletions.
12 changes: 8 additions & 4 deletions HotwiredBooks/Controllers/BooksController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,16 @@ from author in collection.TryGetValue("author", out var a) ? a.Just() : Nothing
}

[HttpGet]
public ActionResult Edit(int id) =>
throw new NotImplementedException();
public async Task<ActionResult> Edit(Guid id) =>
(await booksRepository.Lookup(id))
.Match(
book => View(new BooksEditViewModel(book)),
() => throw new ArgumentException()
);

[HttpPost]
[HttpPatch, HttpPut]
[ValidateAntiForgeryToken]
public ActionResult Edit(int id, IFormCollection collection)
public ActionResult Update(Guid id, IFormCollection collection)
{
try
{
Expand Down
11 changes: 11 additions & 0 deletions HotwiredBooks/ViewComponents/BookFormViewComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using HotwiredBooks.Models;
using HotwiredBooks.ViewModels;
using Microsoft.AspNetCore.Mvc;
using MonadicBits;

namespace HotwiredBooks.ViewComponents;

public sealed class BookFormViewComponent : ViewComponent
{
public IViewComponentResult Invoke(Maybe<Book> book) => View(new BookFormViewModel(book));
}
6 changes: 6 additions & 0 deletions HotwiredBooks/ViewModels/BookFormViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
using HotwiredBooks.Models;
using MonadicBits;

namespace HotwiredBooks.ViewModels;

public sealed record BookFormViewModel(Maybe<Book> Book);
5 changes: 5 additions & 0 deletions HotwiredBooks/ViewModels/BooksEditViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
using HotwiredBooks.Models;

namespace HotwiredBooks.ViewModels;

public sealed record BooksEditViewModel(Book Book);
10 changes: 10 additions & 0 deletions HotwiredBooks/Views/Books/Edit.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@using HotwiredBooks.Extensions
@using MonadicBits
@model HotwiredBooks.ViewModels.BooksEditViewModel
@{
Layout = null;
}

<turbo-frame id="@Html.DomId(Model.Book)">
<vc:book-form book="@Model.Book.Just()"/>
</turbo-frame>
2 changes: 1 addition & 1 deletion HotwiredBooks/Views/Shared/Components/Book/Default.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<strong>Author:</strong> @Model.Author
</p>
<div class="action-left">
<form asp-page="/Index/Edit" method="get" class="button_to">
<form asp-controller="Books" asp-action="Edit" asp-route-id="@Model.Id" method="get" class="button_to">
<button class="button secondary" type="submit">Edit this book</button>
</form>
</div>
Expand Down
34 changes: 34 additions & 0 deletions HotwiredBooks/Views/Shared/Components/BookForm/Default.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
@model HotwiredBooks.ViewModels.BookFormViewModel

@{
var id = Model.Book.Map(book => book.Id).Match(id => id.ToString(), () => string.Empty);
var action = Model.Book.Match(_ => "Update", () => "Create");
var method = Model.Book.Match(_ => "put", () => "post");
var title = Model.Book.Map(book => book.Title).Match(title => title, () => string.Empty);
var author = Model.Book.Map(book => book.Author).Match(author => author, () => string.Empty);
}

<form asp-controller="Books"
asp-action="@action"
asp-route-id="@id"
method="@method"
accept-charset="UTF-8"
class="book-form">
<div class="form-field title">
<label style="display: block" for="title">Title</label>
<input type="text" name="title" id="title" value="@title"/>
</div>

<div class="form-field author">
<label style="display: block" for="author">Author</label>
<input type="text" name="author" id="author" value="@author"/>
</div>

<div class="action-left">
<a class="button secondary" asp-controller="Books" asp-action="Index">Cancel</a>
</div>

<div class="action-right">
<input type="submit" name="commit" value="Submit" class="button primary" data-disable-with="Submit"/>
</div>
</form>

0 comments on commit 0a774ca

Please sign in to comment.