Exemplo n.º 1
0
        private async Task <int> DeleteGeofence(Geofence geofence, Geofence existing)
        {
            Log.LogDebug(
                $"GeofenceRepository/DeleteGeofence: going to update geofence={JsonConvert.SerializeObject(geofence)}");

            var upsertedCount = 0;

            if (existing != null)
            {
                if (geofence.LastActionedUTC >= existing.LastActionedUTC)
                {
                    Log.LogDebug($"GeofenceRepository/DeleteGeofence: going to update geofence={geofence.GeofenceUID}");

                    const string update =
                        @"UPDATE Geofence
                SET IsDeleted = 1,
                  LastActionedUTC = @LastActionedUTC
                WHERE GeofenceUID = @GeofenceUID";

                    upsertedCount = await ExecuteWithAsyncPolicy(update, geofence);

                    Log.LogDebug(
                        $"GeofenceRepository/DeleteGeofence: (update): upserted {upsertedCount} rows for: geofenceUid:{geofence.GeofenceUID}");
                    return(upsertedCount);
                }

                Log.LogDebug($"GeofenceRepository/DeleteGeofence: old delete event ignored geofence={geofence.GeofenceUID}");
            }
            else
            {
                geofence.Setup();

                Log.LogDebug(
                    $"GeofenceRepository/DeleteGeofence: going to insert a deleted dummy geofence={geofence.GeofenceUID}");
                var formattedPolygon = RepositoryHelper.WKTToSpatial(geofence.GeometryWKT);

                var insert =
                    "INSERT Geofence " +
                    "      (GeofenceUID, Name, Description, PolygonST, FillColor, IsTransparent, IsDeleted, fk_CustomerUID, UserUID, LastActionedUTC, fk_GeofenceTypeID, AreaSqMeters) " +
                    "  VALUES " +
                    $"      (@GeofenceUID, @Name, @Description, {formattedPolygon}, @FillColor, @IsTransparent, @IsDeleted, @CustomerUID, @UserUID, @LastActionedUTC, @GeofenceType, @AreaSqMeters)";
                upsertedCount = await ExecuteWithAsyncPolicy(insert, geofence);

                Log.LogDebug($"DeleteGeofence (insert): upserted {upsertedCount} rows for: geofenceUid:{geofence.GeofenceUID}");
                return(upsertedCount);
            }

            return(upsertedCount);
        }
Exemplo n.º 2
0
        private async Task <int> UpdateGeofence(Geofence geofence, Geofence existing)
        {
            Log.LogDebug($"GeofenceRepository/UpdateGeofence: geofence={JsonConvert.SerializeObject(geofence)}");
            var upsertedCount = 0;

            if (existing != null)
            {
                if (geofence.LastActionedUTC >= existing.LastActionedUTC)
                {
                    if (string.IsNullOrEmpty(geofence.Name))
                    {
                        geofence.Name = existing.Name;
                    }
                    // can't change TO generic
                    if (geofence.GeofenceType == GeofenceType.Generic)
                    {
                        geofence.GeofenceType = existing.GeofenceType;
                    }
                    if (string.IsNullOrEmpty(geofence.GeometryWKT))
                    {
                        geofence.GeometryWKT = existing.GeometryWKT;
                    }
                    if (!geofence.FillColor.HasValue)
                    {
                        geofence.FillColor = existing.FillColor;
                    }
                    if (!geofence.IsTransparent.HasValue)
                    {
                        geofence.IsTransparent = existing.IsTransparent;
                    }
                    // customerUID is not actually in an UpdateGeofenceEvent, but just to future-proof
                    if (string.IsNullOrEmpty(geofence.CustomerUID))
                    {
                        geofence.CustomerUID = existing.CustomerUID;
                    }
                    if (!Guid.TryParse(geofence.UserUID, out var gotUpdatedGuid) || gotUpdatedGuid == Guid.Empty)
                    {
                        geofence.UserUID = existing.UserUID;
                    }
                    if (string.IsNullOrEmpty(geofence.Description))
                    {
                        geofence.Description = existing.Description;
                    }

                    // Note that AreaSqMeters is stored as 0 dp in database
                    if (Math.Abs(geofence.AreaSqMeters) < 0.0001)
                    {
                        geofence.AreaSqMeters = existing.AreaSqMeters;
                    }

                    Log.LogDebug($"GeofenceRepository/UpdateGeofence: going to update geofence={geofence.GeofenceUID}");

                    var formattedPolygon = RepositoryHelper.WKTToSpatial(geofence.GeometryWKT);

                    var update =
                        "UPDATE Geofence " +
                        $"      SET Name = @Name, fk_GeofenceTypeID = @GeofenceType, PolygonST = {formattedPolygon}, FillColor = @FillColor, IsTransparent = @IsTransparent, fk_CustomerUID = @CustomerUID, UserUID = @UserUID, Description = @Description, LastActionedUTC = @LastActionedUTC, AreaSqMeters = @AreaSqMeters " +
                        "    WHERE GeofenceUID = @GeofenceUID";

                    upsertedCount = await ExecuteWithAsyncPolicy(update, geofence);

                    Log.LogDebug(
                        $"UpdateGeofence (update): upserted {upsertedCount} rows for: geofenceUid:{geofence.GeofenceUID}");
                    return(upsertedCount);
                }

                Log.LogDebug($"GeofenceRepository/UpdateGeofence: old update event ignored geofence={geofence.GeofenceUID}");
            }
            else
            {
                Log.LogDebug(
                    $"GeofenceRepository/UpdateGeofence: update received before the Create. Creating geofence={geofence.GeofenceUID}");

                geofence.Setup();

                var formattedPolygon = RepositoryHelper.WKTToSpatial(geofence.GeometryWKT);

                var insert =
                    "INSERT Geofence " +
                    "      (GeofenceUID, Name, Description, PolygonST, FillColor, IsTransparent, IsDeleted, fk_CustomerUID, UserUID, LastActionedUTC, fk_GeofenceTypeID, AreaSqMeters) " +
                    "  VALUES " +
                    $"     (@GeofenceUID, @Name, @Description, {formattedPolygon}, @FillColor, @IsTransparent, @IsDeleted, @CustomerUID, @UserUID, @LastActionedUTC, @GeofenceType, @AreaSqMeters)";

                upsertedCount = await ExecuteWithAsyncPolicy(insert, geofence);

                Log.LogDebug(
                    $"GeofenceRepository/CreateGeofence upserted {upsertedCount} rows for: geofenceUid:{geofence.GeofenceUID}");
                return(upsertedCount);
            }

            return(upsertedCount);
        }