public async Task <Coin> UpdateCoinAsync(int id, CoinUpdateDto coin)
        {
            var coinEntity = await _repositoryWrapper.Coin.GetCoinByIdAsync(id);

            if (coinEntity == null)
            {
                return(null);
            }

            _mapper.Map(coin, coinEntity);
            _repositoryWrapper.Coin.UpdateCoin(coinEntity);
            await _repositoryWrapper.SaveAsync();

            return(coinEntity);
        }
        public async Task <IActionResult> UpdateCoin(Guid id, [FromBody] CoinUpdateDto coin)
        {
            if (coin == null)
            {
                return(BadRequest());
            }

            if ((coin.Note == coin.Subject) && (coin.Note != null || coin.Subject != null))
            {
                ModelState.AddModelError(nameof(CoinUpdateDto),
                                         "The provided note should be different from the coin's subject");
            }

            if (!ModelState.IsValid)
            {
                return(new UnprocessableEntityObjectResult(ModelState));
            }

            if (!await _countryService.CountryExists(coin.CountryId))
            {
                return(BadRequest());
            }

            var retrievedCoin = await _coinService.FindCoinById(id);

            if (retrievedCoin == null)
            {
                return(NotFound());
            }

            var collectorValue         = _mapper.Map <CollectorValue>(coin.CollectorValue);
            var existingCollectorValue = await _collectorValueService.FindCollectorValueByValues(collectorValue);

            retrievedCoin.CollectorValueId = existingCollectorValue == null?Guid.NewGuid() : existingCollectorValue.Id;

            retrievedCoin.CollectorValue = collectorValue;

            _mapper.Map(coin, retrievedCoin);
            _coinService.UpdateCoin(retrievedCoin);

            if (!await _coinService.Save())
            {
                throw new Exception($"Updating coin {id} failed on save.");
            }

            return(NoContent());
        }
        public CoinUpdateDto GetForEdit(int id)
        {
            CoinUpdateDto coinDto = null;

            try
            {
                var coin = _unitOfWork.GenericRepository <Coin>().GetById(id);
                if (coin != null)
                {
                    coinDto = Mapper.Map <Coin, CoinUpdateDto>(coin);
                }
            }
            catch (Exception ex)
            {
                Tracing.SaveException(ex);
            }

            return(coinDto);
        }
        public async Task <ActionResult> UpdateCoin(int id, [FromBody] CoinUpdateDto coin)
        {
            if (coin == null)
            {
                _logger.LogError("Coin object sent from client is null.");
                return(BadRequest("Coin object is null"));
            }

            if (coin.ID != id)
            {
                _logger.LogError("Coin object sent from client doesn't match with provided resource ID.");
                return(BadRequest("Coin object doesn't match provided resource ID"));
            }

            var coinEntity = await _coinService.UpdateCoinAsync(id, coin);

            if (coinEntity == null)
            {
                _logger.LogError($"Coin with id: {id.ToString()}, hasn't been found in db.");
                return(NotFound());
            }

            return(NoContent());
        }
        public CoinDto Update(CoinUpdateDto dto)
        {
            CoinDto coinDto = null;

            try
            {
                var coin = _unitOfWork.GenericRepository <Coin>().GetById(dto.Id);
                Mapper.Map <CoinUpdateDto, Coin>(dto, coin);
                coin.ModifiedBy = _appSession.GetUserName();
                _unitOfWork.CreateTransaction();

                _unitOfWork.GenericRepository <Coin>().Update(coin);
                _unitOfWork.Save();

                _unitOfWork.Commit();

                coinDto = Mapper.Map <Coin, CoinDto>(coin);
            }
            catch (Exception ex)
            {
                Tracing.SaveException(ex);
            }
            return(coinDto);
        }