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

Commit a953422

Browse files
committed
Increase or decrease user answers count
1 parent a214f4a commit a953422

2 files changed

Lines changed: 79 additions & 4 deletions

File tree

src/Atles.Domain.Commands.Handlers/Posts/SetReplyAsAnswerHandler.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public async Task<IEnumerable<IEvent>> Handle(SetReplyAsAnswer command)
2525
{
2626
var reply = await _dbContext.Posts
2727
.Include(x => x.Topic)
28+
.Include(x => x.CreatedByUser)
2829
.FirstOrDefaultAsync(x =>
2930
x.Id == command.ReplyId &&
3031
x.TopicId == command.TopicId &&
@@ -38,7 +39,17 @@ public async Task<IEnumerable<IEvent>> Handle(SetReplyAsAnswer command)
3839
}
3940

4041
reply.SetAsAnswer(command.IsAnswer);
42+
reply.Topic.SetAsAnswered(command.IsAnswer);
4143

44+
if (command.IsAnswer)
45+
{
46+
reply.CreatedByUser.IncreaseAnswersCount();
47+
}
48+
else
49+
{
50+
reply.CreatedByUser.DecreaseAnswersCount();
51+
}
52+
4253
var @event = new ReplySetAsAnswer
4354
{
4455
IsAnswer = reply.IsAnswer,
@@ -50,8 +61,6 @@ public async Task<IEnumerable<IEvent>> Handle(SetReplyAsAnswer command)
5061

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

53-
reply.Topic.SetAsAnswered(command.IsAnswer);
54-
5564
await _dbContext.SaveChangesAsync();
5665

5766
_cacheManager.Remove(CacheKeys.Forum(reply.Topic.ForumId));

test/Atles.Domain.Commands.Handlers.Tests/Posts/SetReplyAsAnswerHandlerTests.cs

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,76 @@ public async Task Should_set_reply_as_answer_and_add_event()
6363

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

67-
Assert.AreEqual(true, replyUpdated.IsAnswer);
68-
Assert.AreEqual(true, replyUpdated.Topic.HasAnswer);
68+
Assert.True(replyUpdated.IsAnswer);
69+
Assert.True(replyUpdated.Topic.HasAnswer);
6970
Assert.NotNull(replyEvent);
71+
Assert.AreEqual(1, userUpdated.AnswersCount);
72+
}
73+
}
74+
75+
[Test]
76+
public async Task Should_set_topic_as_not_answered_and_add_event()
77+
{
78+
var options = Shared.CreateContextOptions();
79+
80+
var siteId = Guid.NewGuid();
81+
var categoryId = Guid.NewGuid();
82+
var forumId = Guid.NewGuid();
83+
var topicId = Guid.NewGuid();
84+
var userId = Guid.NewGuid();
85+
86+
var category = new Category(categoryId, siteId, "Category", 1, Guid.NewGuid());
87+
var forum = new Forum(forumId, categoryId, "Forum", "my-forum", "My Forum", 1);
88+
var topic = Post.CreateTopic(topicId, forumId, Guid.NewGuid(), "Title", "slug", "Content", PostStatusType.Published);
89+
var reply = Post.CreateReply(Guid.NewGuid(), topicId, forumId, userId, "Content", PostStatusType.Published);
90+
var user = new User(userId, Guid.NewGuid().ToString(), "Email", "Display Name");
91+
92+
category.IncreaseRepliesCount();
93+
forum.IncreaseRepliesCount();
94+
topic.IncreaseRepliesCount();
95+
user.IncreaseRepliesCount();
96+
97+
reply.SetAsAnswer(true);
98+
topic.SetAsAnswered(true);
99+
user.IncreaseAnswersCount();
100+
101+
using (var dbContext = new AtlesDbContext(options))
102+
{
103+
dbContext.Categories.Add(category);
104+
dbContext.Forums.Add(forum);
105+
dbContext.Posts.Add(topic);
106+
dbContext.Posts.Add(reply);
107+
dbContext.Users.Add(user);
108+
await dbContext.SaveChangesAsync();
109+
}
110+
111+
using (var dbContext = new AtlesDbContext(options))
112+
{
113+
var command = new SetReplyAsAnswer
114+
{
115+
ReplyId = reply.Id,
116+
SiteId = siteId,
117+
ForumId = forumId,
118+
TopicId = topicId,
119+
IsAnswer = false
120+
};
121+
122+
var cacheManager = new Mock<ICacheManager>();
123+
124+
var sut = new SetReplyAsAnswerHandler(dbContext, cacheManager.Object);
125+
126+
await sut.Handle(command);
127+
128+
var replyUpdated = await dbContext.Posts.Include(x => x.Topic).FirstOrDefaultAsync(x => x.Id == reply.Id);
129+
var replyEvent = await dbContext.Events.FirstOrDefaultAsync(x => x.TargetId == reply.Id);
130+
var userUpdated = await dbContext.Users.FirstOrDefaultAsync(x => x.Id == user.Id);
131+
132+
Assert.False(replyUpdated.IsAnswer);
133+
Assert.False(replyUpdated.Topic.HasAnswer);
134+
Assert.NotNull(replyEvent);
135+
Assert.AreEqual(0, userUpdated.AnswersCount);
70136
}
71137
}
72138
}

0 commit comments

Comments
 (0)