public async Task <bool> UpdateCreditAsync(UpdateCreditRequest request, int creditId) { var updateDto = new UpdateCreditDto { Id = creditId, BankAccountNumber = request.BankAccountNumber, BankName = request.BankName, ContractNumber = request.ContractNumber, CurrencyId = request.CurrencyId, Description = request.Description, InitialCreditValue = request.InitialCreditValue, InitNoOfPayments = request.InitNoOfPayments, Name = request.Name, }; var credit = _mapper.Map <Credit>(updateDto); return(await _baseRepository.UpdateEntityAsync(credit)); }
public async Task <IActionResult> UpdateCredit([FromRoute] int creditId, [FromBody] UpdateCreditRequest request) { var userId = User.Claims.FirstOrDefault(c => c.Type == "id").Value; // check if user exists var userExists = await _identityService.CheckIfUserExists(userId); if (!userExists) { return(NotFound(new ErrorResponse(new ErrorModel { Message = $"There is no user with id: {userId}" }))); } var creditInDb = await _creditService.GetCreditAsync(creditId); if (creditInDb == null) { return(NotFound(new ErrorResponse(new ErrorModel { Message = $"There is no credit with id: {creditId}" }))); } if (creditInDb.UserId != userId) { return(Forbid()); } var updateCredit = await _creditService.UpdateCreditAsync(request, creditId); if (!updateCredit) { return(BadRequest(new ErrorResponse(new ErrorModel { Message = $"Could not update credit with id:{creditId}" }))); } var updatedCredit = await _creditService.GetCreditAsync(creditId); return(Ok(new Response <CreditResponse>(_mapper.Map <CreditResponse>(updatedCredit)))); }