Skip to content
This repository has been archived by the owner on Apr 14, 2023. It is now read-only.

Commit

Permalink
Increase or decrease user answers count
Browse files Browse the repository at this point in the history
  • Loading branch information
lucabriguglia committed Feb 11, 2022
1 parent a214f4a commit a953422
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public async Task<IEnumerable<IEvent>> Handle(SetReplyAsAnswer command)
{
var reply = await _dbContext.Posts
.Include(x => x.Topic)
.Include(x => x.CreatedByUser)
.FirstOrDefaultAsync(x =>
x.Id == command.ReplyId &&
x.TopicId == command.TopicId &&
Expand All @@ -38,7 +39,17 @@ public async Task<IEnumerable<IEvent>> Handle(SetReplyAsAnswer command)
}

reply.SetAsAnswer(command.IsAnswer);
reply.Topic.SetAsAnswered(command.IsAnswer);

if (command.IsAnswer)
{
reply.CreatedByUser.IncreaseAnswersCount();
}
else
{
reply.CreatedByUser.DecreaseAnswersCount();
}

var @event = new ReplySetAsAnswer
{
IsAnswer = reply.IsAnswer,
Expand All @@ -50,8 +61,6 @@ public async Task<IEnumerable<IEvent>> Handle(SetReplyAsAnswer command)

_dbContext.Events.Add(@event.ToDbEntity());

reply.Topic.SetAsAnswered(command.IsAnswer);

await _dbContext.SaveChangesAsync();

_cacheManager.Remove(CacheKeys.Forum(reply.Topic.ForumId));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,76 @@ public async Task Should_set_reply_as_answer_and_add_event()

var replyUpdated = await dbContext.Posts.Include(x => x.Topic).FirstOrDefaultAsync(x => x.Id == reply.Id);
var replyEvent = await dbContext.Events.FirstOrDefaultAsync(x => x.TargetId == reply.Id);
var userUpdated = await dbContext.Users.FirstOrDefaultAsync(x => x.Id == user.Id);

Assert.AreEqual(true, replyUpdated.IsAnswer);
Assert.AreEqual(true, replyUpdated.Topic.HasAnswer);
Assert.True(replyUpdated.IsAnswer);
Assert.True(replyUpdated.Topic.HasAnswer);
Assert.NotNull(replyEvent);
Assert.AreEqual(1, userUpdated.AnswersCount);
}
}

[Test]
public async Task Should_set_topic_as_not_answered_and_add_event()
{
var options = Shared.CreateContextOptions();

var siteId = Guid.NewGuid();
var categoryId = Guid.NewGuid();
var forumId = Guid.NewGuid();
var topicId = Guid.NewGuid();
var userId = Guid.NewGuid();

var category = new Category(categoryId, siteId, "Category", 1, Guid.NewGuid());
var forum = new Forum(forumId, categoryId, "Forum", "my-forum", "My Forum", 1);
var topic = Post.CreateTopic(topicId, forumId, Guid.NewGuid(), "Title", "slug", "Content", PostStatusType.Published);
var reply = Post.CreateReply(Guid.NewGuid(), topicId, forumId, userId, "Content", PostStatusType.Published);
var user = new User(userId, Guid.NewGuid().ToString(), "Email", "Display Name");

category.IncreaseRepliesCount();
forum.IncreaseRepliesCount();
topic.IncreaseRepliesCount();
user.IncreaseRepliesCount();

reply.SetAsAnswer(true);
topic.SetAsAnswered(true);
user.IncreaseAnswersCount();

using (var dbContext = new AtlesDbContext(options))
{
dbContext.Categories.Add(category);
dbContext.Forums.Add(forum);
dbContext.Posts.Add(topic);
dbContext.Posts.Add(reply);
dbContext.Users.Add(user);
await dbContext.SaveChangesAsync();
}

using (var dbContext = new AtlesDbContext(options))
{
var command = new SetReplyAsAnswer
{
ReplyId = reply.Id,
SiteId = siteId,
ForumId = forumId,
TopicId = topicId,
IsAnswer = false
};

var cacheManager = new Mock<ICacheManager>();

var sut = new SetReplyAsAnswerHandler(dbContext, cacheManager.Object);

await sut.Handle(command);

var replyUpdated = await dbContext.Posts.Include(x => x.Topic).FirstOrDefaultAsync(x => x.Id == reply.Id);
var replyEvent = await dbContext.Events.FirstOrDefaultAsync(x => x.TargetId == reply.Id);
var userUpdated = await dbContext.Users.FirstOrDefaultAsync(x => x.Id == user.Id);

Assert.False(replyUpdated.IsAnswer);
Assert.False(replyUpdated.Topic.HasAnswer);
Assert.NotNull(replyEvent);
Assert.AreEqual(0, userUpdated.AnswersCount);
}
}
}
Expand Down

0 comments on commit a953422

Please sign in to comment.