Example #1
0
        /// <inheritdoc />
        public async Task AddCommentAsync(long campaignId, PromotionSentiment sentiment, string content)
        {
            AuthorizationService.RequireAuthenticatedUser();
            AuthorizationService.RequireClaims(AuthorizationClaim.PromotionsComment);

            ValidateComment(content);

            if (await PromotionCommentRepository.AnyAsync(new PromotionCommentSearchCriteria()
            {
                CampaignId = campaignId,
                CreatedById = AuthorizationService.CurrentUserId.Value,
                IsModified = false
            }))
            {
                throw new InvalidOperationException("Only one comment can be made per user, per campaign");
            }

            var campaign = await PromotionCampaignRepository.ReadDetailsAsync(campaignId);

            if (campaign.Subject.Id == AuthorizationService.CurrentUserId)
            {
                throw new InvalidOperationException("You aren't allowed to comment on your own campaign");
            }

            if (!(campaign.CloseAction is null))
            {
                throw new InvalidOperationException($"Campaign {campaignId} has already been closed");
            }

            var rankRoles = await GetRankRolesAsync(AuthorizationService.CurrentGuildId.Value);

            if (!await CheckIfUserIsRankOrHigherAsync(rankRoles, AuthorizationService.CurrentUserId.Value, campaign.TargetRole.Id))
            {
                throw new InvalidOperationException($"Commenting on a promotion campaign requires a rank at least as high as the proposed target rank");
            }

            PromotionActionSummary resultAction;

            using (var transaction = await PromotionCommentRepository.BeginCreateTransactionAsync())
            {
                resultAction = await PromotionCommentRepository.CreateAsync(new PromotionCommentCreationData()
                {
                    GuildId     = campaign.GuildId,
                    CampaignId  = campaignId,
                    Sentiment   = sentiment,
                    Content     = content,
                    CreatedById = AuthorizationService.CurrentUserId.Value
                });

                transaction.Commit();
            }

            PublishActionNotificationAsync(resultAction);
        }
Example #2
0
        /// <inheritdoc />
        public async Task AddCommentAsync(long campaignId, PromotionSentiment sentiment, string content)
        {
            AuthorizationService.RequireClaims(AuthorizationClaim.PromotionsComment);

            if (content == null || content.Length <= 3)
            {
                throw new InvalidOperationException("Comment content must be longer than 3 characters.");
            }

            using (var transaction = await PromotionCommentRepository.BeginCreateTransactionAsync())
            {
                if (await PromotionCommentRepository.AnyAsync(new PromotionCommentSearchCriteria()
                {
                    CampaignId = campaignId,
                    CreatedById = AuthorizationService.CurrentUserId.Value
                }))
                {
                    throw new InvalidOperationException("Only one comment can be made per user, per campaign");
                }

                var campaign = await PromotionCampaignRepository.ReadDetailsAsync(campaignId);

                if (!(campaign.CloseAction is null))
                {
                    throw new InvalidOperationException($"Campaign {campaignId} has already been closed");
                }

                var rankRoles = await GetRankRolesAsync(AuthorizationService.CurrentGuildId.Value);

                if (!await CheckIfUserIsRankOrHigher(rankRoles, AuthorizationService.CurrentUserId.Value, campaign.TargetRole.Id))
                {
                    throw new InvalidOperationException($"Commenting on a promotion campaign requires a rank at least as high as the proposed target rank");
                }

                await PromotionCommentRepository.CreateAsync(new PromotionCommentCreationData()
                {
                    GuildId     = campaign.GuildId,
                    CampaignId  = campaignId,
                    Sentiment   = sentiment,
                    Content     = content,
                    CreatedById = AuthorizationService.CurrentUserId.Value
                });

                transaction.Commit();
            }
        }