Skip to content

Commit

Permalink
Updated ErrorOr and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
apfohl committed Jan 4, 2024
1 parent d953063 commit ae08821
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 18 deletions.
10 changes: 5 additions & 5 deletions HotwiredBooks/Controllers/BooksController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ select book
)
.Select<Book, IActionResult>(async book =>
View(new BooksCreateViewModel(book, (await booksRepository.All()).Count())))
.OrElse(StatusCode(500, "An unexpected error occurred on the server."));
.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)))
.OrElse(StatusCode(500, "An unexpected error occurred on the server."));
.Else(StatusCode(500, "An unexpected error occurred on the server."));

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

[HttpPost]
[ValidateAntiForgeryToken]
[TurboStreamResponse]
public Task<IActionResult> Delete(Guid id) =>
booksRepository
.Lookup(id)
.SelectMany(booksRepository.Delete)
.ThenAsync(booksRepository.Delete)
.Select<Book, IActionResult>(async book =>
View(new BooksDeleteViewModel(book, (await booksRepository.All()).Count())))
.OrElse(StatusCode(500, "An unexpected error occurred on the server."));
.Else(StatusCode(500, "An unexpected error occurred on the server."));

private static Task<ErrorOr<FormData>> ParseFormData(IFormCollection collection) =>
(
Expand Down
12 changes: 0 additions & 12 deletions HotwiredBooks/Extensions/FunctionalExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,4 @@ public static ErrorOr<StringValues> JustGetValue(this IFormCollection collection
collection.TryGetValue(key, out var value)
? ErrorOrFactory.From(value)
: ErrorOr<StringValues>.From([Error.NotFound()]);

public static T OrElse<T>(this ErrorOr<T> errorOr, T orElse) =>
errorOr.Match(value => value, _ => orElse);

public static T OrElse<T>(this ErrorOr<T> errorOr, Func<T> orElse) =>
errorOr.Match(value => value, _ => orElse());

public static async Task<T> OrElse<T>(this Task<ErrorOr<T>> errorOr, Func<T> orElse) =>
(await errorOr).Match(value => value, _ => orElse());

public static async Task<T> OrElse<T>(this Task<ErrorOr<T>> errorOr, T orElse) =>
(await errorOr).Match(value => value, _ => orElse);
}
2 changes: 1 addition & 1 deletion HotwiredBooks/HotwiredBooks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<ItemGroup>
<PackageReference Include="bridgefield.FoundationalBits" Version="0.1.2" />
<PackageReference Include="ErrorOr" Version="1.5.0" />
<PackageReference Include="ErrorOr" Version="1.6.0" />
<PackageReference Include="ErrorOr.Extensions" Version="1.1.0" />
<PackageReference Include="Humanizer" Version="2.14.1" />
<PackageReference Include="System.Reactive" Version="6.0.0" />
Expand Down

0 comments on commit ae08821

Please sign in to comment.