public void Mapping_AssetToAssetViewModel_Maps() { // Arrange Mapper.CreateMap<Asset, AssetViewModel>(); Asset a = new Asset { AssetId = 2, CategoryId = 2, LocationDescription = "Room", ObsolescenseDate = null }; a.CreatedDate = new DateTime(2014, 7, 31); a.CategoryId = 1; var avm = Mapper.DynamicMap<Asset, AssetViewModel>(a); // Assert Mapper.AssertConfigurationIsValid(); }
private void ClearLookups(Asset asset) { asset.AssetId = 0; // Asset Make if(asset.AssetMakeId != 0) { asset.AssetMake = null; } else if (asset.AssetMakeId == 0 && asset.AssetMake != null && asset.AssetMake.LookupId != 0) { asset.AssetMakeId = asset.AssetMake.LookupId; asset.AssetMake = null; } // Category if (asset.CategoryId != 0) { asset.Category = null; } else if (asset.CategoryId == 0 && asset.Category != null && asset.Category.LookupId != 0) { asset.CategoryId = asset.Category.LookupId; asset.Category = null; } // Warranty Period if (asset.WarrantyPeriodId != 0) { asset.WarrantyPeriod = null; } else if(asset.WarrantyPeriodId == 0 && asset.WarrantyPeriod != null && asset.WarrantyPeriod.LookupId != 0) { asset.WarrantyPeriodId = asset.WarrantyPeriod.LookupId; asset.WarrantyPeriod = null; } }
private Asset LoadLookupsForAsset(Asset asset, List<Lookup> lookups) { if(asset.AssetMakeId == 0) { Lookup existingLookup = lookups.FirstOrDefault( x => x.Type.Description == EnumHelper.LookupTypes.Make.ToString() && x.Description == asset.AssetMake.Description); asset.AssetMakeId = existingLookup == null ? 0 : existingLookup.LookupId; if (existingLookup == null) { lookups.Add(asset.AssetMake); } else { asset.AssetMake = existingLookup; } } if (asset.CategoryId == 0) { Lookup existingLookup = lookups.FirstOrDefault( x => x.Type.Description == EnumHelper.LookupTypes.Category.ToString() && x.Description == asset.Category.Description); asset.CategoryId = existingLookup == null ? 0 : existingLookup.LookupId; if (existingLookup == null) { lookups.Add(asset.Category); } else { asset.Category = existingLookup; } } if (asset.WarrantyPeriodId == 0) { Lookup existingLookup = lookups.FirstOrDefault( x => x.Type.Description == EnumHelper.LookupTypes.WarrantyPeriod.ToString() && x.Description == asset.WarrantyPeriod.Description); asset.WarrantyPeriodId = existingLookup == null ? 0 : existingLookup.LookupId; if (existingLookup == null) { lookups.Add(asset.WarrantyPeriod); } else { asset.WarrantyPeriod = existingLookup; } } return asset; }
/// <summary> /// Create and asset object that can be saved to the database for a single row in the csv file /// </summary> /// <param name="header"></param> /// <param name="line"></param> /// <returns></returns> public ConvertedAsset ProcessLineToAsset(string[] header, string line, int tenantId, int rowId) { ConvertedAsset convertedAsset = new ConvertedAsset(); convertedAsset.Errors = new List<string>(); string[] linesArray = line.Split(','); Asset asset = new Asset(); for (int i = 0; i < linesArray.Length; i++) { asset.AssetId = rowId; switch (header[i].ToLower()) { case "model": asset.Model = linesArray[i]; break; case "serialnumber": asset.SerialNumber = linesArray[i]; break; case "purchasedate": DateTime purchaseDate; if (DateTime.TryParse(linesArray[i], out purchaseDate)) { asset.PurchaseDate = purchaseDate; } else { convertedAsset.Errors.Add(string.Format(HIResources.Strings.ImportError_PurchaseDate, rowId, linesArray[i])); } break; case "warrantyperiod": string warrantyPeriodDescription = linesArray[i]; Lookup warrantyPeriod = LookupRepository.Single( x => x.Type.Description == EnumHelper.LookupTypes.WarrantyPeriod.ToString() && x.Description.Equals(warrantyPeriodDescription, StringComparison.InvariantCultureIgnoreCase)); if (warrantyPeriod == null) { LookupType type = LookupTypesRepository.Single( x => x.Description == EnumHelper.LookupTypes.WarrantyPeriod.ToString()); warrantyPeriod = new Lookup { Description = warrantyPeriodDescription, LookupTypeId = type.LookupTypeId, TenantId = tenantId, Type = type }; } asset.WarrantyPeriod = warrantyPeriod; break; case "obsolescencedate": DateTime obsolescenseDate; if (DateTime.TryParse(linesArray[i], out obsolescenseDate)) { asset.ObsolescenseDate = obsolescenseDate; } else { convertedAsset.Errors.Add(string.Format(HIResources.Strings.ImportError_ObsolescenseDate, rowId, linesArray[i])); } break; case "pricepaid": decimal pricePaid; if(decimal.TryParse(linesArray[i], out pricePaid)) { asset.PricePaid = pricePaid; } else { convertedAsset.Errors.Add(string.Format(HIResources.Strings.ImportError_PricePaid, rowId, linesArray[i])); } break; case "assetmake": string assetMakeDescription = linesArray[i]; Lookup assetMake = LookupRepository.Single( x=> x.Type.Description == EnumHelper.LookupTypes.Make.ToString() && x.Description.Equals(assetMakeDescription, StringComparison.InvariantCultureIgnoreCase)); if (assetMake == null) { LookupType type = LookupTypesRepository.Single( x => x.Description == EnumHelper.LookupTypes.Make.ToString()); assetMake = new Lookup { Description = assetMakeDescription, LookupTypeId = type.LookupTypeId, TenantId = tenantId, Type = type }; } asset.AssetMake = assetMake; break; case "category": string categoryDescription = linesArray[i]; Lookup category = LookupRepository.Single( x => x.Type.Description == EnumHelper.LookupTypes.Category.ToString() && x.Description.Equals(categoryDescription, StringComparison.InvariantCultureIgnoreCase)); if (category == null) { LookupType type = LookupTypesRepository.Single( x => x.Description == EnumHelper.LookupTypes.Category.ToString()); category = new Lookup { Description = categoryDescription, LookupTypeId = type.LookupTypeId, TenantId = tenantId, Type = type }; } asset.Category = category; break; case "locationdescription": asset.LocationDescription = linesArray[i]; break; default: break; } } if(string.IsNullOrWhiteSpace(asset.Model)) { convertedAsset.Errors.Add(string.Format(HIResources.Strings.ImportError_ModelMissing, asset.AssetId)); } if(string.IsNullOrWhiteSpace(asset.Model)) { convertedAsset.Errors.Add(string.Format(HIResources.Strings.ImportError_SerialNumberMissing, asset.AssetId)); } convertedAsset.Asset = asset; return convertedAsset; }