private DebitCard CreateDebitCard(CreateDebitCardRequest request) { return(new DebitCard() { DebitCardNo = request.DebitCardNo, PIN = request.PIN, ExpirationDate = request.ExpirationDate, BankAccountId = request.BankAccountId, CreatedBy = _httpContextAccessor.HttpContext.User.Identity.Name }); }
/// <summary> /// Add or Edit an existing debit card /// </summary> /// <param name="debitCardId"></param> /// <param name="request"></param> /// <param name="cancellationToken"></param> /// <returns></returns> public async Task <Response> AddOrEditDebitCardAsync(int debitCardId, CreateDebitCardRequest request, CancellationToken cancellationToken = default) { var responseModel = new Response(); if (debitCardId != 0) { var debitCard = await _debitCardsRepo.FindByIdAsync(debitCardId); try { if (debitCard != null) { debitCard.DebitCardNo = request.DebitCardNo; debitCard.ExpirationDate = request.ExpirationDate; debitCard.BankAccountId = request.BankAccountId; debitCard.LastModifiedBy = _httpContextAccessor.HttpContext.User.Identity.Name; debitCard.LastModifiedOn = DateTime.UtcNow; await _debitCardsRepo.UpdateAsync(debitCard); } else { responseModel.AddError(ExceptionCreator.CreateNotFoundError(nameof(debitCard), $"Debit card of id: {debitCardId} not found")); return(responseModel); } } catch (Exception ex) { responseModel.AddError(ExceptionCreator.CreateInternalServerError(ex.ToString())); } } else { try { await _debitCardsRepo.AddAsync(CreateDebitCard(request)); } catch (Exception ex) { responseModel.AddError(ExceptionCreator.CreateInternalServerError(ex.ToString())); } } return(responseModel); }
public async Task <IActionResult> AddOrEditDebitCard([FromRoute] int debitCardId, [FromBody] CreateDebitCardRequest request, CancellationToken cancellationToken = default) { var apiResponse = new Response(); var user = await _userManager.GetUserAsync(User); var customer = await _customerService.GetCustomerByCreditCardIdsync(debitCardId, cancellationToken); if (customer == null) { apiResponse.AddError(ExceptionCreator.CreateNotFoundError(nameof(customer))); return(NotFound(apiResponse)); } if (user.Id != customer?.Data?.UserId) { apiResponse.AddError(ExceptionCreator.CreateBadRequestError(nameof(user), "user is not authorized to complete this operation")); return(BadRequest(apiResponse)); } try { apiResponse = await _debitCardsService.AddOrEditDebitCardAsync(debitCardId, request, cancellationToken); if (apiResponse.Success) { return(Ok(apiResponse)); } else if (apiResponse.Errors[0].Code == StatusCodes.Status404NotFound) { return(NotFound(apiResponse)); } return(BadRequest(apiResponse)); } catch (Exception exception) { return(_actionResultMapper.Map(exception)); } }