Пример #1
0
        /// <summary>
        /// Add or Edit an existing branch
        /// </summary>
        /// <param name="branchId"></param>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task <Response> AddOrEditBranchAsync(int branchId, CreateBranchRequest request, CancellationToken cancellationToken = default)
        {
            var responseModel = new Response();

            if (await BranchExists(request.Address.CountryId, request.Address.CityId, request.Name))
            {
                responseModel.AddError(ExceptionCreator.CreateBadRequestError("branch", $"branch name: {request.Name} already exists"));
                return(responseModel);
            }

            if (branchId != 0)
            {
                var branch = await _branchRepo.FindByIdAsync(branchId);

                if (branch != null)
                {
                    branch.Name           = request.Name;
                    branch.Code           = request.Code;
                    branch.Address        = request.Address;
                    branch.LastModifiedBy = _httpContextAccessor.HttpContext.User.Identity.Name;
                    branch.LastModifiedOn = DateTime.UtcNow;

                    await _branchRepo.UpdateAsync(_dbContext, branch);
                }
                else
                {
                    responseModel.AddError(ExceptionCreator.CreateNotFoundError(nameof(branch), $"branch of Id: {branchId} not found"));
                    return(responseModel);
                }
            }
            else
            {
                var newAddress = CreateAddress(request);
                var newBranch  = CreateBranch(request);

                var dbContextTransaction = await _dbContext.Database.BeginTransactionAsync(cancellationToken);

                using (dbContextTransaction)
                {
                    try
                    {
                        await _addressRepo.AddAsync(newAddress, _dbContext);

                        newBranch.AddressId = newAddress.Id;

                        await _branchRepo.AddAsync(_dbContext, newBranch);

                        await dbContextTransaction.CommitAsync(cancellationToken);
                    }
                    catch (Exception ex)
                    {
                        await dbContextTransaction.RollbackAsync(cancellationToken);

                        responseModel.AddError(ExceptionCreator.CreateInternalServerError(ex.ToString()));
                    }
                }
            }

            return(responseModel);
        }
Пример #2
0
        /// <summary>
        /// forgot password request
        /// </summary>
        /// <param name="email"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task <Response> ForgotPasswordAsync(string email, CancellationToken cancellationToken)
        {
            var apiResponse = new ApiResponse <ForgotPasswordResponse>();

            var user = await _userManager.FindByEmailAsync(email);

            if (user == null)
            {
                apiResponse.AddError(ExceptionCreator.CreateNotFoundError(nameof(user), $"user not found"));
                return(apiResponse);
            }

            var token = await _userManager.GeneratePasswordResetTokenAsync(user);

            var encodedToken = Encoding.UTF8.GetBytes(token);
            var validToken   = WebEncoders.Base64UrlEncode(encodedToken);

            string url = $"{_configuration["AppUrl"]}/reset-password?email={email}&token={validToken}";

            await _mailService.SendEmailAsync(email, "Reset Password", "<h1>Follow the instructions to reset your passwod</h1>" +
                                              $"<p>To reset your password <a href='{url}'>Click here</a></p>", cancellationToken);


            return(apiResponse);
        }
Пример #3
0
        /// <summary>
        /// confirm email
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="token"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task <Response> ConfirmEmailAsync(string userId, string token, CancellationToken cancellationToken)
        {
            var apiResponse = new Response();

            var user = await _userManager.FindByIdAsync(userId);

            if (user == null)
            {
                apiResponse.AddError(ExceptionCreator.CreateNotFoundError(nameof(user), $"user not found"));
                return(apiResponse);
            }

            var decodedToken    = WebEncoders.Base64UrlDecode(token);
            var normalizedToken = WebEncoders.Base64UrlEncode(decodedToken);

            var result = await _userManager.ConfirmEmailAsync(user, normalizedToken);

            if (result.Succeeded)
            {
                apiResponse.Message = "Your email has been confirmed successfully!";
                return(apiResponse);
            }

            foreach (var error in result.Errors)
            {
                apiResponse.AddError(new ErrorResponse(error.Description));
            }

            return(apiResponse);
        }
Пример #4
0
        public async Task <IActionResult> CreateRole([FromQuery] string roleName, CancellationToken cancellationToken)
        {
            var apiRsponse = new Response();

            if (await CheckRoleExists(roleName))
            {
                apiRsponse.AddError(ExceptionCreator.CreateBadRequestError($"Role name '{roleName}' already exists"));

                return(BadRequest(apiRsponse));
            }

            IdentityRole identityRole = new IdentityRole
            {
                Name = roleName
            };

            IdentityResult result = await _roleManager.CreateAsync(identityRole);

            if (result.Succeeded)
            {
                return(Ok(apiRsponse));
            }

            foreach (IdentityError error in result.Errors)
            {
                apiRsponse.AddError(ExceptionCreator.CreateUnexpectedError(Convert.ToInt32(error.Code), error.Description));
            }

            return(BadRequest(apiRsponse));
        }
Пример #5
0
        public async Task <IActionResult> ConfirmEmail(string userId, string token, CancellationToken cancellationToken)
        {
            var apiResponse = new Response();

            var user = await _userManager.GetUserAsync(User);

            if (user == null)
            {
                apiResponse.AddError(ExceptionCreator.CreateNotFoundError(nameof(user), $"user not found"));
                return(NotFound(apiResponse));
            }

            try
            {
                apiResponse = await _authService.ConfirmEmailAsync(userId, token, cancellationToken);

                if (apiResponse.Success)
                {
                    return(Ok(apiResponse));
                }

                return(BadRequest(apiResponse));
            }
            catch (Exception exception)
            {
                return(_actionResultMapper.Map(exception));
            }
        }
Пример #6
0
        /// <summary>
        /// Login user
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task <ApiResponse <LoginResponse> > LoginAsync(LoginRequest request, CancellationToken cancellationToken)
        {
            var apiResponse = new ApiResponse <LoginResponse>();

            var user = await _userManager.FindByNameAsync(request.CustomerNo);

            if (user == null)
            {
                apiResponse.AddError(ExceptionCreator.CreateUnauthorizedError("Invalid login credentials"));
                return(apiResponse);
            }

            var result = await _signInManager.PasswordSignInAsync(user, request.Password, false, true);

            if (result.Succeeded)
            {
                apiResponse.Data = new LoginResponse()
                {
                    AccessToken  = _tokenService.GenerateAccessToken(await user.GetClaimsAsync(_userManager)),
                    RefreshToken = _tokenService.GenerateRefreshToken()
                };

                user.RefreshToken = apiResponse.Data.RefreshToken;
                await _userManager.UpdateAsync(user);
            }
            else
            {
                apiResponse.AddError(ExceptionCreator.CreateUnauthorizedError("Invalid login attempt"));
                return(apiResponse);
            }

            return(apiResponse);
        }
Пример #7
0
        public async Task <IActionResult> GetCitiesByCountryId([FromRoute] int countryId, CancellationToken cancellationToken = default)
        {
            var apiResponse = new Response();

            try
            {
                if (!await _countriesService.CountryExists(countryId))
                {
                    apiResponse.AddError(ExceptionCreator.CreateNotFoundError("country", $"country of id: {countryId} not found"));
                    return(NotFound(apiResponse));
                }

                apiResponse = await _citiesService.GetCitiesByCountryIdAsync(countryId, cancellationToken);

                if (apiResponse.Success)
                {
                    return(Ok(apiResponse));
                }


                return(BadRequest(apiResponse));
            }
            catch (Exception exception)
            {
                return(_actionResultMapper.Map(exception));
            }
        }
Пример #8
0
        /// <summary>
        /// Retrieve country for the specified id
        /// </summary>
        /// <param name="countryId"></param>
        /// <param name="includeCities"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task <ApiResponse <CountryResponse> > GetCountryByIdAsync(int countryId, bool includeCities = false, CancellationToken cancellationToken = default)
        {
            var responseModel = new ApiResponse <CountryResponse>();

            Country country = null;

            if (includeCities)
            {
                country = await _countriesRepo.FindByIdWithCitiesAsync(countryId);
            }

            else
            {
                country = await _countriesRepo.FindByIdAsync(countryId);
            }


            if (country == null)
            {
                responseModel.AddError(ExceptionCreator.CreateNotFoundError(nameof(country), $"country of id {countryId}: not found"));
                return(responseModel);
            }

            if (includeCities)
            {
                responseModel.Data = await CreateCountryWithCitiesResponse(country);
            }

            else
            {
                responseModel.Data = CreateCountryResponse(country);
            }

            return(responseModel);
        }
Пример #9
0
        /// <summary>
        /// Retrieve customer by IBAN
        /// </summary>
        /// <param name="iban"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task <ApiResponse <CustomerResponse> > GetCustomerByIBANAsync(string iban, CancellationToken cancellationToken = default)
        {
            var responseModel = new ApiResponse <CustomerResponse>();

            var bankAccount = await _bankAccountRepo.FindByIBANAsync(iban);

            if (bankAccount == null)
            {
                responseModel.AddError(ExceptionCreator.CreateNotFoundError(nameof(bankAccount), $"bank Account of IBAN: {iban} not found"));
                return(responseModel);
            }

            var customer = await _customerRepo.FindByIBANAsync(iban);

            if (customer == null)
            {
                responseModel.AddError(ExceptionCreator.CreateNotFoundError(nameof(customer), $"bank Account's holder of IBAN: {iban} not found"));
                return(responseModel);
            }

            var address = await _addressRepo.FindByIdAsync(customer.AddressId);

            responseModel.Data = CreateCustomerResponse(customer, address);

            return(responseModel);
        }
Пример #10
0
        /// <summary>
        /// Reset password
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task <Response> ResetPasswordAsync(ResetPasswordRequest request, CancellationToken cancellationToken)
        {
            var apiResponse = new Response();

            var user = await _userManager.FindByEmailAsync(request.Email);

            if (user == null)
            {
                apiResponse.AddError(ExceptionCreator.CreateNotFoundError(nameof(user), $"user not found"));
                return(apiResponse);
            }

            var result = await _userManager.ResetPasswordAsync(user, request.Token, request.NewPassword);

            if (result.Succeeded)
            {
                apiResponse.Message = "Password has been reset successfully!";
                return(apiResponse);
            }

            foreach (var error in result.Errors)
            {
                apiResponse.AddError(new ErrorResponse(error.Description));
            }

            return(apiResponse);
        }
Пример #11
0
        /// <summary>
        /// Add or Edit an existing city
        /// </summary>
        /// <param name="cityId"></param>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task <Response> AddOrEditCityAsync(int cityId, CreateCityRequest request, CancellationToken cancellationToken = default)
        {
            var responseModel = new Response();

            if (await _citiesRepo.CityNameExists(request.CountryId, request.Name))
            {
                responseModel.AddError(ExceptionCreator.CreateBadRequestError("city", "city name does already exist"));
                return(responseModel);
            }

            if (cityId != 0)
            {
                var city = await _dbContext.Cities.FirstOrDefaultAsync(c => c.Id == cityId);

                try
                {
                    if (city != null)
                    {
                        city.CountryId      = request.CountryId;
                        city.Name           = request.Name;
                        city.LastModifiedBy = _httpContextAccessor.HttpContext.User.Identity.Name;
                        city.LastModifiedOn = DateTime.UtcNow;

                        await _citiesRepo.UpdateAsync(city);
                    }
                    else
                    {
                        responseModel.AddError(ExceptionCreator.CreateNotFoundError(nameof(city), $"city of Id: { cityId} not found"));
                        return(responseModel);
                    }
                }
                catch (Exception ex)
                {
                    responseModel.AddError(ExceptionCreator.CreateInternalServerError(ex.ToString()));
                }
            }
            else
            {
                try
                {
                    await _citiesRepo.AddAsync(CreateCity(request));
                }
                catch (Exception ex)
                {
                    responseModel.AddError(ExceptionCreator.CreateInternalServerError(ex.ToString()));
                }
            }

            return(responseModel);
        }
Пример #12
0
        /// <summary>
        /// Add or Edit an existing district
        /// </summary>
        /// <param name="districtId"></param>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task <Response> AddOrEditDistrictAsync(int districtId, CreateDistrictRequest request, CancellationToken cancellationToken = default)
        {
            var responseModel = new Response();

            if (await _districtsRepo.DistrictNameExists(request.CityId, request.Name))
            {
                responseModel.AddError(ExceptionCreator.CreateBadRequestError("cdistrict name does already exist"));
                return(responseModel);
            }

            if (districtId != 0)
            {
                var district = await _dbContext.Districts.FirstOrDefaultAsync(c => c.Id == districtId);

                try
                {
                    if (district != null)
                    {
                        district.CityId         = request.CityId;
                        district.Name           = request.Name;
                        district.LastModifiedBy = _httpContextAccessor.HttpContext.User.Identity.Name;
                        district.LastModifiedOn = DateTime.UtcNow;

                        _dbContext.Districts.Update(district);
                    }
                    else
                    {
                        responseModel.AddError(ExceptionCreator.CreateNotFoundError(nameof(district), $"district of Id: { districtId} not found"));
                        return(responseModel);
                    }
                }
                catch (Exception ex)
                {
                    responseModel.AddError(ExceptionCreator.CreateInternalServerError(ex.ToString()));
                }
            }
            else
            {
                try
                {
                    await _districtsRepo.AddAsync(CreateDistrict(request));
                }
                catch (Exception ex)
                {
                    responseModel.AddError(ExceptionCreator.CreateInternalServerError(ex.ToString()));
                }
            }

            return(responseModel);
        }
Пример #13
0
        /// <summary>
        /// Add or Edit an existing address
        /// </summary>
        /// <param name="cityId"></param>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task <Response> AddOrEditAddressAsync(int addressId, CreateAddressRequest request, CancellationToken cancellationToken = default)
        {
            var responseModel = new Response();


            if (addressId != 0)
            {
                var address = await _addressRepository.FindByIdAsync(addressId);

                if (address != null)
                {
                    address.Name           = request.Name;
                    address.CountryId      = request.CountryId;
                    address.CityId         = request.CityId;
                    address.DistrictId     = request.DistrictId;
                    address.Street         = request.Street;
                    address.PostalCode     = request.PostalCode;
                    address.LastModifiedBy = _httpContextAccessor.HttpContext.User.Identity.Name;
                    address.LastModifiedOn = DateTime.UtcNow;

                    await _addressRepository.UpdateAsync(address);
                }
                else
                {
                    responseModel.AddError(ExceptionCreator.CreateNotFoundError(nameof(address), $"address id: {addressId} not found"));

                    return(responseModel);
                }
            }
            else
            {
                if (await AddressExistsAsync(request.CountryId, request.CityId, request.DistrictId, request.Street, request.Name))
                {
                    responseModel.AddError(ExceptionCreator.CreateBadRequestError("address", "naddress name does already exist"));

                    return(responseModel);
                }

                try
                {
                    await _addressRepository.AddAsync(CreateAddress(request));
                }
                catch (Exception ex)
                {
                    responseModel.AddError(ExceptionCreator.CreateInternalServerError(ex.ToString()));
                }
            }

            return(responseModel);
        }
Пример #14
0
        public async Task <IActionResult> GetBankAccountFastTransactions([FromRoute] string iban,
                                                                         [FromQuery] int pageNumber          = PagingConstants.DefaultPageNumber,
                                                                         [FromQuery] int pageSize            = PagingConstants.DefaultPageSize,
                                                                         CancellationToken cancellationToken = default)
        {
            var user = await _userManager.GetUserAsync(User);

            var customer = await _customerService.GetCustomerByIBANAsync(iban, cancellationToken);

            var apiResponse = new ApiResponse <FastTransactionListResponse>();

            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
            {
                var bankAccount = await _bankAccountService.GetBankAccountByIBANAsync(iban);

                if (bankAccount.Data == null)
                {
                    return(NotFound());
                }

                apiResponse = await _fastTransactionsService.GetBankAccountFastTransactionsAsync(iban, pageNumber, pageSize, cancellationToken);

                if (!apiResponse.Success)
                {
                    return(BadRequest(apiResponse));
                }
                var pagedResponse = new PagedResponse <FastTransactionListResponse>(apiResponse.Data);

                return(Ok(pagedResponse));
            }
            catch (Exception exception)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, exception.ToString()));
            }
        }
Пример #15
0
        /// <summary>
        /// Retrieve district by id
        /// </summary>
        /// <param name="districtId"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task <ApiResponse <DistrictResponse> > GetDistrictByIdAsync(int districtId, CancellationToken cancellationToken = default)
        {
            var responseModel = new ApiResponse <DistrictResponse>();

            var district = await _districtsRepo.FindByIdAsync(districtId);

            if (district == null)
            {
                responseModel.AddError(ExceptionCreator.CreateNotFoundError(nameof(district), $"district Id: {districtId} not found"));
                return(responseModel);
            }

            responseModel.Data = CreateDistrictResponse(district);

            return(responseModel);
        }
Пример #16
0
        /// <summary>
        /// Retrieve debit card by id
        /// </summary>
        /// <param name="debitCardId"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task <ApiResponse <DebitCardResponse> > GetDebitCardByIdAsync(int debitCardId, CancellationToken cancellationToken = default)
        {
            var responseModel = new ApiResponse <DebitCardResponse>();

            var debitCard = await _debitCardsRepo.FindByIdAsync(debitCardId);

            if (debitCard == null)
            {
                responseModel.AddError(ExceptionCreator.CreateNotFoundError(nameof(DebitCard), $"Debit card of id: {debitCardId}: is not found"));
                return(responseModel);
            }

            responseModel.Data = CreateDebitCardResponse(debitCard);

            return(responseModel);
        }
        /// <summary>
        /// Retrieve fast transaction by id
        /// </summary>
        /// <param name="id"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task <ApiResponse <FastTransactionResponse> > GetFastTransactionByIdAsync(int id, CancellationToken cancellationToken = default)
        {
            var responseModel = new ApiResponse <FastTransactionResponse>();

            var transaction = await _fastTransactionsRepo.FindByIdAsync(id);

            if (transaction == null)
            {
                responseModel.AddError(ExceptionCreator.CreateNotFoundError(nameof(transaction)));
                return(responseModel);
            }

            responseModel.Data = await CreateFastTransactionResponse(transaction);

            return(responseModel);
        }
Пример #18
0
        /// <summary>
        /// Retrieve debit card by account number
        /// </summary>
        /// <param name="accountNo"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task <ApiResponse <DebitCardResponse> > GetDebitCardByAccountNoAsync(string accountNo, CancellationToken cancellationToken = default)
        {
            var responseModel = new ApiResponse <DebitCardResponse>();

            var debitCard = await _debitCardsRepo.FindByAccountNoAsync(accountNo);

            if (debitCard == null)
            {
                responseModel.AddError(ExceptionCreator.CreateNotFoundError(nameof(debitCard), $"Debit card of account No: {accountNo} not found"));
                return(responseModel);
            }

            responseModel.Data = CreateDebitCardResponse(debitCard);

            return(responseModel);
        }
Пример #19
0
        public async Task <IActionResult> AddOrEditFastTransaction([FromRoute] int id,
                                                                   [FromRoute] string iban,
                                                                   [FromBody] CreateFastTransactionRequest request,
                                                                   CancellationToken cancellationToken = default)
        {
            var user = await _userManager.GetUserAsync(User);

            var customer = await _customerService.GetCustomerByIBANAsync(iban, cancellationToken);

            var apiResponse = new Response();

            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 _fastTransactionsService.AddOrEditFastTransactionAsync(id, 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(StatusCode(StatusCodes.Status500InternalServerError, exception.ToString()));
            }
        }
Пример #20
0
        public async Task <IActionResult> AddOrEditCreditCard([FromRoute] int creditCardId, [FromBody] CreateCreditCardRequest request,
                                                              CancellationToken cancellationToken = default)
        {
            var apiResponse = new Response();

            var user = await _userManager.GetUserAsync(User);

            var customer = await _customerService.GetCustomerByCreditCardIdsync(creditCardId, 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 _creditCardsService.AddOrEditCreditCardAsync(creditCardId, 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));
            }
        }
Пример #21
0
        /// <summary>
        /// Deactivate debit card
        /// </summary>
        /// <param name="creditCardId"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task <Response> DeactivateDebitCardAsync(int debitCardId, CancellationToken cancellationToken = default)
        {
            var responseModel = new ApiResponse <DebitCardResponse>();

            var debitCard = await _debitCardsRepo.FindByIdAsync(debitCardId);

            if (debitCard != null)
            {
                debitCard.Disabled = true;
            }
            else
            {
                responseModel.AddError(ExceptionCreator.CreateNotFoundError(nameof(debitCard), $"debit card id: {debitCard} not found"));
            }

            return(responseModel);
        }
Пример #22
0
        /// <summary>
        /// Deactivate bank account for for the specified id
        /// </summary>
        /// <param name="accountId"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task <Response> DeactivateBankAccountAsync(int accountId, CancellationToken cancellationToken = default)
        {
            var responseModel = new ApiResponse <BankAccountResponse>();

            var bankAccount = await _bankAccountRepo.FindByIdAsync(accountId);

            if (bankAccount != null)
            {
                bankAccount.Disabled = true;
            }
            else
            {
                responseModel.AddError(ExceptionCreator.CreateNotFoundError(nameof(bankAccount)));
            }

            return(responseModel);
        }
Пример #23
0
        /// <summary>
        /// Add Or Edit country
        /// </summary>
        /// <param name="countryId"></param>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task <Response> AddOrEditCountryAsync(int countryId, CreateCountryRequest request, CancellationToken cancellationToken = default)
        {
            var responseModel = new Response();

            if (await CountryNameExists(request.Name))
            {
                responseModel.AddError(ExceptionCreator.CreateBadRequestError("country", "country name does already exist"));
                return(responseModel);
            }

            if (countryId != 0)
            {
                var country = await _countriesRepo.FindByIdAsync(countryId);

                if (country != null)
                {
                    country.Name           = request.Name;
                    country.Code           = request.Code;
                    country.LastModifiedBy = _httpContextAccessor.HttpContext.User.Identity.Name;
                    country.LastModifiedOn = DateTime.UtcNow;

                    await _countriesRepo.UpdateAsync(country);
                }
                else
                {
                    responseModel.AddError(ExceptionCreator.CreateNotFoundError(nameof(country), $"country of id {countryId}: not found"));
                    return(responseModel);
                }
            }

            else
            {
                try
                {
                    var newCountry = CreateCountry(request);

                    await _countriesRepo.AddAsync(newCountry);
                }
                catch (Exception ex)
                {
                    responseModel.AddError(ExceptionCreator.CreateInternalServerError(ex.ToString()));
                }
            }

            return(responseModel);
        }
        /// <summary>
        /// Add or edit existing fast transaction
        /// </summary>
        /// <param name="id"></param>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task <Response> AddOrEditFastTransactionAsync(int id, CreateFastTransactionRequest request, CancellationToken cancellationToken = default)
        {
            var responseModel = new Response();

            if (id != 0)
            {
                var fastTransaction = await _fastTransactionsRepo.FindByIdAsync(id);

                if (fastTransaction != null)
                {
                    fastTransaction.AccountId      = request.BankAccountId;
                    fastTransaction.BranchId       = request.BranchId;
                    fastTransaction.RecipientName  = request.RecipientName;
                    fastTransaction.RecipientIBAN  = request.RecipientIBAN;
                    fastTransaction.LastModifiedBy = _httpContextAccessor.HttpContext.User.Identity.Name;
                    fastTransaction.LastModifiedOn = DateTime.UtcNow;

                    try
                    {
                        await _fastTransactionsRepo.UpdateAsync(fastTransaction);
                    }
                    catch (Exception ex)
                    {
                        responseModel.AddError(ExceptionCreator.CreateInternalServerError(ex.ToString()));
                    }
                }
                else
                {
                    responseModel.AddError(ExceptionCreator.CreateNotFoundError(nameof(fastTransaction)));
                    return(responseModel);
                }
            }
            else
            {
                try
                {
                    await _fastTransactionsRepo.AddAsync(CreateFastTransaction(request));
                }
                catch (Exception ex)
                {
                    responseModel.AddError(ExceptionCreator.CreateInternalServerError(ex.ToString()));
                }
            }

            return(responseModel);
        }
Пример #25
0
        /// <summary>
        /// Add or edit loan
        /// </summary>
        /// <param name="loanId"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task <Response> AddOrEditLoanAsync(int loanId, CreateLoanRequest request, CancellationToken cancellationToken = default)
        {
            var responseModel = new Response();


            if (loanId != 0)
            {
                var loan = await _loansRepo.FindByIdAsync(loanId);

                try
                {
                    if (loan != null)
                    {
                        loan.CustomerId    = request.CustomerId;
                        loan.BankAccountId = request.BankAccountId;
                        loan.LoanType      = request.LoanType;
                        loan.Amount        = request.Amount;
                        loan.DueDate       = request.DueDate;

                        await _loansRepo.UpdateAsync(loan);
                    }
                    else
                    {
                        responseModel.AddError(ExceptionCreator.CreateNotFoundError(nameof(loan), $"loan of id: { loanId} not found"));
                        return(responseModel);
                    }
                }
                catch (Exception ex)
                {
                    responseModel.AddError(ExceptionCreator.CreateInternalServerError(ex.ToString()));
                }
            }
            else
            {
                try
                {
                    await _loansRepo.AddAsync(CreateLoan(request));
                }
                catch (Exception ex)
                {
                    responseModel.AddError(ExceptionCreator.CreateInternalServerError(ex.ToString()));
                }
            }

            return(responseModel);
        }
Пример #26
0
        /// <summary>
        /// Retrieve loam by id
        /// </summary>
        /// <param name="id"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task <ApiResponse <LoanResponse> > GetLoanByIdsync(int id, CancellationToken cancellationToken = default)
        {
            var responseModel = new ApiResponse <LoanResponse>();

            var loan = await _loansRepo.FindByIdAsync(id);


            if (loan == null)
            {
                responseModel.AddError(ExceptionCreator.CreateNotFoundError(nameof(loan), $"loan Id: {id} not found"));
                return(responseModel);
            }

            responseModel.Data = CreateLoanResponse(loan);

            return(responseModel);
        }
Пример #27
0
        /// <summary>
        /// Retrieve city for the specified id
        /// </summary>
        /// <param name="cityId"></param>
        /// <param name="includeDistricts"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task <ApiResponse <CityResponse> > GetCityByIdAsync(int cityId, bool includeDistricts = true, CancellationToken cancellationToken = default)
        {
            var responseModel = new ApiResponse <CityResponse>();

            var city = await _citiesRepo.FindByIdAsync(cityId);

            if (city == null)
            {
                responseModel.AddError(ExceptionCreator.CreateNotFoundError(nameof(city), $"city of id {cityId}: not found"));

                return(responseModel);
            }

            responseModel.Data = CreateCityResponse(city);

            return(responseModel);
        }
Пример #28
0
        /// <summary>
        /// Activate credit card
        /// </summary>
        /// <param name="creditCardId"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task <Response> ActivateCreditCardAsync(int creditCardId, CancellationToken cancellationToken = default)
        {
            var responseModel = new ApiResponse <CreditCardResponse>();

            var creditCard = await _creditCardsRepo.FindByIdAsync(creditCardId);

            if (creditCard != null)
            {
                creditCard.Disabled = false;
            }
            else
            {
                responseModel.AddError(ExceptionCreator.CreateNotFoundError(nameof(creditCard), $"credit card id: {creditCard} not found"));
            }

            return(responseModel);
        }
Пример #29
0
        /// <summary>
        /// Retrieve credit card by id
        /// </summary>
        /// <param name="id"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task <ApiResponse <CreditCardResponse> > GetCreditCardByIdAsync(int creditCardId, CancellationToken cancellationToken = default)
        {
            var responseModel = new ApiResponse <CreditCardResponse>();

            var creditCard = await _creditCardsRepo.FindByIdAsync(creditCardId);

            if (creditCard == null)
            {
                responseModel.AddError(ExceptionCreator.CreateNotFoundError(nameof(creditCard), $"credit card of id {creditCardId}: not found"));
                return(responseModel);
            }


            responseModel.Data = CreateCreditCardResponse(creditCard);

            return(responseModel);
        }
Пример #30
0
        /// <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);
        }