private void UpdateFields(CentralBankExchangeRate entity, PortalCentralBankExchangeRateDetailsForm form, bool isNew)
        {
            if (isNew)
            {
                entity.LocalCurrencyCode   = form.LocalCurrencyCode;
                entity.ForeignCurrencyCode = form.ForeignCurrencyCode;
            }

            entity.DefaultOrder = form.DefaultOrder.GetMandatoryValue();
        }
        public override async Task <CommonDetailsPostResponse> ExecuteAsync(PortalCentralBankExchangeRateDetailsPostRequest request)
        {
            var isNew = request.Form.Id == null;
            CentralBankExchangeRate entity;

            if (isNew)
            {
                // check if duplicate currency pair
                var isDuplicate = await _dbContext.CentralBankExchangeRates
                                  .Where(x => x.LocalCurrencyCode == request.Form.LocalCurrencyCode &&
                                         x.ForeignCurrencyCode == request.Form.ForeignCurrencyCode)
                                  .AnyAsync();

                if (isDuplicate)
                {
                    throw new InvalidOperationException("Central bank rate for such currency pair already exists.");
                }

                entity = new CentralBankExchangeRate();
                _dbContext.CentralBankExchangeRates.Add(entity);
            }
            else
            {
                var entityId = request.Form.Id.Value;
                entity = await _dbContext.CentralBankExchangeRates
                         .Where(x => x.Id == entityId)
                         .FirstOrDefaultAsync();

                if (entity == null)
                {
                    throw EntityNotFoundException.Create <CentralBankExchangeRate>(entityId);
                }
            }

            UpdateFields(entity, request.Form, isNew);

            await _dbContext.SaveChangesAsync();

            return(new CommonDetailsPostResponse()
            {
                Id = entity.Id,
            });
        }