Skip to content

Commit

Permalink
partly done with google analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
mastoj committed Oct 30, 2012
1 parent 0924e06 commit 901f619
Show file tree
Hide file tree
Showing 18 changed files with 118 additions and 123 deletions.
2 changes: 1 addition & 1 deletion NBlog.Domain.Specs/Blog/CreateBlog.cs
Expand Up @@ -15,7 +15,7 @@ public class When_Creating_The_Blog: BaseCommandTest<CreateBlogCommand>
{
protected override CreateBlogCommand When()
{
_createBlogCommand = new CreateBlogCommand("Blog title", "Subtitle", Guid.Empty);
_createBlogCommand = new CreateBlogCommand("Blog title", "Subtitle", Guid.Empty, Guid.Empty);
return _createBlogCommand;
}

Expand Down
1 change: 1 addition & 0 deletions NBlog.Domain.Specs/NBlog.Domain.Specs.csproj
Expand Up @@ -65,6 +65,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="BaseCommandTest.cs" />
<Compile Include="Blog\GoogleAnalyticsTests.cs" />
<Compile Include="Blog\CreateBlog.cs" />
<Compile Include="Post\SetPublishDateOnPost.cs" />
<Compile Include="Post\CreatePost.cs" />
Expand Down
Expand Up @@ -19,7 +19,7 @@ public void When()
var repositoryFactory = new DomainRepositoryStubFactory();
_blogRepository = repositoryFactory.GetDomainRepository<Blog>();
_blogCommandHandlers = new BlogCommandHandlers(repositoryFactory);
_createBlogCommand = new CreateBlogCommand("Title", "SubTitle", Guid.Empty);
_createBlogCommand = new CreateBlogCommand("Title", "SubTitle", Guid.Empty, Guid.Empty);
_blogCommandHandlers.Handle(_createBlogCommand);
}

Expand Down
8 changes: 7 additions & 1 deletion NBlog.Domain/CommandHandlers/BlogCommandHandlers.cs
Expand Up @@ -5,7 +5,7 @@

namespace NBlog.Domain.CommandHandlers
{
public class BlogCommandHandlers : IHandle<CreateBlogCommand>
public class BlogCommandHandlers : IHandle<CreateBlogCommand>, IHandle<EnableGoogleAnalyticsCommand>
{
private readonly IDomainRepository<Blog> _blogRepository;

Expand All @@ -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);
}
}
}
15 changes: 13 additions & 2 deletions NBlog.Domain/Commands/CreateBlogCommand.cs
Expand Up @@ -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;
}
}
}
12 changes: 12 additions & 0 deletions NBlog.Domain/Entities/Blog.cs
Expand Up @@ -8,6 +8,7 @@ namespace NBlog.Domain.Entities
public class Blog : AggregateRoot
{
private List<Guid> _usersIds;
private string _uaAccount;

public Blog()
{
Expand All @@ -18,6 +19,7 @@ private void RegisterHandlers()
{
RegisterEventHandler<BlogCreatedEvent>(BlogCreated);
RegisterEventHandler<UserAddedToBlogEvent>(UserAddedToBlogEvent);
RegisterEventHandler<GoogleAnalyticsEnabledEvent>(EnableGoogleAnalytics);
}

private void UserAddedToBlogEvent(UserAddedToBlogEvent UserAddedToBlogEvent)
Expand Down Expand Up @@ -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);
}
}
}
12 changes: 12 additions & 0 deletions NBlog.Domain/Event/BlogCreatedEvent.cs
Expand Up @@ -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;
}
}
}
9 changes: 0 additions & 9 deletions NBlog.Infrastructure/INBlogDomainConfiguration.cs

This file was deleted.

1 change: 1 addition & 0 deletions NBlog.Infrastructure/MessageRouting/CommandRouter.cs
Expand Up @@ -22,6 +22,7 @@ public CommandRouter(IDomainRepositoryFactory domainRepositoryFactory)
Register<CreateUserCommand>(_userCommandHandlers.Handle);

Register<CreateBlogCommand>(_blogCommandHandlers.Handle);
Register<EnableGoogleAnalyticsCommand>(_blogCommandHandlers.Handle);

Register<CreatePostCommand>(_postCommandHandlers.Handle);
Register<PublishPostCommand>(_postCommandHandlers.Handle);
Expand Down
2 changes: 2 additions & 0 deletions NBlog.Infrastructure/MessageRouting/EventRouter.cs
Expand Up @@ -27,6 +27,8 @@ public class EventRouter : MessageRouter

Register<UserCreatedEvent>(_userEventHandlers.Handle);
Register<BlogCreatedEvent>(_blogEventHandlers.Handle);
Register<GoogleAnalyticsEnabledEvent>(_blogEventHandlers.Handle);

Register<PostCreatedEvent>(_postEventHandlers.Handle);
Register<PostDeletedEvent>(_postEventHandlers.Handle);
Register<PostPublishedEvent>(_postEventHandlers.Handle);
Expand Down
2 changes: 0 additions & 2 deletions NBlog.Infrastructure/NBlog.Infrastructure.csproj
Expand Up @@ -52,10 +52,8 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="INBlogDomainConfiguration.cs" />
<Compile Include="MessageRouting\EventRouter.cs" />
<Compile Include="MessageRouting\CommandRouter.cs" />
<Compile Include="NBlogDomainConfiguration.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
102 changes: 0 additions & 102 deletions NBlog.Infrastructure/NBlogDomainConfiguration.cs

This file was deleted.

12 changes: 12 additions & 0 deletions NBlog.Views/BlogView.cs
Expand Up @@ -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");
Expand All @@ -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; }
}
}
34 changes: 33 additions & 1 deletion NBlog.Web/Controllers/BlogController.cs
Expand Up @@ -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]
Expand All @@ -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;
Expand Down Expand Up @@ -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; }
}
}
1 change: 1 addition & 0 deletions NBlog.Web/NBlog.Web.csproj
Expand Up @@ -354,6 +354,7 @@
<Content Include="Views\Blog\Create.cshtml" />
<Content Include="Views\Post\_EditPost.cshtml" />
<Content Include="Views\Blog\Edit.cshtml" />
<Content Include="Views\Blog\_GoogleAnalytics.cshtml" />
</ItemGroup>
<ItemGroup>
<Folder Include="App_Data\" />
Expand Down
1 change: 1 addition & 0 deletions NBlog.Web/Views/Blog/Create.cshtml
Expand Up @@ -28,6 +28,7 @@
@Html.ValidationMessageFor(model => model.Subtitle)
</div>
</div>
@Html.HiddenFor(model => model.AggregateId)
<input type="submit" value="Create blog"/>
</div>
}
Expand Down
18 changes: 16 additions & 2 deletions NBlog.Web/Views/Blog/Edit.cshtml
Expand Up @@ -2,9 +2,23 @@

@{
ViewBag.Title = "Edit blog data";
ViewBag.DisableGoogleAnalytics = true;
}

<h2>@ViewBag.Title</h2>
<div class="editor">
@Html.ActionLink("Reset data views", "ResetViews")
<div class="editor">
<fieldset>
@using(Html.BeginForm("EnableGoogleAnalytics", "Blog", FormMethod.Post))
{
<div class="editor-entry">
<div class="editor-field">
@Html.TextBox("uaAccount")
</div>
</div>
<input type="submit" value="Enable google analytics" class="submit-buttom" />
}
</fieldset>
<fieldset>
@Html.ActionLink("Reset data views", "ResetViews")
</fieldset>
</div>

0 comments on commit 901f619

Please sign in to comment.