public async Task <OperationDataResult <ContractInspectionsModel> > Index(ContractInspectionsRequestModel contractInspectionsPnRequestModel)
        {
            try
            {
                ContractInspectionsModel contractInspectionsModel = new ContractInspectionsModel();
                Core _core = await _coreHelper.GetCore();

                IQueryable <ContractInspection> contractInspectionsQuery = _dbContext.ContractInspection.
                                                                           Where(x => x.WorkflowState != Constants.WorkflowStates.Removed).AsQueryable();
                if (!string.IsNullOrEmpty(contractInspectionsPnRequestModel.SortColumnName) &&
                    contractInspectionsPnRequestModel.SortColumnName != "")
                {
                    if (contractInspectionsPnRequestModel.IsSortDsc)
                    {
                        contractInspectionsQuery = contractInspectionsQuery.CustomOrderByDescending(contractInspectionsPnRequestModel.SortColumnName);
                    }
                    else
                    {
                        contractInspectionsQuery = contractInspectionsQuery.CustomOrderBy(contractInspectionsPnRequestModel.SortColumnName);
                    }
                }
                contractInspectionsModel.Total = await contractInspectionsQuery.CountAsync(x => x.WorkflowState != Constants.WorkflowStates.Removed);

                contractInspectionsQuery
                    = contractInspectionsQuery
                      .Where(x => x.WorkflowState != Constants.WorkflowStates.Removed)
                      .Skip(contractInspectionsPnRequestModel.Offset)
                      .Take(contractInspectionsPnRequestModel.PageSize);
                List <ContractInspection> contractInspections = await contractInspectionsQuery.ToListAsync();

                contractInspections.ForEach(contractInspection =>
                {
                    ContractInspectionItem contractInspectionItem =
                        _dbContext.ContractInspectionItem.FirstOrDefault(x =>
                                                                         x.ContractInspectionId == contractInspection.Id);

                    Contract contract = _dbContext.Contract.Single(x => x.Id == contractInspection.ContractId);

                    Customer customer =
                        _customerDbContext.Customers.Single(x => x.Id == contract.CustomerId);
                    RentableItemCustomerModel rentableItemCustomerModel = new RentableItemCustomerModel
                    {
                        Id              = customer.Id,
                        CustomerNo      = customer.CustomerNo,
                        CompanyName     = customer.CompanyName,
                        ContactPerson   = customer.ContactPerson,
                        CompanyAddress  = customer.CompanyAddress,
                        CompanyAddress2 = customer.CompanyAddress2,
                        CityName        = customer.CityName,
                        ZipCode         = customer.ZipCode,
                        CountryCode     = customer.CountryCode,
                        EanCode         = customer.EanCode,
                        VatNumber       = customer.VatNumber,
                        Email           = customer.Email,
                        Phone           = customer.Phone,
                        Description     = customer.Description
                    };
                    List <RentableItemModel> rentableItemModels = new List <RentableItemModel>();
                    foreach (ContractRentableItem contractRentableItem in _dbContext.ContractRentableItem.Where(x =>
                                                                                                                x.ContractId == contract.Id && x.WorkflowState == Constants.WorkflowStates.Created).ToList())
                    {
                        RentableItem rentableItem           = _dbContext.RentableItem.Single(x => x.Id == contractRentableItem.RentableItemId);
                        RentableItemModel rentableItemModel = new RentableItemModel
                        {
                            Id               = rentableItem.Id,
                            Brand            = rentableItem.Brand,
                            ModelName        = rentableItem.ModelName,
                            PlateNumber      = rentableItem.PlateNumber,
                            VinNumber        = rentableItem.VinNumber,
                            SerialNumber     = rentableItem.SerialNumber,
                            RegistrationDate = rentableItem.RegistrationDate,
                            EFormId          = rentableItem.eFormId
                        };
                        rentableItemModels.Add(rentableItemModel);
                    }

                    using (var dbContext = _core.DbContextHelper.GetDbContext())
                    {
                        if (contractInspectionItem != null)
                        {
                            contractInspectionsModel.ContractInspections.Add(new ContractInspectionModel
                            {
                                SdkCaseApiId = contractInspectionItem.SDKCaseId,
                                SdkCaseId    = dbContext.Cases
                                               .Single(x => x.MicrotingUid == contractInspectionItem.SDKCaseId).Id,
                                eFormId              = rentableItemModels.First().EFormId,
                                ContractId           = contractInspection.ContractId,
                                ContractStart        = contract.ContractStart,
                                ContractEnd          = contract.ContractEnd,
                                DoneAt               = contractInspection.DoneAt,
                                Id                   = contractInspection.Id,
                                Status               = contractInspectionItem.Status,
                                RentableItemCustomer = rentableItemCustomerModel,
                                RentableItems        = rentableItemModels
                            });
                        }
                    }
                });
                return(new OperationDataResult <ContractInspectionsModel>(true, contractInspectionsModel));
            }
            catch (Exception e)
            {
                Trace.TraceError(e.Message);
                _logger.LogError(e.Message);
                return(new OperationDataResult <ContractInspectionsModel>(false,
                                                                          _rentableItemsLocalizationService.GetString("ErrorObtainingContractInspectionInfo")));
            }
        }
 public Task <OperationDataResult <ContractInspectionsModel> > Index(ContractInspectionsRequestModel requestModel)
 {
     return(_contractsInspectionService.Index(requestModel));
 }