public async Task <OperationDataResult <TrashInspectionBaseSettings> > GetSettings() { try { var option = _options.Value; if (option.Token == "...") { const string chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; var random = new Random(); var result = new string(chars.Select(c => chars[random.Next(chars.Length)]).Take(32).ToArray()); await _options.UpdateDb(settings => { settings.Token = result; }, _dbContext, _userService.UserId); } if (option.SdkConnectionString == "...") { var connectionString = _dbContext.Database.GetDbConnection().ConnectionString; var dbNameSection = Regex.Match(connectionString, @"(Database=(...)_eform-angular-\w*-plugin;)").Groups[0].Value; var dbPrefix = Regex.Match(connectionString, @"Database=(\d*)_").Groups[1].Value; var sdk = $"Database={dbPrefix}_SDK;"; connectionString = connectionString.Replace(dbNameSection, sdk); await _options.UpdateDb(settings => { settings.SdkConnectionString = connectionString; }, _dbContext, _userService.UserId); } return(new OperationDataResult <TrashInspectionBaseSettings>(true, option)); } catch (Exception e) { Trace.TraceError(e.Message); _logger.LogError(e.Message); return(new OperationDataResult <TrashInspectionBaseSettings>(false, _trashInspectionLocalizationService.GetString("ErrorWhileObtainingTrashInspectionSettings"))); } }
public async Task <OperationDataResult <SegmentsModel> > Index(SegmentRequestModel pnRequestModel) { try { SegmentsModel segmentsModel = new SegmentsModel(); IQueryable <Segment> segmentQuery = _dbContext.Segments.AsQueryable(); if (!string.IsNullOrEmpty(pnRequestModel.Sort)) { if (pnRequestModel.IsSortDsc) { segmentQuery = segmentQuery .CustomOrderByDescending(pnRequestModel.Sort); } else { segmentQuery = segmentQuery .CustomOrderBy(pnRequestModel.Sort); } } else { segmentQuery = _dbContext.Segments .OrderBy(x => x.Id); } segmentQuery = segmentQuery .Where(x => x.WorkflowState != Constants.WorkflowStates.Removed) .Skip(pnRequestModel.Offset) .Take(pnRequestModel.PageSize); List <SegmentModel> segmentModels = await segmentQuery.Select(x => new SegmentModel { Id = x.Id, Name = x.Name, Description = x.Description, SdkFolderId = x.SdkFolderId }).ToListAsync(); segmentsModel.Total = await _dbContext.Segments.CountAsync(x => x.WorkflowState != Constants.WorkflowStates.Removed); segmentsModel.SegmentList = segmentModels; Core _core = await _coreHelper.GetCore(); return(new OperationDataResult <SegmentsModel>(true, segmentsModel)); } catch (Exception e) { Trace.TraceError(e.Message); _coreHelper.LogException(e.Message); return(new OperationDataResult <SegmentsModel>(false, _trashInspectionLocalizationService.GetString("ErrorObtainingSegments"))); } }
public async Task <OperationDataResult <InstallationsModel> > Index(InstallationRequestModel pnRequestModel) { try { var installationsModel = new InstallationsModel(); var installationsQuery = _dbContext.Installations .Where(x => x.WorkflowState != Constants.WorkflowStates.Removed) .AsQueryable(); if (!pnRequestModel.NameFilter.IsNullOrEmpty() && pnRequestModel.NameFilter != "") { installationsQuery = installationsQuery.Where(x => x.Name.Contains(pnRequestModel.NameFilter)); } QueryHelper.AddSortToQuery(installationsQuery, pnRequestModel.Sort, pnRequestModel.IsSortDsc); installationsModel.Total = installationsQuery.Count(); installationsQuery = installationsQuery .Skip(pnRequestModel.Offset) .Take(pnRequestModel.PageSize); var installations = await installationsQuery.Select(x => new InstallationModel { Id = x.Id, Name = x.Name }).ToListAsync(); installationsModel.InstallationList = installations; return(new OperationDataResult <InstallationsModel>(true, installationsModel)); } catch (Exception e) { Trace.TraceError(e.Message); _coreHelper.LogException(e.Message); return(new OperationDataResult <InstallationsModel>(false, _trashInspectionLocalizationService.GetString("ErrorObtainingInstallations"))); } }
public async Task <OperationDataResult <Paged <TrashInspectionModel> > > Index(TrashInspectionRequestModel pnRequestModel) { try { var trashInspectionSettings = _options.Value; var trashInspectionsQuery = _dbContext.TrashInspections .Where(x => x.WorkflowState != Constants.WorkflowStates.Removed) .AsNoTracking() .AsQueryable(); if (!string.IsNullOrEmpty(pnRequestModel.NameFilter)) { if (int.TryParse(pnRequestModel.NameFilter, out var i)) { trashInspectionsQuery = trashInspectionsQuery.Where(x => x.Id == i); } else { if (DateTime.TryParse(pnRequestModel.NameFilter, out var dateTime)) { trashInspectionsQuery = trashInspectionsQuery.Where(x => x.Date.Year == dateTime.Year && x.Date.Month == dateTime.Month && x.Date.Day == dateTime.Day); } else { trashInspectionsQuery = trashInspectionsQuery.Where(x => x.Comment.Contains(pnRequestModel.NameFilter) || x.WeighingNumber.Contains(pnRequestModel.NameFilter)); } } } trashInspectionsQuery = QueryHelper.AddSortToQuery(trashInspectionsQuery, pnRequestModel.Sort, pnRequestModel.IsSortDsc); var total = await trashInspectionsQuery.Select(x => x.Id).CountAsync(); trashInspectionsQuery = trashInspectionsQuery .Skip(pnRequestModel.Offset) .Take(pnRequestModel.PageSize); var timeZoneInfo = await _userService.GetCurrentUserTimeZoneInfo(); var trashInspections = await AddSelectToQuery(trashInspectionsQuery, timeZoneInfo, trashInspectionSettings.Token) .ToListAsync(); var core = await _coreHelper.GetCore(); var eFormIds = new List <KeyValuePair <int, int> >(); // <FractionId, eFormId> foreach (var trashInspectionModel in trashInspections) { var fractionEFormId = await _dbContext.Fractions .Where(y => y.Id == trashInspectionModel.FractionId) .Select(x => x.eFormId) .FirstOrDefaultAsync(); if (trashInspectionModel.Status == 100) { var result = await _dbContext.TrashInspectionCases .Where(x => x.TrashInspectionId == trashInspectionModel.Id) .Where(x => x.Status == 100) .ToListAsync(); if (result.Any()) { trashInspectionModel.SdkCaseId = int.Parse(result.First().SdkCaseId); trashInspectionModel.SdkCaseId = (int)core.CaseLookupMUId(trashInspectionModel.SdkCaseId).Result.CaseId; } if (eFormIds.Any(x => x.Key == trashInspectionModel.FractionId)) { trashInspectionModel.SdkeFormId = eFormIds.First(x => x.Key == trashInspectionModel.FractionId).Value; } else { try { var locale = await _userService.GetCurrentUserLocale(); var language = core.DbContextHelper.GetDbContext(). Languages .Single(x => x.LanguageCode == locale); var eForm = await core.TemplateItemRead(fractionEFormId, language); trashInspectionModel.SdkeFormId = eForm.Id; var kvp = new KeyValuePair <int, int>(fractionEFormId, eForm.Id); eFormIds.Add(kvp); } catch { // KeyValuePair<int, int> kvp = new KeyValuePair<int, int>(fraction.eFormId, ""); // eFormIds.Add(kvp); } } } } var trashInspectionsModel = new Paged <TrashInspectionModel> { Total = total, Entities = trashInspections }; return(new OperationDataResult <Paged <TrashInspectionModel> >(true, trashInspectionsModel)); } catch (Exception e) { Trace.TraceError(e.Message); _logger.LogError(e.Message); return(new OperationDataResult <Paged <TrashInspectionModel> >(false, _trashInspectionLocalizationService.GetString("ErrorObtainingTrashInspections"))); } }
public async Task <OperationDataResult <Paged <FractionModel> > > Index(FractionRequestModel pnRequestModel) { try { var fractionsModel = new Paged <FractionModel>(); var fractionsQuery = _dbContext.Fractions .Where(x => x.WorkflowState != Constants.WorkflowStates.Removed) .AsQueryable(); //if (!pnRequestModel.NameFilter.IsNullOrEmpty() && pnRequestModel.NameFilter != "") //{ // fractionsQuery = fractionsQuery.Where(x => // x.Name.Contains(pnRequestModel.NameFilter) || // x.Description.Contains(pnRequestModel.NameFilter)); //} fractionsQuery = QueryHelper.AddSortToQuery(fractionsQuery, pnRequestModel.Sort, pnRequestModel.IsSortDsc); fractionsModel.Total = await fractionsQuery.Select(x => x.Id).CountAsync(); fractionsQuery = fractionsQuery .Skip(pnRequestModel.Offset) .Take(pnRequestModel.PageSize); var fractions = await fractionsQuery .Select(x => new FractionModel { Id = x.Id, Name = x.Name, eFormId = x.eFormId, Description = x.Description, LocationCode = x.LocationCode, ItemNumber = x.ItemNumber }) .ToListAsync(); fractionsModel.Entities = fractions; var core = await _coreHelper.GetCore(); var eFormNames = new List <KeyValuePair <int, string> >(); var locale = await _userService.GetCurrentUserLocale(); var language = core.DbContextHelper.GetDbContext().Languages.Single(x => x.LanguageCode == locale); foreach (var fractionModel in fractionsModel.Entities) { if (fractionModel.eFormId > 0) { if (eFormNames.Any(x => x.Key == fractionModel.eFormId)) { fractionModel.SelectedTemplateName = eFormNames.First(x => x.Key == fractionModel.eFormId).Value; } else { try { var eFormName = core.TemplateItemRead(fractionModel.eFormId, language).Result.Label; fractionModel.SelectedTemplateName = eFormName; var kvp = new KeyValuePair <int, string>(fractionModel.eFormId, eFormName); eFormNames.Add(kvp); } catch { var kvp = new KeyValuePair <int, string>(fractionModel.eFormId, ""); eFormNames.Add(kvp); } } } } return(new OperationDataResult <Paged <FractionModel> >(true, fractionsModel)); } catch (Exception e) { Trace.TraceError(e.Message); _coreHelper.LogException(e.Message); return(new OperationDataResult <Paged <FractionModel> >(false, _trashInspectionLocalizationService.GetString("ErrorObtainingFractions"))); } }
public async Task <OperationDataResult <TransportersModel> > Index(TransporterRequestModel pnRequestModel) { try { var transportersModel = new TransportersModel(); var transporterQuery = _dbContext.Transporters.AsQueryable(); if (!pnRequestModel.NameFilter.IsNullOrEmpty() && pnRequestModel.NameFilter != "") { transporterQuery = transporterQuery.Where(x => x.Name.Contains(pnRequestModel.NameFilter) || x.Description.Contains(pnRequestModel.NameFilter)); } if (!string.IsNullOrEmpty(pnRequestModel.Sort)) { if (pnRequestModel.IsSortDsc) { transporterQuery = transporterQuery .CustomOrderByDescending(pnRequestModel.Sort); } else { transporterQuery = transporterQuery .CustomOrderBy(pnRequestModel.Sort); } } else { transporterQuery = _dbContext.Transporters .OrderBy(x => x.Id); } transporterQuery = transporterQuery .Where(x => x.WorkflowState != Constants.WorkflowStates.Removed) .Skip(pnRequestModel.Offset) .Take(pnRequestModel.PageSize); var transporters = await transporterQuery.Select(x => new TransporterModel { Id = x.Id, Name = x.Name, Description = x.Description, ForeignId = x.ForeignId, Address = x.Address, City = x.City, ZipCode = x.ZipCode, Phone = x.Phone, ContactPerson = x.ContactPerson }).ToListAsync(); transportersModel.Total = _dbContext.Transporters.Count(x => x.WorkflowState != Constants.WorkflowStates.Removed); transportersModel.TransporterList = transporters; return(new OperationDataResult <TransportersModel>(true, transportersModel)); } catch (Exception e) { Trace.TraceError(e.Message); _coreHelper.LogException(e.Message); return(new OperationDataResult <TransportersModel>(false, _trashInspectionLocalizationService.GetString("ErrorObtainingTransporters"))); } }