public async Task <IMerchantGroup> CreateAsync(IMerchantGroup src)
        {
            MerchantGroupEntity entity = MerchantGroupEntity.ByOwner.Create(src);

            await _tableStorage.InsertThrowConflict(entity);

            AzureIndex index = MerchantGroupEntity.IndexById.Create(entity);

            await _groupIndexStorage.InsertThrowConflict(index);

            return(Mapper.Map <MerchantGroup>(entity));
        }
        public async Task UpdateAsync(IMerchantGroup src)
        {
            try
            {
                await _merchantGroupRepository.UpdateAsync(src);
            }
            catch (KeyNotFoundException ex)
            {
                _log.Error(ex, context: src.ToDetails());

                throw new MerchantGroupNotFoundException(src.Id);
            }
        }
        public Task <IMerchantGroup> CreateAsync(IMerchantGroup src)
        {
            try
            {
                return(_merchantGroupRepository.CreateAsync(src));
            }
            catch (DuplicateKeyException ex)
            {
                _log.Error(ex, context: src.ToDetails());

                throw new MerchantGroupAlreadyExistsException(src.DisplayName);
            }
        }
        public async Task <IActionResult> Get(string id)
        {
            try
            {
                IMerchantGroup group = await _merchantGroupService.GetAsync(Uri.UnescapeDataString(id));

                return(Ok(Mapper.Map <MerchantGroupResponse>(group)));
            }
            catch (InvalidRowKeyValueException e)
            {
                _log.Error(e, context: $"{e.Variable}: {e.Value}");

                return(NotFound(ErrorResponse.Create("Group not found")));
            }
        }
        public async Task <IActionResult> Add([FromBody] AddMerchantGroupModel request)
        {
            foreach (var requestMerchant in request.Merchants)
            {
                if (await _merchantService.GetAsync(requestMerchant) == null)
                {
                    return(NotFound(ErrorResponse.Create($"Merchant [{requestMerchant}] not found")));
                }
            }

            try
            {
                IMerchantGroup group = await _merchantGroupService.CreateAsync(Mapper.Map <MerchantGroup>(request));

                return(Ok(Mapper.Map <MerchantGroupResponse>(group)));
            }
            catch (MerchantGroupAlreadyExistsException e)
            {
                _log.Error(e, context: request.ToDetails());

                return(BadRequest(ErrorResponse.Create(e.Message)));
            }
        }
        public async Task UpdateAsync(IMerchantGroup src)
        {
            AzureIndex index = await _groupIndexStorage.GetDataAsync(
                MerchantGroupEntity.IndexById.GeneratePartitionKey(src.Id),
                MerchantGroupEntity.IndexById.GenerateRowKey());

            if (index == null)
            {
                throw new KeyNotFoundException();
            }

            MerchantGroupEntity updatedEntity = await _tableStorage.MergeAsync(
                MerchantGroupEntity.ByOwner.GeneratePartitionKey(index.PrimaryPartitionKey),
                MerchantGroupEntity.ByOwner.GenerateRowKey(index.PrimaryRowKey),
                entity =>
            {
                if (src.DisplayName != null)
                {
                    entity.DisplayName = src.DisplayName;
                }

                if (!string.IsNullOrEmpty(src.Merchants))
                {
                    entity.Merchants = src.Merchants;
                }

                entity.MerchantGroupUse = src.MerchantGroupUse;

                return(entity);
            });

            if (updatedEntity == null)
            {
                throw new KeyNotFoundException();
            }
        }