diff --git a/NBlog.Domain.Specs/Blog/CreateBlog.cs b/NBlog.Domain.Specs/Blog/CreateBlog.cs index 077a4f7..92cebc9 100644 --- a/NBlog.Domain.Specs/Blog/CreateBlog.cs +++ b/NBlog.Domain.Specs/Blog/CreateBlog.cs @@ -15,7 +15,7 @@ public class When_Creating_The_Blog: BaseCommandTest { protected override CreateBlogCommand When() { - _createBlogCommand = new CreateBlogCommand("Blog title", "Subtitle", Guid.Empty); + _createBlogCommand = new CreateBlogCommand("Blog title", "Subtitle", Guid.Empty, Guid.Empty); return _createBlogCommand; } diff --git a/NBlog.Domain.Specs/NBlog.Domain.Specs.csproj b/NBlog.Domain.Specs/NBlog.Domain.Specs.csproj index 760ac4d..1113d63 100644 --- a/NBlog.Domain.Specs/NBlog.Domain.Specs.csproj +++ b/NBlog.Domain.Specs/NBlog.Domain.Specs.csproj @@ -65,6 +65,7 @@ + diff --git a/NBlog.Domain.Tests/CommandHandlers/BlogCommandHandlerTestHandler.cs b/NBlog.Domain.Tests/CommandHandlers/BlogCommandHandlerTestHandler.cs index 06f371b..655b3c2 100644 --- a/NBlog.Domain.Tests/CommandHandlers/BlogCommandHandlerTestHandler.cs +++ b/NBlog.Domain.Tests/CommandHandlers/BlogCommandHandlerTestHandler.cs @@ -19,7 +19,7 @@ public void When() var repositoryFactory = new DomainRepositoryStubFactory(); _blogRepository = repositoryFactory.GetDomainRepository(); _blogCommandHandlers = new BlogCommandHandlers(repositoryFactory); - _createBlogCommand = new CreateBlogCommand("Title", "SubTitle", Guid.Empty); + _createBlogCommand = new CreateBlogCommand("Title", "SubTitle", Guid.Empty, Guid.Empty); _blogCommandHandlers.Handle(_createBlogCommand); } diff --git a/NBlog.Domain/CommandHandlers/BlogCommandHandlers.cs b/NBlog.Domain/CommandHandlers/BlogCommandHandlers.cs index 7f3e45c..a0b3e96 100644 --- a/NBlog.Domain/CommandHandlers/BlogCommandHandlers.cs +++ b/NBlog.Domain/CommandHandlers/BlogCommandHandlers.cs @@ -5,7 +5,7 @@ namespace NBlog.Domain.CommandHandlers { - public class BlogCommandHandlers : IHandle + public class BlogCommandHandlers : IHandle, IHandle { private readonly IDomainRepository _blogRepository; @@ -19,5 +19,11 @@ public void Handle(CreateBlogCommand createBlogCommand) var blog = Blog.Create(createBlogCommand.BlogTitle, createBlogCommand.Subtitle, createBlogCommand.UserId, createBlogCommand.AggregateId); _blogRepository.Insert(blog); } + + public void Handle(EnableGoogleAnalyticsCommand enableGoogleAnalyticsCommand) + { + var blog = _blogRepository.Get(enableGoogleAnalyticsCommand.AggregateId); + blog.EnableGoogleAnalytics(enableGoogleAnalyticsCommand.UAAccount); + } } } \ No newline at end of file diff --git a/NBlog.Domain/Commands/CreateBlogCommand.cs b/NBlog.Domain/Commands/CreateBlogCommand.cs index 65cc5dd..0fb5c23 100644 --- a/NBlog.Domain/Commands/CreateBlogCommand.cs +++ b/NBlog.Domain/Commands/CreateBlogCommand.cs @@ -19,12 +19,23 @@ public CreateBlogCommand() : base(Guid.NewGuid()) { } - public CreateBlogCommand(string blogTitle, string subtitle, Guid userId) - : base(Guid.NewGuid()) + public CreateBlogCommand(string blogTitle, string subtitle, Guid userId, Guid aggregateId) + : base(aggregateId) { BlogTitle = blogTitle; Subtitle = subtitle; UserId = userId; } } + + public class EnableGoogleAnalyticsCommand : Command + { + public string UAAccount { get; set; } + + public EnableGoogleAnalyticsCommand(string uaAccount, Guid aggregateId) + : base(aggregateId) + { + UAAccount = uaAccount; + } + } } \ No newline at end of file diff --git a/NBlog.Domain/Entities/Blog.cs b/NBlog.Domain/Entities/Blog.cs index fee13c1..daa96a3 100644 --- a/NBlog.Domain/Entities/Blog.cs +++ b/NBlog.Domain/Entities/Blog.cs @@ -8,6 +8,7 @@ namespace NBlog.Domain.Entities public class Blog : AggregateRoot { private List _usersIds; + private string _uaAccount; public Blog() { @@ -18,6 +19,7 @@ private void RegisterHandlers() { RegisterEventHandler(BlogCreated); RegisterEventHandler(UserAddedToBlogEvent); + RegisterEventHandler(EnableGoogleAnalytics); } private void UserAddedToBlogEvent(UserAddedToBlogEvent UserAddedToBlogEvent) @@ -46,5 +48,15 @@ public static Blog Create(string title, string subtitle, Guid userId, Guid aggre { return new Blog(title, subtitle, userId, aggregateId); } + + public void EnableGoogleAnalytics(GoogleAnalyticsEnabledEvent googleAnalyticsEnabledEvent) + { + _uaAccount = googleAnalyticsEnabledEvent.UAAccount; + } + public void EnableGoogleAnalytics(string uaAccount) + { + var googleAnalyticsEnabled = new GoogleAnalyticsEnabledEvent(uaAccount, AggregateId); + Apply(googleAnalyticsEnabled); + } } } \ No newline at end of file diff --git a/NBlog.Domain/Event/BlogCreatedEvent.cs b/NBlog.Domain/Event/BlogCreatedEvent.cs index 00b92bb..f90f9f3 100644 --- a/NBlog.Domain/Event/BlogCreatedEvent.cs +++ b/NBlog.Domain/Event/BlogCreatedEvent.cs @@ -27,4 +27,16 @@ public BlogCreatedEvent(string blogTitle, string subTitle, DateTime creationTime public string SubTitle { get; set; } public DateTime CreationTime { get; set; } } + + public class GoogleAnalyticsEnabledEvent : DomainEventBase + { + public string UAAccount { get; set; } + public Guid AggregateId { get; set; } + + public GoogleAnalyticsEnabledEvent(string uaAccount, Guid aggregateId) + { + UAAccount = uaAccount; + AggregateId = aggregateId; + } + } } \ No newline at end of file diff --git a/NBlog.Infrastructure/INBlogDomainConfiguration.cs b/NBlog.Infrastructure/INBlogDomainConfiguration.cs deleted file mode 100644 index 7396b30..0000000 --- a/NBlog.Infrastructure/INBlogDomainConfiguration.cs +++ /dev/null @@ -1,9 +0,0 @@ -using TJ.CQRS.Messaging; - -namespace NBlog.Infrastructure -{ - public interface INBlogDomainConfiguration - { - void ConfigureMessageRouter(IMessageRouter messageRouter); - } -} \ No newline at end of file diff --git a/NBlog.Infrastructure/MessageRouting/CommandRouter.cs b/NBlog.Infrastructure/MessageRouting/CommandRouter.cs index 7a01644..b025046 100644 --- a/NBlog.Infrastructure/MessageRouting/CommandRouter.cs +++ b/NBlog.Infrastructure/MessageRouting/CommandRouter.cs @@ -22,6 +22,7 @@ public CommandRouter(IDomainRepositoryFactory domainRepositoryFactory) Register(_userCommandHandlers.Handle); Register(_blogCommandHandlers.Handle); + Register(_blogCommandHandlers.Handle); Register(_postCommandHandlers.Handle); Register(_postCommandHandlers.Handle); diff --git a/NBlog.Infrastructure/MessageRouting/EventRouter.cs b/NBlog.Infrastructure/MessageRouting/EventRouter.cs index 317fe6c..b2f4ed7 100644 --- a/NBlog.Infrastructure/MessageRouting/EventRouter.cs +++ b/NBlog.Infrastructure/MessageRouting/EventRouter.cs @@ -27,6 +27,8 @@ public class EventRouter : MessageRouter Register(_userEventHandlers.Handle); Register(_blogEventHandlers.Handle); + Register(_blogEventHandlers.Handle); + Register(_postEventHandlers.Handle); Register(_postEventHandlers.Handle); Register(_postEventHandlers.Handle); diff --git a/NBlog.Infrastructure/NBlog.Infrastructure.csproj b/NBlog.Infrastructure/NBlog.Infrastructure.csproj index 92ebc31..2febbe9 100644 --- a/NBlog.Infrastructure/NBlog.Infrastructure.csproj +++ b/NBlog.Infrastructure/NBlog.Infrastructure.csproj @@ -52,10 +52,8 @@ - - diff --git a/NBlog.Infrastructure/NBlogDomainConfiguration.cs b/NBlog.Infrastructure/NBlogDomainConfiguration.cs deleted file mode 100644 index f62476f..0000000 --- a/NBlog.Infrastructure/NBlogDomainConfiguration.cs +++ /dev/null @@ -1,102 +0,0 @@ -using NBlog.Domain.CommandHandlers; -using NBlog.Domain.Commands; -using NBlog.Domain.Entities; -using NBlog.Domain.Event; -using NBlog.Views; -using TJ.CQRS.Messaging; -using TJ.CQRS.Respositories; - -namespace NBlog.Infrastructure -{ - public class NBlogDomainConfiguration : INBlogDomainConfiguration - { - private readonly IDomainRepositoryFactory _domainRepositoryFactory; - private readonly IViewRepository _postViewRepostiory; - private readonly IViewRepository _blogViewRepository; - private readonly IViewRepository _userViewRepository; - private PostCommandHandlers _postCommandHandlers; - private PostView _postEventHandlers; - private BlogCommandHandlers _blogCommandHandlers; - private BlogView _blogEventHandlers; - private UserView _userEventHandlers; - private UserCommandHandlers _userCommandHandlers; - - public NBlogDomainConfiguration(IDomainRepositoryFactory domainRepositoryFactory, - IViewRepository postViewRepostiory, - IViewRepository blogViewRepository, - IViewRepository userViewRepository) - { - _domainRepositoryFactory = domainRepositoryFactory; - _postViewRepostiory = postViewRepostiory; - _blogViewRepository = blogViewRepository; - _userViewRepository = userViewRepository; - _postCommandHandlers = new PostCommandHandlers(_domainRepositoryFactory); - _blogCommandHandlers = new BlogCommandHandlers(_domainRepositoryFactory); - _userCommandHandlers = new UserCommandHandlers(_domainRepositoryFactory); - _postEventHandlers = new PostView(_postViewRepostiory); - _blogEventHandlers = new BlogView(_blogViewRepository); - _userEventHandlers = new UserView(_userViewRepository); - } - - public void ConfigureMessageRouter(IMessageRouter messageRouter) - { - RegisterCommandHandlers(messageRouter); - RegisterEventHandlers(messageRouter); - } - - private void RegisterEventHandlers(IMessageRouter messageRouter) - { - RegisterPostEventHandlers(messageRouter); - RegisterBlogEventHandlers(messageRouter); - RegisterUserEventHandlers(messageRouter); - } - - private void RegisterUserEventHandlers(IMessageRouter messageRouter) - { - messageRouter.Register(_userEventHandlers.Handle); - } - - private void RegisterBlogEventHandlers(IMessageRouter messageRouter) - { - messageRouter.Register(_blogEventHandlers.Handle); - } - - private void RegisterPostEventHandlers(IMessageRouter messageRouter) - { - messageRouter.Register(_postEventHandlers.Handle); - messageRouter.Register(_postEventHandlers.Handle); - messageRouter.Register(_postEventHandlers.Handle); - messageRouter.Register(_postEventHandlers.Handle); - } - - private void RegisterCommandHandlers(IMessageRouter messageRouter) - { - RegisterPostCommandHandlers(messageRouter); - RegisterBlogCommandHandlers(messageRouter); - RegisterUserCommandHandlers(messageRouter); - } - - private void RegisterUserCommandHandlers(IMessageRouter messageRouter) - { - messageRouter.Register(_userCommandHandlers.Handle); - } - - private void RegisterBlogCommandHandlers(IMessageRouter messageRouter) - { - messageRouter.Register(_blogCommandHandlers.Handle); - } - - private void RegisterPostCommandHandlers(IMessageRouter messageRouter) - { - messageRouter.Register(_postCommandHandlers.Handle); - messageRouter.Register(_postCommandHandlers.Handle); - messageRouter.Register(_postCommandHandlers.Handle); - messageRouter.Register(_postCommandHandlers.Handle); - } - - // only used for debugging purpose. - public PostView PostView { get { return _postEventHandlers; } } - public BlogView BlogView { get { return _blogEventHandlers; } } - public UserView UserView { get { return _userEventHandlers; } } - } -} diff --git a/NBlog.Views/BlogView.cs b/NBlog.Views/BlogView.cs index 6d99d90..254f54a 100644 --- a/NBlog.Views/BlogView.cs +++ b/NBlog.Views/BlogView.cs @@ -30,6 +30,14 @@ public void Handle(BlogCreatedEvent createdEvent) _blogViewRepository.CommitChanges(); } + public void Handle(GoogleAnalyticsEnabledEvent googleAnalyticsEnabledEvent) + { + var blogViewItem = _blogViewRepository.Find(y => y.BlogId == googleAnalyticsEnabledEvent.AggregateId); + blogViewItem.UAAccount = googleAnalyticsEnabledEvent.UAAccount; + blogViewItem.GoogleAnalyticsEnabled = true; + _blogViewRepository.CommitChanges(); + } + public void ResetView() { _blogViewRepository.Clear("BlogViewIndex"); @@ -47,5 +55,9 @@ public class BlogViewItem public Guid BlogId { get; set; } public string BlogTitle { get; set; } public string SubTitle { get; set; } + + public bool GoogleAnalyticsEnabled { get; set; } + + public string UAAccount { get; set; } } } \ No newline at end of file diff --git a/NBlog.Web/Controllers/BlogController.cs b/NBlog.Web/Controllers/BlogController.cs index dd5ec7b..2c64eee 100644 --- a/NBlog.Web/Controllers/BlogController.cs +++ b/NBlog.Web/Controllers/BlogController.cs @@ -36,7 +36,8 @@ public virtual ActionResult Create() { ActionResult actionResult; if (RedirectIfBlogExists(out actionResult)) return actionResult; - return View("Create"); + var createBlogCommand = new CreateBlogCommand(); + return View("Create", createBlogCommand); } [Authorize] @@ -46,6 +47,17 @@ public ActionResult Edit() return View(editBlogViewModel); } + [HttpPost] + [Authorize] + public ActionResult EnableGoogleAnalytics(string uaAccount) + { + var blog = _blogView.GetBlogs().First(); + var blogId = blog.BlogId; + var enableGoogleAnalyticsCommand = new EnableGoogleAnalyticsCommand(uaAccount, blogId); + return ValidateAndSendCommand(enableGoogleAnalyticsCommand, () => RedirectToAction("Edit"), + () => RedirectToAction("Edit")); + } + private bool RedirectIfBlogExists(out ActionResult actionResult) { actionResult = null; @@ -100,5 +112,25 @@ public ActionResult ResetViews() _eventBus.PublishEvents(allEvents); return RedirectToAction("Edit"); } + + [ChildActionOnly] + public ActionResult GoogleAnalytics() + { + var googleAnalyticsViewModel = new GoogleAnalyticsViewModel(); + var blog = _blogView.GetBlogs().FirstOrDefault(); + if(blog != null && blog.GoogleAnalyticsEnabled) + { + googleAnalyticsViewModel.GoogleAnalyticsEnabled = true; + googleAnalyticsViewModel.UAAccount = blog.UAAccount; + } + return View("_GoogleAnalytics", googleAnalyticsViewModel); + } + } + + public class GoogleAnalyticsViewModel + { + public bool GoogleAnalyticsEnabled { get; set; } + + public string UAAccount { get; set; } } } diff --git a/NBlog.Web/NBlog.Web.csproj b/NBlog.Web/NBlog.Web.csproj index beee919..e3f5e66 100644 --- a/NBlog.Web/NBlog.Web.csproj +++ b/NBlog.Web/NBlog.Web.csproj @@ -354,6 +354,7 @@ + diff --git a/NBlog.Web/Views/Blog/Create.cshtml b/NBlog.Web/Views/Blog/Create.cshtml index a0e625a..e1ae9d3 100644 --- a/NBlog.Web/Views/Blog/Create.cshtml +++ b/NBlog.Web/Views/Blog/Create.cshtml @@ -28,6 +28,7 @@ @Html.ValidationMessageFor(model => model.Subtitle) + @Html.HiddenFor(model => model.AggregateId) } diff --git a/NBlog.Web/Views/Blog/Edit.cshtml b/NBlog.Web/Views/Blog/Edit.cshtml index 2ac1bd0..8b70c87 100644 --- a/NBlog.Web/Views/Blog/Edit.cshtml +++ b/NBlog.Web/Views/Blog/Edit.cshtml @@ -2,9 +2,23 @@ @{ ViewBag.Title = "Edit blog data"; + ViewBag.DisableGoogleAnalytics = true; }

@ViewBag.Title

-
- @Html.ActionLink("Reset data views", "ResetViews") +
+
+ @using(Html.BeginForm("EnableGoogleAnalytics", "Blog", FormMethod.Post)) + { +
+
+ @Html.TextBox("uaAccount") +
+
+ + } +
+
+ @Html.ActionLink("Reset data views", "ResetViews") +
diff --git a/NBlog.Web/Views/Shared/_Layout.cshtml b/NBlog.Web/Views/Shared/_Layout.cshtml index 7131b61..d6251dd 100644 --- a/NBlog.Web/Views/Shared/_Layout.cshtml +++ b/NBlog.Web/Views/Shared/_Layout.cshtml @@ -14,13 +14,16 @@ -@* + @* -*@ +*@ + + @{ Html.RenderAction("GoogleAnalytics", "Blog"); } +