/// <summary> /// Generate a query for the specified 'filter'. /// </summary> /// <param name="context"></param> /// <param name="filter"></param> /// <returns></returns> public static IQueryable <Entity.PimsContactMgrVw> GenerateContactQuery(this PimsContext context, Entity.Models.ContactFilter filter) { filter.ThrowIfNull(nameof(filter)); IQueryable <PimsContactMgrVw> query = context.PimsContactMgrVws.AsNoTracking(); query = query.GenerateCommonContactQuery(filter); return(query); }
/// <summary> /// Create a new List with new instances of ParcelFiscal. /// </summary> /// <param name="context"></param> /// <param name="parcel"></param> /// <param name="startFiscalYear"></param> /// <param name="count"></param> /// <param name="key"></param> /// <param name="value"></param> /// <returns></returns> public static List <Entity.ParcelFiscal> CreateFiscals(this PimsContext context, Entity.Parcel parcel, int startFiscalYear, int count, Entity.FiscalKeys key = Entity.FiscalKeys.Estimated, decimal value = 1) { var evaluations = new List <Entity.ParcelFiscal>(count); for (var i = startFiscalYear; i < (startFiscalYear + count); i++) { evaluations.Add(context.CreateFiscal(parcel, i, key, value)); } return(evaluations); }
/// <summary> /// Create a new List with new instances of NotificationTemplates. /// </summary> /// <param name="context"></param> /// <param name="startId"></param> /// <param name="count"></param> /// <param name="agency"></param> /// <param name="status"></param> /// <returns></returns> public static List <Entity.NotificationTemplate> CreateNotificationTemplates(this PimsContext context, int startId, int count, string name = "test") { var templates = new List <Entity.NotificationTemplate>(count); for (var i = startId; i < (startId + count); i++) { templates.Add(context.CreateNotificationTemplate(i, $"{name}-{i}")); } return(templates); }
/// <summary> /// Generate a query for the specified 'filter'. /// </summary> /// <param name="context"></param> /// <param name="filter"></param> /// <returns></returns> public static IQueryable <Entities.PimsResearchFile> GenerateResearchQuery(this PimsContext context, Entity.Models.ResearchFilter filter) { filter.ThrowIfNull(nameof(filter)); var query = context.PimsResearchFiles.AsNoTracking(); query = query.GenerateCommonResearchQuery(filter); return(query); }
/// <summary> /// Create a new List with new instances of Project Snapshots. /// </summary> /// <param name="context"></param> /// <param name="startId"></param> /// <param name="projectId"></param> /// <param name="snapshotOn"></param> /// <returns></returns> public static List <Entity.ProjectSnapshot> CreateProjectSnapshots(this PimsContext context, int startId, int count, int projectId = 0, DateTime?snapshotOn = null) { var projectSnapshots = new List <Entity.ProjectSnapshot>(count); for (var i = startId; i < (startId + count); i++) { projectSnapshots.Add(context.CreateProjectSnapshot(i, projectId, snapshotOn)); } return(projectSnapshots); }
/// <summary> /// Create a new instance of an Address. /// </summary> /// <param name="id"></param> /// <param name="address1"></param> /// <param name="address2"></param> /// <param name="postal"></param> /// <returns></returns> public static Entity.Address CreateAddress(this PimsContext context, int id, string address1, string address2, string postal) { var province = context.Provinces.Find("BC") ?? EntityHelper.CreateProvince("BC", "British Columbia"); return(new Entity.Address(address1, address2, "Victoria", province, postal) { Id = id, RowVersion = new byte[] { 12, 13, 14 } }); }
/// <summary> /// Disposes parent parcels of subdivisions by setting the classification to subdivided /// </summary> /// <param name="context"></param> /// <param name="parentParcels"></param> public static void DisposeSubdivisionParentParcels(this PimsContext context, IEnumerable <Parcel> parentParcels) { var subdivided = context.PropertyClassifications.AsNoTracking().FirstOrDefault(c => c.Name == "Subdivided") ?? throw new KeyNotFoundException("Classification 'Subdivided' not found."); parentParcels.ForEach(parentParcel => { parentParcel.ClassificationId = subdivided.Id; context.Update(parentParcel); }); }
/// <summary> /// Make a query to determine if the building names are unique. /// - No two buildings should have the same name on a parcel. /// </summary> /// <param name="context"></param> /// <param name="parcel"></param> /// <param name="building"></param> /// <exception type="DbUpdateException">The name within a parcel should be unique.</exception> public static void ThrowIfNotUnique(this PimsContext context, Entity.Parcel parcel, Entity.Building building) { var parcelBuildings = context.Parcels.Where(p => p.Id == parcel.Id).SelectMany(p => p.Buildings.Select(b => b.Building.Name)).Distinct().ToArray(); var alreadyExists = parcelBuildings.Contains(building.Name); if (alreadyExists) { throw new DbUpdateException("A building name must be unique on the parcel."); } }
/// <summary> /// Creates an instance of a service of the specified 'T' type and initializes it with the specified 'user'. /// Will use any 'args' passed in instead of generating defaults. /// Once you create a service you can no longer add to the services collection. /// </summary> /// <param name="helper"></param> /// <param name="context"></param> /// <param name="args"></param> /// <typeparam name="T"></typeparam> /// <returns></returns> public static T CreateRepository <T>(this TestHelper helper, PimsContext context, params object[] args) where T : IRepository { helper.MockConstructorArguments <T>(args); helper.AddSingleton(context); helper.BuildServiceProvider(); var service = helper.CreateInstance <T>(); return(service); }
/// <summary> /// Create a new List with new instances of Project Reports. /// </summary> /// <param name="context"></param> /// <param name="startId"></param> /// <param name="count"></param> /// <returns></returns> public static List <Entity.ProjectReport> CreateProjectReports(this PimsContext context, int startId, int count) { var projectReports = new List <Entity.ProjectReport>(count); for (var i = startId; i < (startId + count); i++) { projectReports.Add(context.CreateProjectReport(i, "projectReport" + i)); } return(projectReports); }
/// <summary> /// Create a new List with new instances of ParcelFiscal. /// </summary> /// <param name="context"></param> /// <param name="parcel"></param> /// <param name="fiscalYears"></param> /// <param name="key"></param> /// <param name="value"></param> /// <returns></returns> public static List <Entity.ParcelFiscal> CreateFiscals(this PimsContext context, Entity.Parcel parcel, int[] fiscalYears, Entity.FiscalKeys key = Entity.FiscalKeys.Estimated, decimal value = 1) { var evaluations = new List <Entity.ParcelFiscal>(fiscalYears.Length); foreach (var fiscalYear in fiscalYears) { evaluations.Add(context.CreateFiscal(parcel, fiscalYear, key, value)); } return(evaluations); }
/// <summary> /// Create a new instance of a Parcel and add it to the database context. /// </summary> /// <param name="context"></param> /// <param name="id"></param> /// <param name="code"></param> /// <param name="name"></param> /// <returns></returns> public static Entity.Agency CreateAgency(this PimsContext context, int id = 1, string code = "AG", string name = "Agency") { var agency = new Entity.Agency(code, name) { Id = id, RowVersion = new byte[] { 12, 13, 14 } }; context.Agencies.Add(agency); return(agency); }
/// <summary> /// Get the next available id from the PIMS_LEASE_ID_SEQ /// </summary> /// <param name="context"></param> public static Int64 GetNextLeaseSequenceValue(this PimsContext context) { SqlParameter result = new SqlParameter("@result", System.Data.SqlDbType.BigInt) { Direction = System.Data.ParameterDirection.Output }; context.Database.ExecuteSqlRaw("set @result = next value for dbo.PIMS_LEASE_ID_SEQ;", result); return((Int64)result.Value); }
/// <summary> /// Create a new List with new instances of Parcels. /// </summary> /// <param name="context"></param> /// <param name="startId"></param> /// <param name="count"></param> /// <returns></returns> public static List <Entity.Parcel> CreateParcels(this PimsContext context, int startId, int count) { var agency = context.Agencies.FirstOrDefault(a => a.Id == 1) ?? EntityHelper.CreateAgency(startId); var parcels = new List <Entity.Parcel>(count); for (var i = startId; i < (startId + count); i++) { parcels.Add(context.CreateParcel(i, 0, 0, agency)); } return(parcels); }
/// <summary> /// Transfer Project properties to a new agency with updated classifications /// </summary> /// <param name="context"></param> /// <param name="originalProject"></param> /// <param name="project"></param> /// <returns></returns> public static void TransferProjectProperties(this PimsContext context, Entity.Project originalProject, Entity.Project project) { var parentParcels = originalProject.GetSubdivisionParentParcels(); context.DisposeSubdivisionParentParcels(parentParcels); var propertiesWithNoSubdivisions = originalProject.Properties.Where(p => !parentParcels.Any(pp => p.Parcel?.Id == pp.Id)); propertiesWithNoSubdivisions.ForEach(p => { var matchingProperty = project.Properties.First(property => p.Id == property.Id); context.Update(p.UpdateProjectProperty(matchingProperty)); }); }
/// <summary> /// Create a new instance of a Parcel and add it to the database context. /// </summary> /// <param name="context"></param> /// <param name="name"></param> /// <param name="type"></param> /// <param name="identifierType"></param> /// <param name="address"></param> /// <returns></returns> public static Entity.PimsOrganization CreateOrganization(this PimsContext context, long id, string name, Entity.PimsOrganizationType type = null, Entity.PimsOrgIdentifierType identifierType = null, Entity.PimsAddress address = null) { type ??= context.PimsOrganizationTypes.FirstOrDefault() ?? throw new InvalidOperationException("Unable to find an organization type."); identifierType ??= context.PimsOrgIdentifierTypes.FirstOrDefault() ?? throw new InvalidOperationException("Unable to find an organization identifier type."); address ??= EntityHelper.CreateAddress(id); var organization = new Entity.PimsOrganization(name, type, identifierType, address) { Id = id, ConcurrencyControlNumber = 1 }; return(organization); }
/// <summary> /// Create a new instance of an Property. /// Adds the property to the specified 'context'. /// </summary> /// <param name="context"></param> /// <param name="pid"></param> /// <param name="type"></param> /// <param name="classification"></param> /// <param name="address"></param> /// <param name="tenure"></param> /// <param name="areaUnit"></param> /// <param name="dataSource"></param> /// <returns></returns> public static Entity.PimsProperty CreateProperty(this PimsContext context, int pid, int?pin = null, Entity.PimsPropertyType type = null, Entity.PimsPropertyClassificationType classification = null, Entity.PimsAddress address = null, Entity.PimsPropertyTenureType tenure = null, Entity.PimsAreaUnitType areaUnit = null, Entity.PimsDataSourceType dataSource = null) { type ??= context.PimsPropertyTypes.FirstOrDefault() ?? throw new InvalidOperationException("Unable to find a property type."); classification ??= context.PimsPropertyClassificationTypes.FirstOrDefault() ?? throw new InvalidOperationException("Unable to find a property classification type."); address ??= context.CreateAddress(pid, "12342 Test Street"); tenure ??= context.PimsPropertyTenureTypes.FirstOrDefault() ?? throw new InvalidOperationException("Unable to find a property tenure type."); areaUnit ??= context.PimsAreaUnitTypes.FirstOrDefault() ?? throw new InvalidOperationException("Unable to find a property area unit type."); dataSource ??= context.PimsDataSourceTypes.FirstOrDefault() ?? throw new InvalidOperationException("Unable to find a property data source type."); var lease = context.PimsLeases.FirstOrDefault() ?? EntityHelper.CreateLease(pid); var property = EntityHelper.CreateProperty(pid, pin, type, classification, address, tenure, areaUnit, dataSource); context.PimsProperties.Add(property); return(property); }
/// <summary> /// Generates a query that returns properties in ERP and/or SPL. /// Note - this does not filter properties that a user shouldn't not be able to view. /// Note - this will return sensitive properties. /// </summary> /// <param name="context"></param> /// <param name="filter"></param> /// <returns></returns> private static IQueryable <Entity.Views.Property> GenerateProjectQuery(this PimsContext context, Entity.Models.AllPropertyFilter filter) { filter.ThrowIfNull(nameof(filter)); if (filter.InEnhancedReferralProcess == true || filter.InSurplusPropertyProgram == true) { var workflowCodes = new List <string>(); if (filter.InEnhancedReferralProcess == true) { workflowCodes.Add("ERP"); // TODO: This should be configurable, not hardcoded. } if (filter.InSurplusPropertyProgram == true) { workflowCodes.Add("SPL"); // TODO: This should be configurable, not hardcoded. } var codes = workflowCodes.ToArray(); var doNotIncludeCodes = new[] { "DE", "DIS", "CA", "T-GRE" }; // TODO: This should be configurable, not hardcoded. // Generate a query that finds all properties in projects that match the specified workflow. var properties = (from vp in context.Properties join pp in ( context.Projects .Where(p => codes.Contains(p.Workflow.Code) && !doNotIncludeCodes.Contains(p.Status.Code)) .SelectMany(p => p.Properties) .Where(p => p.PropertyType == Entities.PropertyTypes.Land || p.PropertyType == Entities.PropertyTypes.Subdivision) .Select(p => p.Parcel) )on new { vp.Id, PropertyTypeId = (int)vp.PropertyTypeId } equals new { pp.Id, PropertyTypeId = (int)pp.PropertyTypeId } select new { vp.Id, vp.PropertyTypeId }) .Union(from vp in context.Properties join pp in ( context.Projects .Where(p => codes.Contains(p.Workflow.Code) && !doNotIncludeCodes.Contains(p.Status.Code)) .SelectMany(p => p.Properties) .Where(p => p.PropertyType == Entities.PropertyTypes.Building) .Select(p => p.Building) )on new { vp.Id, PropertyTypeId = (int)vp.PropertyTypeId } equals new { pp.Id, PropertyTypeId = (int)pp.PropertyTypeId } select new { vp.Id, vp.PropertyTypeId }) .Distinct(); // Join result of prior query to view this way because spatial data types cannot be included in a union statement. return((from p in context.Properties join pp in properties on new { p.Id, p.PropertyTypeId } equals new { pp.Id, pp.PropertyTypeId } select p).AsNoTracking()); } return(context.Properties.AsNoTracking()); }
/// <summary> /// Initializes the database with default data to support other tables. /// </summary> /// <param name="context"></param> /// <returns></returns> public static PimsContext SeedDatabase(this PimsContext context) { context.AddRange(EntityHelper.CreateDefaultPropertyClassifications()); context.AddRange(EntityHelper.CreateDefaultProvinces()); context.AddRange(EntityHelper.CreateDefaultAdministrativeAreas()); context.AddRange(EntityHelper.CreateDefaultRoles()); context.AddRange(EntityHelper.CreateDefaultBuildingPredominateUses()); context.AddRange(EntityHelper.CreateDefaultBuildingConstructionTypes()); context.AddRange(EntityHelper.CreateDefaultBuildingOccupantTypes()); context.AddRange(EntityHelper.CreateDefaultAgencies()); context.AddRange(EntityHelper.CreateDefaultTierLevels()); context.AddRange(EntityHelper.CreateDefaultProjectRisks()); context.SaveChanges(); return(context); }
/// <summary> /// Create a new instance of an Person. /// </summary> /// <param name="id"></param> /// <param name="surname"></param> /// <param name="firstName"></param> /// <param name="address"></param> /// <returns></returns> public static Entity.PimsPerson CreatePerson(this PimsContext context, long id, string surname, string firstName, Entity.PimsAddress address = null) { address ??= EntityHelper.CreateAddress(context, id, "1234 St"); return(new Entity.PimsPerson(surname, firstName, address) { PersonId = id, ConcurrencyControlNumber = 1, AppCreateUserDirectory = "", AppCreateUserid = "", AppLastUpdateUserDirectory = "", AppLastUpdateUserid = "", DbCreateUserid = "", DbLastUpdateUserid = "", IsDisabled = false }); }
/// <summary> /// Create a new instance of a Lease. /// </summary> /// <returns></returns> public static Entity.PimsLease CreateLease(this PimsContext context, int pid, string lFileNo = null, string tenantFirstName = null, string tenantLastName = null, string motiFirstName = null, string motiLastName = null, PimsAddress address = null, bool addTenant = false, bool addProperty = true) { var programType = context.PimsLeaseProgramTypes.FirstOrDefault() ?? throw new InvalidOperationException("Unable to find lease program type."); var leasePurposeType = context.PimsLeasePurposeTypes.FirstOrDefault() ?? throw new InvalidOperationException("Unable to find lease purpose type."); var leaseStatusType = context.PimsLeaseStatusTypes.FirstOrDefault() ?? throw new InvalidOperationException("Unable to find lease status type."); var leasePayRvblType = context.PimsLeasePayRvblTypes.FirstOrDefault() ?? throw new InvalidOperationException("Unable to find lease rvbl type."); var leaseCategoryType = context.PimsLeaseCategoryTypes.FirstOrDefault() ?? throw new InvalidOperationException("Unable to find lease category type."); var leaseInitiatorType = context.PimsLeaseInitiatorTypes.FirstOrDefault() ?? throw new InvalidOperationException("Unable to find lease initiator type."); var leaseResponsibilityType = context.PimsLeaseResponsibilityTypes.FirstOrDefault() ?? throw new InvalidOperationException("Unable to find lease reponsibility type."); var leaseLicenseType = context.PimsLeaseLicenseTypes.FirstOrDefault() ?? throw new InvalidOperationException("Unable to find lease license type."); var lease = EntityHelper.CreateLease(pid, lFileNo, tenantFirstName, tenantLastName, motiFirstName, motiLastName, address, addTenant, addProperty, programType, leasePurposeType, leaseStatusType, leasePayRvblType, leaseCategoryType, leaseInitiatorType, leaseResponsibilityType, leaseLicenseType); context.PimsLeases.Add(lease); return(lease); }
/// <summary> /// Set Project properties' visibility to other agencies /// </summary> /// <param name="context"></param> /// <param name="project"></param> /// <param name="visibility"></param> public static void SetProjectPropertiesVisiblity(this PimsContext context, Entity.Project project, bool visibility) { project.Properties.ForEach(p => { if (p.BuildingId.HasValue) { p.Building.IsVisibleToOtherAgencies = visibility; context.Buildings.Update(p.Building); } else if (p.ParcelId.HasValue) { p.Parcel.IsVisibleToOtherAgencies = visibility; context.Parcels.Update(p.Parcel); } }); }
/// <summary> /// Generate a query for the specified 'filter'. /// Returns all properties, even properties that don't belong to the user's agency or sub-agencies. /// The results of this query must be 'cleaned' so that only appropriate data is returned to API. /// </summary> /// <param name="context"></param> /// <param name="user"></param> /// <param name="filter"></param> /// <returns></returns> public static IQueryable <Entity.Views.Property> GenerateAllPropertyQuery(this PimsContext context, ClaimsPrincipal user, Entity.Models.AllPropertyFilter filter) { filter.ThrowIfNull(nameof(filter)); filter.ThrowIfNull(nameof(user)); var query = context.GenerateProjectQuery(filter); // Only return properties owned by user's agency or sub-agencies. if (!filter.IncludeAllProperties) { var userAgencies = user.GetAgenciesAsNullable(); query = query.Where(p => userAgencies.Contains(p.AgencyId)); } query = context.GenerateCommonQuery(query, user, filter); return(query); }
/// <summary> /// Update the project number for the specified 'project' with the specified 'projectNumber'. /// </summary> /// <param name="context"></param> /// <param name="project"></param> /// <param name="projectNumber"></param> public static void UpdateProjectNumber(this PimsContext context, Project project, string projectNumber) { project.ProjectNumber = projectNumber; context.Update(project); project.Properties.ForEach(p => { if (p.PropertyType == PropertyTypes.Land && p.Parcel == null) { p.Parcel = context.Parcels.Find(p.ParcelId); } else if (p.PropertyType == PropertyTypes.Building && p.Building == null) { p.Building = context.Buildings.Find(p.BuildingId); } context.Update(p.UpdateProjectNumber(projectNumber)); }); }
/// <summary> /// Create a new instance of a ProjectAgencyResponse. /// </summary> /// <param name="context"></param> /// <param name="projectId"></param> /// <param name="agencyId"></param> /// <returns></returns> public static Entity.ProjectAgencyResponse CreateResponse(this PimsContext context, int projectId, int agencyId) { var response = new Entity.ProjectAgencyResponse() { ProjectId = projectId, AgencyId = agencyId, Note = "NOTE", ReceivedOn = DateTime.UtcNow, Response = Entity.NotificationResponses.Subscribe, CreatedById = Guid.NewGuid(), CreatedOn = DateTime.UtcNow, UpdatedById = Guid.NewGuid(), UpdatedOn = DateTime.UtcNow, RowVersion = new byte[] { 12, 13, 14 } }; context.ProjectAgencyResponses.Add(response); return(response); }
/// <summary> /// Creates a new instance of a Building. /// </summary> /// <param name="context"></param> /// <param name="parcel"></param> /// <param name="id"></param> /// <param name="name"></param> /// <param name="lat"></param> /// <param name="lng"></param> /// <param name="agency"></param> /// <returns></returns> public static Entity.Building CreateBuilding(this PimsContext context, Entity.Parcel parcel, int id, string projectNumber = null, string name = null, int lat = 0, int lng = 0, Entity.Agency agency = null) { name ??= $"l{id}"; agency ??= parcel?.Agency ?? context.Agencies.FirstOrDefault() ?? EntityHelper.CreateAgency(id); var address = (parcel == null ? EntityHelper.CreateAddress(context, id, "1234 Street", null, "V9C9C9") : EntityHelper.CreateAddress(id, parcel.Address.Address1, parcel.Address.Address2, parcel.Address.AdministrativeArea, parcel.Address.Province, parcel.Address.Postal)); var predominateUse = context.BuildingPredominateUses.FirstOrDefault(b => b.Id == 1) ?? EntityHelper.CreateBuildingPredominateUse("use");; var constructionType = context.BuildingConstructionTypes.FirstOrDefault(b => b.Id == 1) ?? EntityHelper.CreateBuildingConstructionType("type"); var occupantType = context.BuildingOccupantTypes.FirstOrDefault(b => b.Id == 1) ?? EntityHelper.CreateBuildingOccupantType("occupant"); var classification = context.PropertyClassifications.FirstOrDefault(b => b.Id == 1) ?? EntityHelper.CreatePropertyClassification("classification"); var building = new Entity.Building(parcel, lat, lng) { Id = id, Name = name, ProjectNumbers = projectNumber, AgencyId = agency.Id, Agency = agency, AddressId = address.Id, Address = address, Classification = classification, ClassificationId = classification.Id, Description = $"description-{id}", BuildingPredominateUse = predominateUse, BuildingPredominateUseId = predominateUse.Id, BuildingConstructionType = constructionType, BuildingConstructionTypeId = constructionType.Id, BuildingOccupantType = occupantType, BuildingOccupantTypeId = occupantType.Id, CreatedById = Guid.NewGuid(), CreatedOn = DateTime.UtcNow, UpdatedById = Guid.NewGuid(), UpdatedOn = DateTime.UtcNow, RowVersion = new byte[] { 12, 13, 14 } }; if (parcel != null) { var parcelBuilding = new Entity.ParcelBuilding(parcel, building); } context.Buildings.Add(building); return(building); }
/// <summary> /// Create a new instance of a Project. /// </summary> /// <param name="context"></param> /// <param name="id"></param> /// <param name="agency"></param> /// <param name="workflowStatus"></param> /// <param name="risk"></param> /// <returns></returns> public static Entity.Project CreateProject(this PimsContext context, int id, Entity.Agency agency = null, Entity.WorkflowProjectStatus workflowStatus = null, Entity.ProjectRisk risk = null) { agency ??= context.Agencies.OrderBy(a => a.Id).FirstOrDefault() ?? EntityHelper.CreateAgency(1); risk ??= context.ProjectRisks.FirstOrDefault() ?? EntityHelper.CreateProjectRisk(1, "risk", "risk", 1); workflowStatus ??= context.WorkflowProjectStatus.FirstOrDefault(ws => ws.WorkflowId == 1 && ws.StatusId == 1); if (workflowStatus == null) { var status = workflowStatus?.Status ?? EntityHelper.CreateProjectStatus("Draft", "DR"); var workflow = workflowStatus?.Workflow ?? EntityHelper.CreateWorkflow(id, "Submit", "SUBMIT-DISPOSAL", new[] { status }); workflowStatus ??= workflow.Status.First(); } var tierLevel = context.TierLevels.FirstOrDefault(s => s.Id == 1) ?? EntityHelper.CreateTierLevel("tierLevel"); var project = EntityHelper.CreateProject(id, agency, tierLevel, workflowStatus, risk); context.Projects.Add(project); return(project); }
/// <summary> /// Create a new List with new instances of Projects. /// </summary> /// <param name="context"></param> /// <param name="startId"></param> /// <param name="count"></param> /// <param name="agency"></param> /// <param name="workflowStatus"></param> /// <param name="risk"></param> /// <returns></returns> public static List <Entity.Project> CreateProjects(this PimsContext context, int startId, int count, Entity.Agency agency = null, Entity.WorkflowProjectStatus workflowStatus = null, Entity.ProjectRisk risk = null) { agency ??= context.Agencies.FirstOrDefault(a => a.Id == 1) ?? EntityHelper.CreateAgency(startId); risk ??= context.ProjectRisks.FirstOrDefault() ?? EntityHelper.CreateProjectRisk(1, "risk", "risk", 1); workflowStatus ??= context.WorkflowProjectStatus.FirstOrDefault(ws => ws.WorkflowId == 1 && ws.StatusId == 1); if (workflowStatus == null) { var status = workflowStatus?.Status ?? EntityHelper.CreateProjectStatus("Draft", "DR"); var workflow = workflowStatus?.Workflow ?? EntityHelper.CreateWorkflow(startId, "Submit", "SUBMIT-DISPOSAL", new[] { status }); workflowStatus ??= workflow.Status.First(); } var projects = new List <Entity.Project>(count); for (var i = startId; i < (startId + count); i++) { projects.Add(context.CreateProject(i, agency, workflowStatus, risk)); } return(projects); }
/// <summary> /// Generate a query for the specified 'filter'. /// Only includes properties that belong to the user's organization or sub-organizations. /// </summary> /// <param name="context"></param> /// <param name="user"></param> /// <param name="filter"></param> /// <returns></returns> public static IQueryable <Entity.PimsProperty> GeneratePropertyQuery(this PimsContext context, ClaimsPrincipal user, Entity.Models.PropertyFilter filter) { filter.ThrowIfNull(nameof(filter)); filter.ThrowIfNull(nameof(user)); // Users may only view sensitive properties if they have the `sensitive-view` claim and belong to the owning organization. var query = context.PimsProperties .Include(p => p.Address) .ThenInclude(a => a.RegionCodeNavigation) .Include(p => p.Address) .ThenInclude(a => a.DistrictCodeNavigation) .Include(p => p.Address) .ThenInclude(a => a.ProvinceState) .Include(p => p.Address) .ThenInclude(a => a.Country) .AsNoTracking(); query = query.GenerateCommonPropertyQuery(user, filter); return(query); }
/// <summary> /// Update the project financial values for the specified 'projectNumber'. /// </summary> /// <param name="context"></param> /// <param name="projectNumber"></param> public static void UpdateProjectFinancials(this PimsContext context, string projectNumber) { var project = context.Projects .Include(p => p.Properties) .ThenInclude(p => p.Parcel) .ThenInclude(p => p.Evaluations) .Include(p => p.Properties) .ThenInclude(p => p.Parcel) .ThenInclude(p => p.Fiscals) .Include(p => p.Properties) .ThenInclude(p => p.Building) .ThenInclude(b => b.Evaluations) .Include(p => p.Properties) .ThenInclude(p => p.Building) .ThenInclude(b => b.Fiscals) .FirstOrDefault(p => p.ProjectNumber == projectNumber); JsonConvert.PopulateObject(project.Metadata, project); project.UpdateProjectFinancials(); }