Add logic for soft-deletes on stations which no longer appear in the onegov reference data.
This commit is contained in:
@@ -21,6 +21,8 @@ public class Station
|
||||
|
||||
public DateTimeOffset CreatedAt { get; set; } = DateTimeOffset.UtcNow;
|
||||
|
||||
public DateTimeOffset? DeletedAt { get; set; }
|
||||
|
||||
//ignored property to use for showing distance to a station from a point
|
||||
[Ignore] public double? Distance { get; set; }
|
||||
|
||||
|
||||
@@ -72,15 +72,27 @@ public class NswFuelApiService : INswFuelApiService
|
||||
Geography = $"SRID=7844;POINT({dto.Location.Longitude} {dto.Location.Latitude})"
|
||||
});
|
||||
|
||||
//stations which exist in the DB, but not in the reference data (they have been closed or updated)
|
||||
var removedStations =
|
||||
existingStations.Where(es => mappedStationData.All(ms => ms.Code != es.Code)).ToList();
|
||||
foreach (var rs in removedStations)
|
||||
{
|
||||
rs.DeletedAt = DateTimeOffset.UtcNow;
|
||||
}
|
||||
|
||||
await db.UpdateAllAsync(removedStations);
|
||||
|
||||
//stations which do not exist in the DB, but exist in the reference data (they are new)
|
||||
var newStationsToAdd = mappedStationData.Where(s =>
|
||||
existingStations.All(es =>
|
||||
es.Code != s.Code));
|
||||
|
||||
db.BulkInsert(newStationsToAdd);
|
||||
transaction.Commit();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.LogError("Error while attempting to get update lovs: {Error}", e);
|
||||
_logger.LogError("Error while attempting to get lovs: {Error}", e);
|
||||
transaction.Rollback();
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -21,6 +21,8 @@ public class StationService : IStationService
|
||||
var totalCount = await db.CountAsync<Station>();
|
||||
|
||||
var stations = await db.SelectAsync(db.From<Station>()
|
||||
//do not show soft-deleted stations
|
||||
.Where(x => x.DeletedAt == null)
|
||||
.OrderBy(x => x.Code)
|
||||
.Limit((pageNumber - 1) * pageSize, pageSize));
|
||||
|
||||
@@ -30,7 +32,7 @@ public class StationService : IStationService
|
||||
public async Task<Station> GetStationByStationCode(int stationCode)
|
||||
{
|
||||
using var db = _dbConnectionFactory.OpenDbConnection();
|
||||
var station = await db.SingleAsync<Station>(x => x.Code == stationCode);
|
||||
var station = await db.SingleAsync<Station>(x => x.Code == stationCode && x.DeletedAt == null);
|
||||
return station;
|
||||
}
|
||||
|
||||
@@ -54,6 +56,7 @@ public class StationService : IStationService
|
||||
)
|
||||
AND (@Brand IS NULL OR brand = @Brand)
|
||||
AND (@StationName IS NULL OR LOWER(name) ILIKE '%' || LOWER(@StationName) || '%')
|
||||
AND (deleted_at IS NULL)
|
||||
ORDER BY distance
|
||||
LIMIT @PageSize OFFSET @Offset
|
||||
""";
|
||||
@@ -95,10 +98,17 @@ public class StationService : IStationService
|
||||
ST_SetSRID(ST_MakePoint(@Longitude, @Latitude), 7844)::geography,
|
||||
@Radius
|
||||
)
|
||||
AND (@Brand IS NULL OR brand = @Brand)
|
||||
AND (@StationName IS NULL OR LOWER(name) ILIKE '%' || LOWER(@StationName) || '%')
|
||||
AND (deleted_at IS NULL)
|
||||
""";
|
||||
|
||||
var totalCount = await db.ScalarAsync<long>(countSql,
|
||||
new { Latitude = latitude, Longitude = longitude, Radius = radiusInMetres });
|
||||
new
|
||||
{
|
||||
Latitude = latitude, Longitude = longitude, Radius = radiusInMetres, Brand = brand,
|
||||
StationName = stationName
|
||||
});
|
||||
|
||||
return new Pagination<Station>(stations, totalCount, pageNumber, pageSize);
|
||||
}
|
||||
@@ -117,6 +127,7 @@ public class StationService : IStationService
|
||||
@Radius
|
||||
)
|
||||
AND (@Brand IS NULL OR brand = @Brand)
|
||||
AND (deleted_at IS NULL)
|
||||
""";
|
||||
|
||||
//use station with distance to map the distance values
|
||||
|
||||
@@ -26,7 +26,7 @@ public class StationWithPricesService : IStationWithPricesService
|
||||
|
||||
var q = db.From<Station>()
|
||||
.LeftJoin<Price>((station, price) => station.Code == price.StationCode)
|
||||
.Where<Station>(s => s.Code == stationCode);
|
||||
.Where<Station>(s => s.Code == stationCode && s.DeletedAt == null);
|
||||
|
||||
var results = await db.SelectMultiAsync<Station, Price>(q.SelectDistinct());
|
||||
|
||||
|
||||
Reference in New Issue
Block a user