Esempio n. 1
0
        public async Task <AddCommentValidationResult> Validate(AddShopCommentCommand comment)
        {
            if (comment == null)
            {
                throw new ArgumentNullException(nameof(comment));
            }

            var validationResult = Validate(comment.ShopId, comment.Content, comment.Score, Constants.DtoNames.Comment.ShopId);

            if (!validationResult.IsValid)
            {
                return(validationResult);
            }

            ShopAggregate shop = null;

            // Check shop exists && 1 comment.
            if (!string.IsNullOrWhiteSpace(comment.ShopId))
            {
                shop = await _shopRepository.Get(comment.ShopId);

                if (shop == null)
                {
                    return(new AddCommentValidationResult(ErrorDescriptions.TheShopDoesntExist));
                }

                var comments = await _shopRepository.SearchComments(new SearchShopCommentsParameter
                {
                    ShopId  = comment.ShopId,
                    Subject = comment.Subject
                });

                if (comments.TotalResults > 0)
                {
                    return(new AddCommentValidationResult(ErrorDescriptions.TheCommentAlreadyExists));
                }
            }

            // Check content size.
            if (!string.IsNullOrWhiteSpace(comment.Content) && comment.Content.Length > 255)
            {
                return(new AddCommentValidationResult(string.Format(ErrorDescriptions.TheParameterLengthCannotExceedNbCharacters, Constants.DtoNames.Comment.Content, "255")));
            }

            // Check score.
            if (comment.Score < 1 || comment.Score > 5)
            {
                return(new AddCommentValidationResult(ErrorDescriptions.TheScoreMustBeBetween));
            }

            return(new AddCommentValidationResult());
        }
Esempio n. 2
0
        public async Task AddShopCommentCommandHandle_AddsShopComment()
        {
            //Arrange
            var user = new AllMarkt.Entities.User {
                Email       = "*****@*****.**",
                Password    = "******",
                DisplayName = "UserTest"
            };

            var shop = new AllMarkt.Entities.Shop
            {
                Address           = "aaaaa",
                Comments          = null,
                CUI               = "aaaaddd",
                IBAN              = "aaaaa",
                Orders            = null,
                PhoneNumber       = "0123654789",
                ProductCategories = null,
                ShopCategoryLink  = null,
                SocialCapital     = 4
            };

            AllMarktContextIM.Users.Add(user);
            AllMarktContextIM.Shops.Add(shop);
            await AllMarktContextIM.SaveChangesAsync();

            var addShopCommentCommand = new AddShopCommentCommand
            {
                Rating        = 5,
                Text          = "cel mai bun",
                ShopId        = shop.Id,
                AddedByUserId = user.Id
            };

            //Act
            await _addShopCommentCommandHandler.Handle(addShopCommentCommand, CancellationToken.None);

            //Assert
            AllMarktContextIM.ShopComments
            .Should()
            .Contain(shopComment =>
                     shopComment.Rating == addShopCommentCommand.Rating &&
                     shopComment.Text == addShopCommentCommand.Text &&
                     shopComment.AddedBy.Id == addShopCommentCommand.AddedByUserId &&
                     shopComment.Shop.Id == addShopCommentCommand.ShopId);
        }
Esempio n. 3
0
        public async Task Handle(AddShopCommentCommand message)
        {
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            var record = await _shopRepository.Get(message.ShopId);

            if (record == null)
            {
                return;
            }

            var shopComment = new ShopComment
            {
                Id             = message.Id,
                Content        = message.Content,
                Score          = message.Score,
                Subject        = message.Subject,
                CreateDateTime = message.CreateDateTime,
                UpdateDateTime = message.UpdateDateTime
            };

            record.AddComment(shopComment);
            await _shopRepository.Update(record);

            _eventPublisher.Publish(new ShopCommentAddedEvent
            {
                Id             = message.Id,
                ShopId         = message.ShopId,
                Content        = message.Content,
                Score          = message.Score,
                Subject        = message.Subject,
                CreateDateTime = message.CreateDateTime,
                UpdateDateTime = message.UpdateDateTime,
                AverageScore   = record.AverageScore,
                NbComments     = record.Comments == null ? 0 : record.Comments.Count(),
                CommonId       = message.CommonId
            });
        }