示例#1
0
        public async Task <IActionResult> UpdateAsync(int?id, [FromBody] CurrencyUpdateDto updateDto)
        {
            if (id == null || id.Value != updateDto.Id)
            {
                return(BadRequest(new { Error = "Invalid currency id." }));
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var currency = await _unitOfWork.CurrencyRepository.GetByIdAsync(id.Value);

            if (currency == null)
            {
                return(BadRequest(new { Error = $"Currency with an id of {id.Value} cannot be found." }));
            }

            var currentUser = await _userManager.GetUserAsync(HttpContext.User);

            currency.Name         = updateDto.Name;
            currency.Symbol       = updateDto.Symbol;
            currency.UpdateUserId = currentUser?.Id;
            currency.UpdateDate   = DateTime.Now;

            await _unitOfWork.CompleteAsync();

            var dto = _mapper.Map <CurrencyDto>(currency);

            return(Ok(dto));
        }
        public ActionResult <CurrencyReadDto> Update(CurrencyUpdateDto currencyUpdateDto)
        {
            Currency currency = _currencyRepository.Get(currencyUpdateDto.Id);

            if (currency == null)
            {
                return(BadRequest("Currency with that id doesn't exist."));
            }

            currency = _mapper.Map(currencyUpdateDto, currency);

            _currencyRepository.Update(currency);

            _logger.Log("Update Currency");

            return(Ok(currency));
        }