Skip to content

Commit

Permalink
Replaced Select with Then
Browse files Browse the repository at this point in the history
  • Loading branch information
apfohl committed Jan 6, 2024
1 parent ae08821 commit 6152d0e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 12 deletions.
12 changes: 6 additions & 6 deletions HotwiredBooks/Controllers/BooksController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ from formData in ParseFormData(collection)
from book in booksRepository.Create(formData.Title, formData.Author)
select book
)
.Select<Book, IActionResult>(async book =>
View(new BooksCreateViewModel(book, (await booksRepository.All()).Count())))
.ThenAsync(async book =>
View(new BooksCreateViewModel(book, (await booksRepository.All()).Count())) as IActionResult)
.Else(StatusCode(500, "An unexpected error occurred on the server."));

[HttpGet]
public Task<IActionResult> Edit(Guid id) =>
booksRepository
.Lookup(id)
.Select<Book, IActionResult>(book => View(new BooksEditViewModel(book)))
.Then(book => View(new BooksEditViewModel(book)) as IActionResult)
.Else(StatusCode(500, "An unexpected error occurred on the server."));

[HttpPatch, HttpPut]
Expand All @@ -59,7 +59,7 @@ book with
)
select updatedBook
)
.Select(book => ViewComponentRenderer.RenderAsync("Book", new BooksEditViewModel(book)))
.ThenAsync(book => ViewComponentRenderer.RenderAsync("Book", new BooksEditViewModel(book)))
.Else(StatusCode(500, "An unexpected error occurred on the server."));

[HttpPost]
Expand All @@ -69,8 +69,8 @@ public Task<IActionResult> Delete(Guid id) =>
booksRepository
.Lookup(id)
.ThenAsync(booksRepository.Delete)
.Select<Book, IActionResult>(async book =>
View(new BooksDeleteViewModel(book, (await booksRepository.All()).Count())))
.ThenAsync(async book =>
View(new BooksDeleteViewModel(book, (await booksRepository.All()).Count())) as IActionResult)
.Else(StatusCode(500, "An unexpected error occurred on the server."));

private static Task<ErrorOr<FormData>> ParseFormData(IFormCollection collection) =>
Expand Down
4 changes: 2 additions & 2 deletions HotwiredBooks/HotwiredBooks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

<ItemGroup>
<PackageReference Include="bridgefield.FoundationalBits" Version="0.1.2" />
<PackageReference Include="ErrorOr" Version="1.6.0" />
<PackageReference Include="ErrorOr.Extensions" Version="1.1.0" />
<PackageReference Include="ErrorOr" Version="1.8.0" />
<PackageReference Include="ErrorOr.Extensions" Version="1.2.0" />
<PackageReference Include="Humanizer" Version="2.14.1" />
<PackageReference Include="System.Reactive" Version="6.0.0" />
</ItemGroup>
Expand Down
7 changes: 3 additions & 4 deletions HotwiredBooks/Views/Shared/Components/BookForm/Default.cshtml
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
@using ErrorOr.Extensions
@model HotwiredBooks.ViewModels.BookFormViewModel

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

<form asp-controller="Books"
Expand Down

0 comments on commit 6152d0e

Please sign in to comment.