Esempio n. 1
0
        public async Task UpdateShop_ShouldSucceed_WithOldNameEqualNewName_Test()
        {
            var shop = new CreateShopCommand
            {
                Name     = Guid.NewGuid().ToString(),
                Location = new LocationDto
                {
                    Street   = "Place Doyen Gosse",
                    PostCode = "38000",
                    Country  = "France",
                    City     = "Grenoble",
                    Address  = "2",
                    Region   = "Rhone Alpes"
                },
                Contact = new ContactDto
                {
                    Phone = "test",
                    Email = "*****@*****.**"
                }
            };
            var createShopResult = await Mediator.Send(shop);

            createShopResult.Status.Should().Be(ResultStatus.Ok);

            var command = new UpdateShopCommand
            {
                NewName = shop.Name,
                OldName = shop.Name,
                ShopId  = createShopResult.TypedEntity,
            };

            var result = await Mediator.Send(command);

            result.Status.Should().Be(ResultStatus.Ok);
        }
Esempio n. 2
0
 public async Task <IActionResult> Update(int id, [FromBody] UpdateShopCommand command)
 {
     if (id != command.Id)
     {
         return(BadRequest());
     }
     return(Ok(await mediator.Send(command)));
 }
Esempio n. 3
0
        public async Task <CoreResult> Update([FromBody] UpdateShopRequestDto dto)
        {
            CoreResult        result  = new CoreResult();
            UpdateShopCommand command = new UpdateShopCommand(dto);
            var res = await _bus.SendCommandAsync(command);

            if (res)
            {
                result.Success("修改成功");
            }
            else
            {
                result.Failed("修改失败");
            }
            return(result);
        }
Esempio n. 4
0
        public async Task <UpdateShopCommandResponse> Handle(UpdateShopCommand command)
        {
            var shop = await _repository.FindAsync(command.ShopId);

            if (shop == null)
            {
                throw new DomainException("فروشگاه یافت نشد");
            }
            shop.AreaRadius   = command.AreaRadius;
            shop.Description  = command.Description;
            shop.Metrage      = command.Metrage;
            shop.Name         = command.Name;
            shop.NationalCode = command.NationalCode;
            shop.FirstName    = command.FirstName;
            shop.LastName     = command.LastName;
            shop.EmailAddress = command.EmailAddress;
            shop.BankAccount  = new BankAccount(command.BankAccount.Iban, command.BankAccount.AccountOwnerName,
                                                command.BankAccount.AccountNumber);
            shop.ImageDocuments =
                new ImageDocuments(command.ImageDocuments.FaceImage, command.ImageDocuments.NationalCardImage);
            var province = _provinceRepository.Find(command.ShopAddress.ProvinceId);

            if (province == null)
            {
                throw new DomainException("استان یافت نشد");
            }

            var city = province.Cities.SingleOrDefault(item => item.Id == command.ShopAddress.CityId);

            if (city == null)
            {
                throw new DomainException("شهر یافت نشد");
            }

            var zone = city.Zones.SingleOrDefault(item => item.Id == command.ShopAddress.ZoneId);

            if (zone == null)
            {
                throw new DomainException("منطقه یافت نشد");
            }

            shop.ShopAddress = new ShopAddress(city.Id, city.Code, city.CityName, command.ShopAddress.AddressText,
                                               command.ShopAddress.PhoneNumber, command.ShopAddress.Position.ToDbGeography(),
                                               command.ShopAddress.ShopMobileNumber, zone.Id, province.Id, province.Code, province.Name);

            return(new UpdateShopCommandResponse());
        }
Esempio n. 5
0
        public async Task UpdateShop_ShouldFail_WithShopNull_Test()
        {
            var shop = new CreateShopCommand
            {
                Name     = null,
                Location = new LocationDto
                {
                    Street   = null,
                    PostCode = null,
                    Country  = null,
                    City     = null,
                    Address  = null,
                    Region   = null
                },
                Contact = new ContactDto
                {
                    Phone = null,
                    Email = null
                }
            };
            var createShopResult = await Mediator.Send(shop);

            createShopResult.Status.Should().Be(ResultStatus.BadRequest);

            var id      = new Random();
            var command = new UpdateShopCommand
            {
                NewName = Guid.NewGuid().ToString(),
                OldName = Guid.NewGuid().ToString(),
                ShopId  = id.Next()
            };

            var result = await Mediator.Send(command);

            result.Status.Should().Be(ResultStatus.NotFound);
        }
Esempio n. 6
0
        public async Task <UpdateShopValidationResult> Validate(UpdateShopCommand shop, string subject)
        {
            if (shop == null)
            {
                throw new ArgumentNullException(nameof(shop));
            }

            if (string.IsNullOrWhiteSpace(subject))
            {
                throw new ArgumentNullException(nameof(subject));
            }

            var record = await _shopRepository.Get(shop.Id);

            if (record == null)
            {
                return(new UpdateShopValidationResult(ErrorDescriptions.TheShopDoesntExist));
            }

            // 1. Check mandatory parameters.
            if (!IsValid(shop.Name, 1, 15))
            {
                return(new UpdateShopValidationResult(string.Format(ErrorDescriptions.TheParameterIsMandatoryAndShouldContainsBetween, Constants.DtoNames.Shop.Name, 1, 15)));
            }

            if (!IsValid(shop.Description, 1, 255))
            {
                return(new UpdateShopValidationResult(string.Format(ErrorDescriptions.TheParameterIsMandatoryAndShouldContainsBetween, Constants.DtoNames.Shop.Description, 5, 15)));
            }

            if (!IsValid(shop.GooglePlaceId))
            {
                return(new UpdateShopValidationResult(string.Format(ErrorDescriptions.TheParameterIsMandatory, Constants.DtoNames.Shop.GooglePlaceId)));
            }

            if (!IsValid(shop.StreetAddress))
            {
                return(new UpdateShopValidationResult(string.Format(ErrorDescriptions.TheParameterIsMandatory, Constants.DtoNames.Shop.StreetAddress)));
            }

            if (!IsValid(shop.PostalCode))
            {
                return(new UpdateShopValidationResult(string.Format(ErrorDescriptions.TheParameterIsMandatory, Constants.DtoNames.Shop.PostalCode)));
            }

            if (!IsValid(shop.Locality))
            {
                return(new UpdateShopValidationResult(string.Format(ErrorDescriptions.TheParameterIsMandatory, Constants.DtoNames.Shop.Locality)));
            }

            if (!IsValid(shop.Country))
            {
                return(new UpdateShopValidationResult(string.Format(ErrorDescriptions.TheParameterIsMandatory, Constants.DtoNames.Shop.Country)));
            }

            if (shop.ProductCategories != null)
            {
                if (shop.ProductCategories.Any(f => string.IsNullOrWhiteSpace(f.Name)))
                {
                    return(new UpdateShopValidationResult(string.Format(ErrorDescriptions.TheParameterIsMandatory, Constants.DtoNames.Shop.ProductCategories + "." + Constants.DtoNames.ProductCategory.Name)));
                }

                if (shop.ProductCategories.GroupBy(c => c.Name).Any(kvp => kvp.Count() > 1))
                {
                    return(new UpdateShopValidationResult(string.Format(ErrorDescriptions.DuplicateValues, Constants.DtoNames.Shop.ProductCategories)));
                }

                if (shop.ProductCategories.Count() > 6)
                {
                    return(new UpdateShopValidationResult(ErrorDescriptions.OnlyEightCategoriesCanBeAdded));
                }
            }

            if (shop.ProductFilters != null)
            {
                if (shop.ProductFilters.Any(f => string.IsNullOrWhiteSpace(f.Name)))
                {
                    return(new UpdateShopValidationResult(string.Format(ErrorDescriptions.TheParameterIsMandatory, Constants.DtoNames.Shop.Filters + "." + Constants.DtoNames.Filter.Name)));
                }

                if (shop.ProductFilters.Any(f => f.Values == null || !f.Values.Any()))
                {
                    return(new UpdateShopValidationResult(string.Format(ErrorDescriptions.TheParameterIsMandatory, Constants.DtoNames.Shop.Filters + "." + Constants.DtoNames.Filter.Values)));
                }

                if (shop.ProductFilters.GroupBy(c => c.Name).Any(kvp => kvp.Count() > 1))
                {
                    return(new UpdateShopValidationResult(string.Format(ErrorDescriptions.DuplicateValues, Constants.DtoNames.Shop.Filters)));
                }

                if (shop.ProductFilters.Any(p => p.Values.GroupBy(v => v).Count() != p.Values.Count()))
                {
                    return(new UpdateShopValidationResult(string.Format(ErrorDescriptions.DuplicateValues, Constants.DtoNames.Shop.Filters + "." + Constants.DtoNames.Filter.Values)));
                }
            }

            if (shop.UpdateGameEntities != null)
            {
                var updateGameEntitiesIdDinstinct = shop.UpdateGameEntities.Where(g => !string.IsNullOrWhiteSpace(g.ProductCategoryId)).Select(g => g.ProductCategoryId);
                var productCategoriesId           = record.ProductCategories.Select(pc => pc.Id);
                if (updateGameEntitiesIdDinstinct.Any(id => !productCategoriesId.Contains(id)))
                {
                    return(new UpdateShopValidationResult(ErrorDescriptions.TheProductCategoriesDoesntExist));
                }

                var groupedResult = shop.UpdateGameEntities.GroupBy(u => u.Name);
                foreach (var kvp in groupedResult)
                {
                    var gameEntity = await _gameEntityRepository.Get(kvp.Key);

                    if (gameEntity.MaxAvailableInStock < kvp.Count() && !gameEntity.IsShelf)
                    {
                        return(new UpdateShopValidationResult(string.Format(ErrorDescriptions.TheNumberOfAllowedEntitiesExceed, kvp.Key, gameEntity.MaxAvailableInStock)));
                    }

                    if (gameEntity.IsShelf && kvp.Count() > shop.ProductCategories.Count())
                    {
                        return(new UpdateShopValidationResult(string.Format(ErrorDescriptions.TheNumberOfAllowedShelvesExceed, gameEntity.MaxAvailableInStock)));
                    }
                }
            }

            return(new UpdateShopValidationResult(record));
        }
Esempio n. 7
0
        public async Task <IHttpActionResult> Put(UpdateShopCommand command)
        {
            var response = await Bus.Send <UpdateShopCommand, UpdateShopCommandResponse>(command);

            return(Ok(response));
        }
Esempio n. 8
0
        public async Task <ActionResult> UpdateShop([FromRoute] long shopId, [FromBody] UpdateShopCommand command)
        {
            await _mediator.Send(command);

            return(NoContent());
        }
Esempio n. 9
0
        public async Task <IActionResult> Execute(JObject jObj, HttpRequest request, string id, string subject, string commonId)
        {
            if (jObj == null)
            {
                throw new ArgumentNullException(nameof(jObj));
            }

            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            if (string.IsNullOrWhiteSpace(id))
            {
                throw new ArgumentNullException(nameof(id));
            }

            if (string.IsNullOrWhiteSpace(subject))
            {
                throw new ArgumentNullException(nameof(subject));
            }

            // 1. Check the request
            UpdateShopCommand command = null;

            try
            {
                command = _requestBuilder.GetUpdateShop(jObj);
            }
            catch (ArgumentException ex)
            {
                var error = _responseBuilder.GetError(ErrorCodes.Request, ex.Message);
                return(_controllerHelper.BuildResponse(HttpStatusCode.BadRequest, error));
            }

            // 2. Add image
            command.Id             = id;
            command.UpdateDateTime = DateTime.UtcNow;
            command.CommonId       = commonId;
            if (command.ProductCategories != null)
            {
                foreach (var productCategory in command.ProductCategories)
                {
                    if (string.IsNullOrWhiteSpace(productCategory.Id))
                    {
                        productCategory.Id = Guid.NewGuid().ToString();
                    }
                }
            }

            if (command.ProductFilters != null)
            {
                foreach (var productFilter in command.ProductFilters)
                {
                    if (string.IsNullOrWhiteSpace(productFilter.Id))
                    {
                        productFilter.Id = Guid.NewGuid().ToString();
                    }

                    if (productFilter.Values != null)
                    {
                        foreach (var value in productFilter.Values)
                        {
                            if (string.IsNullOrWhiteSpace(value.Id))
                            {
                                value.Id = Guid.NewGuid().ToString();
                            }
                        }
                    }
                }
            }

            var validationResult = await _updateShopValidator.Validate(command, subject);

            if (!validationResult.IsValid)
            {
                var error = _responseBuilder.GetError(ErrorCodes.Request, validationResult.Message);
                return(_controllerHelper.BuildResponse(HttpStatusCode.BadRequest, error));
            }

            if (!string.IsNullOrWhiteSpace(command.BannerImage))
            {
                string bannerImage = null;
                if (AddImage(command.BannerImage, request, "banner", out bannerImage))
                {
                    command.BannerImage = bannerImage;
                }
                else
                {
                    command.BannerImage = validationResult.Shop.BannerImage;
                }
            }
            else
            {
                command.BannerImage = validationResult.Shop.BannerImage;
            }

            if (!string.IsNullOrWhiteSpace(command.ProfileImage))
            {
                string profileImage = null;
                if (AddImage(command.ProfileImage, request, "profile", out profileImage))
                {
                    command.ProfileImage = profileImage;
                }
                else
                {
                    command.ProfileImage = validationResult.Shop.ProfileImage;
                }
            }
            else
            {
                command.ProfileImage = validationResult.Shop.ProfileImage;
            }

            _commandSender.Send(command);
            return(new OkResult());
        }
Esempio n. 10
0
        public async Task Handle(UpdateShopCommand message)
        {
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            IEnumerable <ShopAggregateGameEntity> shopGameEntities = null;

            if (message.UpdateGameEntities != null)
            {
                shopGameEntities = message.UpdateGameEntities.Select(u => new ShopAggregateGameEntity
                {
                    Id   = u.Id,
                    Name = u.Name,
                    Row  = u.Row,
                    Col  = u.Col,
                    Type = u.Type,
                    ProductCategoryId = u.ProductCategoryId,
                    IsFlipped         = u.IsFlipped
                }).ToList();
            }

            var shop = await _shopRepository.Get(message.Id);

            if (shop == null)
            {
                return;
            }

            shop.Name           = message.Name;
            shop.Description    = message.Description;
            shop.BannerImage    = message.BannerImage;
            shop.ProfileImage   = message.ProfileImage;
            shop.StreetAddress  = message.StreetAddress;
            shop.PostalCode     = message.PostalCode;
            shop.Locality       = message.Locality;
            shop.Country        = message.Country;
            shop.GooglePlaceId  = message.GooglePlaceId;
            shop.Longitude      = message.Longitude;
            shop.Latitude       = message.Latitude;
            shop.UpdateDateTime = message.UpdateDateTime;
            shop.TagNames       = message.TagNames;
            if (message.ProductCategories != null)
            {
                shop.ProductCategories = message.ProductCategories.Select(p => new ShopProductCategory
                {
                    Id             = p.Id,
                    Name           = p.Name,
                    Description    = p.Description,
                    CreateDateTime = DateTime.UtcNow,
                    UpdateDateTime = DateTime.UtcNow
                }).ToList();
            }

            if (message.ProductFilters != null)
            {
                shop.ShopFilters = message.ProductFilters.Select(f => new ShopFilter
                {
                    Id     = f.Id,
                    Name   = f.Name,
                    Values = f.Values == null ? new List <ShopFilterValue>() : f.Values.Select(v => new ShopFilterValue
                    {
                        Id             = v.Id,
                        CreateDateTime = DateTime.UtcNow,
                        UpdateDateTime = DateTime.UtcNow,
                        Content        = v.Content
                    })
                }).ToList();
            }

            shop.ShopGameEntities = shopGameEntities;
            await _shopRepository.Update(shop);

            _eventPublisher.Publish(new ShopUpdatedEvent
            {
                ShopId    = message.Id,
                Name      = message.Name,
                Longitude = message.Longitude,
                Latitude  = message.Latitude,
                CommonId  = message.CommonId
            });
        }