diff --git a/AsbCloudInfrastructure/Services/DataService.cs b/AsbCloudInfrastructure/Services/DataService.cs index 089dcf9e..2cfb9d08 100644 --- a/AsbCloudInfrastructure/Services/DataService.cs +++ b/AsbCloudInfrastructure/Services/DataService.cs @@ -68,19 +68,32 @@ namespace AsbCloudInfrastructure.Services public void UpdateData(string uid, IEnumerable dtos) { + if (dtos == default || dtos.Count() <= 0) + return; + var telemetryId = telemetryService.GetOrCreateTemetryIdByUid(uid); + var dtoMinDate = dtos.Min(d => d.Date); + var dtoMaxDate = dtos.Max(d => d.Date); - if ((dtos != default) && (dtos.Count() > 0)) + var oldDataSaubBase = (from d in db.DataSaubBases + where d.IdTelemetry == telemetryId + && d.Date > dtoMinDate + && d.Date < dtoMaxDate + select d).ToList(); + + if (oldDataSaubBase.Any()) { - foreach (var item in dtos) - { - var dataSaub = mapper.Map(item); - dataSaub.IdTelemetry = telemetryId; - db.DataSaubBases.Add(dataSaub); - } - - db.SaveChanges(); + db.DataSaubBases.RemoveRange(oldDataSaubBase); } + + foreach (var item in dtos) + { + var dataSaub = mapper.Map(item); + dataSaub.IdTelemetry = telemetryId; + db.DataSaubBases.Add(dataSaub); + } + + db.SaveChanges(); } }