private async Task UpsertProjectTypeGeofence(string upsertType, ProjectDataModel project) { if (string.IsNullOrEmpty(project.Boundary)) { Log.LogInformation( $"ProjectRepository/UpsertProjectTypeGeofence: Unable to Upsert GeofenceBoundary as boundary not available. UpsertType {upsertType}. project={project.ProjectUID}."); return; } // may be an existing one if this create comes from a replay of kafka que. var select = "SELECT GeofenceUID, Name, fk_GeofenceTypeID AS GeofenceType, ST_ASWKT(PolygonST) AS GeometryWKT, " + " FillColor, IsTransparent, IsDeleted, Description, fk_CustomerUID AS CustomerUID, UserUID, " + " AreaSqMeters, g.LastActionedUTC " + " FROM ProjectGeofence pg " + " INNER JOIN Geofence g ON g.GeofenceUID = pg.fk_GeofenceUID " + $" WHERE fk_ProjectUID = '{project.ProjectUID}' " + $" AND fk_GeofenceTypeID = {(int)GeofenceType.Project}; "; var existingGeofence = (await QueryWithAsyncPolicy <Geofence>(select)).FirstOrDefault(); Log.LogDebug( $"ProjectRepository/UpsertProjectTypeGeofence: going to upsert. upsertType {upsertType}. project={project.ProjectUID} existingGeofence? {existingGeofence}"); if (existingGeofence == null) { await CreateGeofenceAndAssociation(project); } else { await UpdateGeofence(project, existingGeofence); } }
private async Task <int> UpdateGeofence(ProjectDataModel project, Geofence existingGeofence) { string formattedPolygon = RepositoryHelper.WKTToSpatial(project.Boundary); var update = "UPDATE Geofence " + $" SET PolygonST = {formattedPolygon} " + $" WHERE GeofenceUID = '{existingGeofence.GeofenceUID}' " + $" AND fk_GeofenceTypeID = {(int)GeofenceType.Project}; "; var upsertedCount = await ExecuteWithAsyncPolicy(update); Log.LogDebug( $"ProjectRepository/UpsertGeofence updated. upsertedCount {upsertedCount} rows for: geofenceUid:{existingGeofence.GeofenceUID}"); return(upsertedCount); }
private async Task <int> CreateGeofenceAndAssociation(ProjectDataModel project) { var geofence = new Geofence().Setup(); geofence.GeofenceUID = Guid.NewGuid().ToString(); geofence.Name = project.Name; geofence.GeofenceType = GeofenceType.Project; geofence.GeometryWKT = project.Boundary; geofence.CustomerUID = project.CustomerUID; var area = GeofenceValidation.CalculateAreaSqMeters(project.Boundary); geofence.AreaSqMeters = area > 1000000000 ? 0 : area; geofence.IsDeleted = false; geofence.LastActionedUTC = DateTime.UtcNow; string formattedPolygon = RepositoryHelper.WKTToSpatial(project.Boundary); string insert = string.Format( "INSERT Geofence " + " (GeofenceUID, Name, Description, PolygonST, FillColor, IsTransparent, IsDeleted, fk_CustomerUID, UserUID, LastActionedUTC, fk_GeofenceTypeID, AreaSqMeters) " + " VALUES " + " (@GeofenceUID, @Name, @Description, {0}, @FillColor, @IsTransparent, @IsDeleted, @CustomerUID, @UserUID, @LastActionedUTC, @GeofenceType, @AreaSqMeters)", formattedPolygon); var upsertedCount = await ExecuteWithAsyncPolicy(insert, geofence); Log.LogDebug( $"ProjectRepository/UpsertGeofence inserted. upsertedCount {upsertedCount} rows for: geofenceUid:{geofence.GeofenceUID}"); if (upsertedCount == 1) { var projectGeofence = new ProjectGeofence() { ProjectUID = project.ProjectUID, GeofenceUID = geofence.GeofenceUID, LastActionedUTC = DateTime.UtcNow }; await AssociateProjectGeofence(projectGeofence, null); return(upsertedCount); } return(0); }
private async Task <int> InsertProjectHistory(ProjectDataModel project) { const string insert = @"INSERT INTO ProjectHistory ( ProjectUID, CustomerUID, ShortRaptorProjectID, Name, fk_ProjectTypeID, ProjectTimeZone, ProjectTimeZoneIana, Boundary, CoordinateSystemFileName, CoordinateSystemLastActionedUTC, IsArchived, LastActionedUTC) SELECT ProjectUID, CustomerUID, ShortRaptorProjectID, Name, fk_ProjectTypeID, ProjectTimeZone, ProjectTimeZoneIana, Boundary, CoordinateSystemFileName, CoordinateSystemLastActionedUTC, IsArchived, LastActionedUTC FROM Project WHERE ProjectUID = @ProjectUID;"; var insertedCount = await ExecuteWithAsyncPolicy(insert, project); Log.LogDebug($"ProjectRepository/CreateProjectHistory: inserted {insertedCount} rows"); return(insertedCount); }
public void MapProjectToResult() { var project = new ProjectDatabaseModel { ProjectUID = Guid.NewGuid().ToString(), ProjectType = CwsProjectType.AcceptsTagFiles, Name = "the Name", ProjectTimeZone = "NZ stuff", ProjectTimeZoneIana = "Pacific stuff", CustomerUID = Guid.NewGuid().ToString(), Boundary = "POLYGON((172.595831670724 -43.5427038560109,172.594630041089 -43.5438859356773,172.59329966542 -43.542486101965, 172.595831670724 -43.5427038560109))", CoordinateSystemFileName = "", CoordinateSystemLastActionedUTC = new DateTime(2017, 01, 21), IsArchived = false, LastActionedUTC = new DateTime(2017, 01, 21) }; var result = AutoMapperUtility.Automapper.Map <ProjectV6Descriptor>(project); Assert.Equal(project.ProjectUID, result.ProjectUid); Assert.Equal(new Guid(project.ProjectUID).ToLegacyId(), result.ShortRaptorProjectId); Assert.Equal(project.ProjectType, result.ProjectType); Assert.Equal(project.Name, result.Name); Assert.Equal(project.ProjectTimeZone, result.ProjectTimeZone); Assert.Equal(project.ProjectTimeZoneIana, result.IanaTimeZone); Assert.Equal(project.CustomerUID, result.CustomerUid); Assert.Equal(project.Boundary, result.ProjectGeofenceWKT); Assert.False(result.IsArchived, "IsArchived has not been mapped correctly"); // just make a copy var copyOfProject = AutoMapperUtility.Automapper.Map <ProjectDatabaseModel>(project); Assert.Equal(project.ProjectUID, copyOfProject.ProjectUID); Assert.Equal(project.ShortRaptorProjectId, copyOfProject.ShortRaptorProjectId); }