protected override async Task Handle(CreateSubTopicCommand request, CancellationToken cancellationToken) { if (!await _mediator.Send(new CheckIfTopicCanOwnSubTopicsQuery(request.ParentTopicId), cancellationToken)) { throw new ApplicationLogicalException("Specified topic can't own subtopics"); } var rules = new TopicRules( request.RolesAllowedToRead.Select(Role.FromName), request.RolesAllowedToWrite.Select(Role.FromName), request.CanOwnSubTopics); var subTopic = Topic.NewSubTopic( request.CreatorId, request.ParentTopicId, rules, request.Name, request.Description, _dateTimeProvider); await _unitOfWork .GetRepository <ITopicRepository>() .Save(subTopic, cancellationToken); var accountRepository = _unitOfWork.GetRepository <IAccountRepository>(); var account = await accountRepository.FindById(request.CreatorId); account.TopicCount += 1; await accountRepository.Update(account, cancellationToken); }
public async Task Should_return_false_when_topic_is_not_valid() { using (var dbContext = new AtlasDbContext(Shared.CreateContextOptions())) { var sut = new TopicRules(dbContext); var actual = await sut.IsValidAsync(Guid.NewGuid(), Guid.NewGuid(), Guid.NewGuid()); Assert.IsFalse(actual); } }
protected override async Task Handle(CreateRootTopicCommand request, CancellationToken cancellationToken) { var rules = new TopicRules( request.RolesAllowedToRead.Select(Role.FromName), request.RolesAllowedToWrite.Select(Role.FromName), request.CanOwnSubTopics); var topic = Topic.NewRootTopic(request.CreatorId, rules, request.Name, request.Description, _dateTimeProvider); await _unitOfWork .GetRepository <ITopicRepository>() .Save(topic, cancellationToken); }
public Topic Convert(TopicEntity source, Topic destination, ResolutionContext context) { var readRestrictions = source.Restrictions .Where(r => r.Type == TopicRestrictionType.Read) .Select(r => Role.FromKey(r.RoleId)) .ToList(); var writeRestrictions = source.Restrictions .Where(r => r.Type == TopicRestrictionType.Write) .Select(r => Role.FromKey(r.RoleId)) .ToList(); TopicRules rules = null; if (readRestrictions.Any() || writeRestrictions.Any() || !source.CanOwnPosts) { rules = new TopicRules(readRestrictions, writeRestrictions, !source.CanOwnPosts); } return(new Topic(source.Id, source.CreatorId, source.ParentId, rules, source.Name, source.Description, source.CreatedDate, source.UpdatedDate, source.ViewCount, source.LastReplyUserId)); }
public async Task Should_return_true_when_topic_is_valid() { var options = Shared.CreateContextOptions(); var category = new Category(Guid.NewGuid(), Guid.NewGuid(), "Category", 1, Guid.NewGuid()); var forum = new Forum(Guid.NewGuid(), category.Id, "Forum", "my-forum", "My Forum", 1, Guid.NewGuid()); var topic = Post.CreateTopic(Guid.NewGuid(), forum.Id, Guid.NewGuid(), "Title", "slug", "Content", StatusType.Published); using (var dbContext = new AtlasDbContext(options)) { dbContext.Categories.Add(category); dbContext.Forums.Add(forum); dbContext.Posts.Add(topic); await dbContext.SaveChangesAsync(); } using (var dbContext = new AtlasDbContext(options)) { var sut = new TopicRules(dbContext); var actual = await sut.IsValidAsync(category.SiteId, forum.Id, topic.Id); Assert.IsTrue(actual); } }