public void CategoryInfoUpdatedEvent_Ctor_Should_Set_Arguments_Correctly() { Guid categoryId = Guid.NewGuid(); string code = "code"; string name = "category"; string url = "category"; string description = "description"; bool isVisible = true; DateTime?visibleFrom = DateTime.Today; DateTime?visibleTo = DateTime.Today.AddDays(2); var @event = new CategoryInfoUpdatedEvent(categoryId, code, name, url, description, isVisible, visibleFrom, visibleTo); Assert.Equal(categoryId, @event.CategoryId); Assert.Equal(code, @event.Code); Assert.Equal(name, @event.Name); Assert.Equal(url, @event.Url); Assert.Equal(description, @event.Description); Assert.Equal(isVisible, @event.IsVisible); Assert.Equal(visibleFrom, @event.VisibleFrom); Assert.Equal(visibleTo, @event.VisibleTo); Assert.Equal(categoryId, @event.AggregateId); Assert.Equal(typeof(Catalog.Models.Category), @event.AggregateType); }
/// <summary> /// Implementation of <see cref="ICategoryCommands.UpdateCategoryInfo(Guid, string, string, string, string, bool, DateTime?, DateTime?)"/> /// </summary> /// <param name="categoryId">The category id</param> /// <param name="code">The category code</param> /// <param name="name">The category name</param> /// <param name="url">The category url</param> /// <param name="description">The category description</param> /// <param name="isVisible">Whether the category is visible</param> /// <param name="visibleFrom">The date and time from when the category is visible</param> /// <param name="visibleTo">The date and time till when the category is visible</param> /// <returns></returns> public virtual async Task UpdateCategoryInfo(Guid categoryId, string code, string name, string url, string description, bool isVisible, DateTime?visibleFrom, DateTime?visibleTo) { try { if (categoryId == Guid.Empty) { throw new ArgumentException("value cannot be empty", nameof(categoryId)); } var category = await Repository.GetByKeyAsync <Category>(categoryId); if (category.Code != code) { category.ChangeCode(code); } if (category.Name != name) { category.ChangeName(name); } if (category.Url != url) { category.ChangeUrl(url); } if (category.Description != description) { category.ChangeDescription(description); } if (category.IsVisible != isVisible) { if (isVisible) { if (!visibleFrom.HasValue && !visibleTo.HasValue) { category.SetAsVisible(); } else { category.SetAsVisible(visibleFrom, visibleTo); } } else { category.Hide(); } } await Repository.SaveChangesAsync(); var @event = new CategoryInfoUpdatedEvent(categoryId, code, name, url, description, isVisible, visibleFrom, visibleTo); EventBus.RaiseEvent(@event); } catch { throw; } }
/// <summary> /// <see cref="IHandleEvent{TEvent}.Handle(TEvent)"/> /// </summary> /// <param name="event"></param> public void Handle(CategoryInfoUpdatedEvent @event) { try { EventStore.Save(@event); } catch { throw; } }