/// <summary> /// Gather all products from catalog from database. /// </summary> /// <returns>All products from catalog from database. Each product contains more information than ProductCatalog entity.</returns> public async Task <IEnumerable <ProductsCatalogModel> > GetAllDataModelAsync() { // gather products catalog, to have unblocking call when grouping var productCatalogs = await db.ProductCatalog.ToListAsync(); var dataModels = (from pc in productCatalogs join gA in (from pc in productCatalogs join p in db.Product on pc.Id equals p.Uid into t group t by pc.Id).Select(x => new { Id = x.Key, Count = x.Single().Count() }) on pc.Id equals gA.Id into groupD from d in groupD.DefaultIfEmpty() join gB in (from pc in productCatalogs join r in db.RecipeProductChanges on pc.Id equals r.ProductId into t group t by pc.Id).Select(x => new { Id = x.Key, Count = x.Single().Count() }) on pc.Id equals gB.Id into groupE from e in groupE.DefaultIfEmpty() select new ProductsCatalogModel { ProductId = pc.Id, Name = pc.Name, CAS = pc.Cas, LicenseRequired = pc.LicenseRequired ?? false, StoragesCount = d.Count, RecipesCount = e.Count }).ToList(); // for each product connect license from licenses list and // based on validation set IsValid property foreach (var item in dataModels) { if (item.LicenseRequired == true) { // set default to false item.IsValid = false; foreach (var license in db.License.Where(i => i.ProductId == item.ProductId)) { // if valid license for current proudct if found, set IsValid to true if (licenseValidation.IsValid(license)) { item.IsValid = true; break; } } } else { // if license isn't required, consider product valid item.IsValid = true; } } return(dataModels); }
/// <summary> /// Convert LicensesDataModel to LicensesViewModel /// </summary> /// <param name="licenseDataModel">Data model to be converted</param> /// <returns>Converted LicensesViewModel instance</returns> private LicensesViewModel LicensesDataModelToViewModel(LicensesDataModel licenseDataModel) { return(new LicensesViewModel { Id = licenseDataModel.Id, GovermentNum = licenseDataModel.GovermentNum, Date = licenseDataModel.Date.ToShortDateString(), ProductId = licenseDataModel.ProductId, ProductName = licenseDataModel.ProductName, ProductCAS = licenseDataModel.ProductCAS, IsValid = licenseValidation.IsValid(new License { Date = licenseDataModel.Date }) }); }
/// <summary> /// Gather single facility, which found by <c>key</c>. /// </summary> /// <param name="key">Id of facility to be found</param> /// <returns>Single facility, which found by <c>key</c>. Contains more information than Facility entity.</returns> public async Task <FacilityModel> GetSingleDataModelAsync(object key) { IEnumerable <FacilityModel> queryOuter; // gather facilities, to have unblocking call when grouping var facilities = await db.Facility.ToListAsync(); // gather licenses, to have validation information on each license var licensesList = await db.License.ToListAsync(); if (await db.TechProcess.Where(i => i.FacilityId == (int)key).AnyAsync()) { // gather basic data required for display facility var queryInner = (from f in facilities where f.Id == (int)key join tp in db.TechProcess on f.Id equals tp.FacilityId into techprocesses from tps in techprocesses.DefaultIfEmpty() join r in db.Recipe on tps.RecipeId equals r.Id into recipes from rs in recipes.DefaultIfEmpty() join rpc in db.RecipeProductChanges on rs.Id equals rpc.RecipeId into recipeproducts from rpcs in recipeproducts.DefaultIfEmpty() join pc in db.ProductCatalog on rpcs.ProductId equals pc.Id into products from pcs in products.DefaultIfEmpty() group new FacilitiesProductContainer() { FacilityId = f.Id, RecipeId = tps.RecipeId, RecipeEfficiency = tps.DayEfficiency, RecipeGovApprov = rs.GovermentApproval ?? false, RecipeTechnoApprov = rs.TechApproval ?? false, ProductId = rpcs.ProductId, ProductType = rpcs.Type, Quantity = rpcs.Quantity, ProductName = pcs.Name, LicenseRequired = pcs.LicenseRequired ?? false } by f.Id).ToList(); // for each product connect license from previously loaded list and // based on validation set IsValid property foreach (var group in queryInner) { foreach (var item in group) { if (item.LicenseRequired) { // set default to false item.IsValid = false; var licenses = licensesList.Where(i => i.ProductId == item.ProductId); // if valid license for current proudct if found, set IsValid to true foreach (var license in licenses) { if (licenseValidation.IsValid(license)) { item.IsValid = true; break; } } } else { // if license isn't required, consider product valid item.IsValid = true; } } } // group gathered data into final model queryOuter = from f in facilities where f.Id == (int)key join qi in queryInner on f.Id equals qi.Key select new FacilityModel { Id = f.Id, Latitude = f.Latitude, Longitude = f.Longitude, Name = f.Name, Products = qi.ToList() }; } else { queryOuter = from f in facilities where f.Id == (int)key select new FacilityModel { Id = f.Id, Latitude = f.Latitude, Longitude = f.Longitude, Name = f.Name, Products = new List <FacilitiesProductContainer>() }; } return(queryOuter.Single()); }