public void LibraryItemBookmarkSetEmojiWorks() { LibraryItem item = new LibraryItem(_eventFactory, "foo/bar.mp3"); LibraryItemBookmarkAddedEvent addBookmarkEvent = _eventFactory.GetBookmarkAddedEvent(item.AggregateId, Guid.NewGuid(), TimeSpan.FromSeconds(42)); item.Apply(addBookmarkEvent); item.Bookmarks.Single(); item.Apply(_eventFactory.GetBookmarkSetEmojiEvent(item.AggregateId, addBookmarkEvent.BookmarkId, "🎶")); item.Bookmarks.Single().Emoji.Should().Be("🎶"); }
public void LibraryItemDeleteBookmarkWorks() { LibraryItem item = new LibraryItem(_eventFactory, "foo/bar.mp3"); item.Bookmarks.Count.Should().Be(0); LibraryItemBookmarkAddedEvent addBookmarkEvent = _eventFactory.GetBookmarkAddedEvent(item.AggregateId, Guid.NewGuid(), TimeSpan.FromSeconds(42)); item.Apply(addBookmarkEvent); Bookmark bookmark = item.Bookmarks.Single(); item.Apply(_eventFactory.GetBookmarkDeletedEvent(item.AggregateId, addBookmarkEvent.BookmarkId)); item.Bookmarks.Should().BeEmpty(); }
private void ItemCanBeRebuiltFromUncommittedEvents(LibraryItem item) { LibraryItem itemFromEvents = new LibraryItem(_eventFactory); IEnumerable <IEvent> events = item.GetUncommittedEvents(); InMemoryJsonEventRepository repo = new InMemoryJsonEventRepository(); foreach (IEvent @event in events) { repo.Save(@event); } itemFromEvents.Apply(repo.GetEvents(item.AggregateId)); Assert.Equal(item, itemFromEvents); }
public void LibraryItemAddBookmarkWorks() { LibraryItem item = new LibraryItem(_eventFactory, "foo/bar.mp3"); item.Bookmarks.Count.Should().Be(0); LibraryItemBookmarkAddedEvent addBookmarkEvent = _eventFactory.GetBookmarkAddedEvent(item.AggregateId, Guid.NewGuid(), TimeSpan.FromSeconds(42)); item.Apply(addBookmarkEvent); Bookmark bookmark = item.Bookmarks.Single(); bookmark.Position.Should().Be(TimeSpan.FromSeconds(42)); bookmark.Emoji.Should().BeNull(); bookmark.Comment.Should().BeNull(); }
public void CanCreateLibraryItemFromEvent() { Guid guid = Guid.NewGuid(); string name = "bar.mp3"; string path = "foo/bar.mp3"; LibraryItemCreatedEvent createdEvent = _eventFactory.GetCreatedEvent(guid, name, path); //new LibraryItemCreatedEvent(Guid.NewGuid(), guid, createdDate, name, path); LibraryItem item = new LibraryItem(_eventFactory); item.Apply(new List <IEvent>() { createdEvent }); Assert.Equal(guid, item.AggregateId); Assert.Equal(name, item.Name); Assert.Equal(path, item.FilePath); Assert.Equal(createdEvent.CreatedTimeUtc, item.CreatedTimeUtc); }
private void RebuildItems() { Stopwatch sw = Stopwatch.StartNew(); Items.Clear(); IEnumerable <IEvent> events = _repo.GetAllEvents(); IEnumerable <IOrderedEnumerable <IEvent> > groupedEvents = events.GroupBy(e => e.AggregateId).Select(g => g.OrderBy(e => e.CreatedTimeUtc).ThenBy(e => e.LocalId)); foreach (IOrderedEnumerable <IEvent> aggregateEvents in groupedEvents) { LibraryItem aggregate = new LibraryItem(_eventFactory); IEvent first = aggregateEvents.First(); if (!(first is LibraryItemCreatedEvent)) { throw new Exception($"Bad event data: first event for item {first.AggregateId} is of type {first.GetType()} not {nameof(LibraryItemCreatedEvent)}"); } aggregate.EventCreated += SaveEventToRepo; aggregate.Apply(aggregateEvents); Items.Add(aggregate); } _logger.Information("Rebuilding all items took {ElapsedMs} ms", sw.ElapsedMilliseconds); List <LibraryItem> itemsToDelete = Items.Where(i => i.Tombstoned).ToList(); foreach (LibraryItem item in itemsToDelete) { Items.Remove(item); } LibraryItemsRebuilt?.Invoke(this, EventArgs.Empty); }