Skip to content

Commit

Permalink
Added creation date to book repository
Browse files Browse the repository at this point in the history
  • Loading branch information
apfohl committed Dec 19, 2023
1 parent 44947bc commit 915f2f0
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
8 changes: 4 additions & 4 deletions HotwiredBooks/Components/MemoryBasedBooksRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace HotwiredBooks.Components;

internal interface IBooksCommand;

internal sealed record Create(string Title, string Author) : IBooksCommand;
internal sealed record Create(string Title, string Author, DateTime CreatedAt) : IBooksCommand;

internal sealed record Update(Book Book) : IBooksCommand;

Expand All @@ -26,7 +26,7 @@ public MemoryBasedBooksRepository() =>
(current, command) => command switch
{
Create create => Task.FromResult((current,
from book in new Book(Guid.NewGuid(), create.Title, create.Author).Just()
from book in new Book(Guid.NewGuid(), create.Title, create.Author, create.CreatedAt).Just()
from createdBook in current.TryAdd(book.Id, book) ? book.Just() : Nothing
select createdBook)),
Delete delete => Task.FromResult((current,
Expand All @@ -49,7 +49,7 @@ public Task<IEnumerable<Book>> All() =>
Task.FromResult<IEnumerable<Book>>(books.Values);

public Task<Maybe<Book>> Create(string title, string author) =>
agent.Tell(new Create(title, author));
agent.Tell(new Create(title, author, DateTime.Now));

public Task<Maybe<Book>> Update(Book book) =>
agent.Tell(new Update(book));
Expand All @@ -71,7 +71,7 @@ private static IEnumerable<KeyValuePair<Guid, Book>> InitialBooks()

private static KeyValuePair<Guid, Book> CreateBook(string title, string author)
{
var book = new Book(Guid.NewGuid(), title, author);
var book = new Book(Guid.NewGuid(), title, author, DateTime.Now);

return KeyValuePair.Create(book.Id, book);
}
Expand Down
2 changes: 1 addition & 1 deletion HotwiredBooks/Controllers/BooksController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ private record FormData(string Title, string Author);

[HttpGet]
public async Task<IActionResult> Index() => View(new BooksIndexViewModel(
(await booksRepository.All()).OrderBy(book => book.Title)));
(await booksRepository.All()).OrderByDescending(book => book.CreatedAt)));

[HttpGet]
public IActionResult New() => View();
Expand Down
2 changes: 1 addition & 1 deletion HotwiredBooks/Models/Book.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
namespace HotwiredBooks.Models;

public sealed record Book(Guid Id, string Title, string Author);
public sealed record Book(Guid Id, string Title, string Author, DateTime CreatedAt);

0 comments on commit 915f2f0

Please sign in to comment.