forked from ddrilling/AsbCloudServer
117 lines
4.2 KiB
C#
117 lines
4.2 KiB
C#
using AsbCloudApp.Data.SAUB;
|
||
using AsbCloudApp.Exceptions;
|
||
using AsbCloudApp.Repositories;
|
||
using AsbCloudApp.Requests;
|
||
using AsbCloudDb;
|
||
using AsbCloudDb.Model;
|
||
using Mapster;
|
||
using Microsoft.EntityFrameworkCore;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Threading;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace AsbCloudInfrastructure.Repository
|
||
{
|
||
public class DrillTestRepository : IDrillTestRepository
|
||
{
|
||
private readonly IAsbCloudDbContext db;
|
||
|
||
public DrillTestRepository(IAsbCloudDbContext db)
|
||
{
|
||
this.db = db;
|
||
}
|
||
|
||
public async Task<IEnumerable<DrillTestDto>> GetAllAsync(int idTelemetry, FileReportRequest request, CancellationToken cancellationToken)
|
||
{
|
||
var query = db.DrillTests
|
||
.Where(d => d.IdTelemetry == idTelemetry)
|
||
.Include(d => d.Telemetry)
|
||
.AsNoTracking();
|
||
|
||
if (request.GeDate.HasValue)
|
||
{
|
||
var startDateUTC = new DateTimeOffset(request.GeDate.Value.Year, request.GeDate.Value.Month, request.GeDate.Value.Day, 0, 0, 0, TimeSpan.Zero);
|
||
query = query.Where(q => q.TimeStampStart >= startDateUTC);
|
||
}
|
||
if (request.LeDate.HasValue)
|
||
{
|
||
var finishDateUTC = new DateTimeOffset(request.LeDate.Value.Year, request.LeDate.Value.Month, request.LeDate.Value.Day, 0, 0, 0, TimeSpan.Zero);
|
||
query = query.Where(q => q.TimeStampStart <= finishDateUTC);
|
||
}
|
||
|
||
var entities = await query.ToListAsync(cancellationToken);
|
||
var dtos = entities.Select(e => Convert(e));
|
||
|
||
return dtos;
|
||
}
|
||
|
||
|
||
public async Task<DrillTestDto> GetAsync(int idTelemetry, int id, CancellationToken cancellationToken)
|
||
{
|
||
var drillTest = await db.DrillTests
|
||
.Where(d => d.Id == id)
|
||
.Include(d => d.Telemetry)
|
||
.Where(d => d.Telemetry.Id == idTelemetry)
|
||
.FirstOrDefaultAsync(cancellationToken);
|
||
|
||
if (drillTest is null)
|
||
throw new ArgumentInvalidException(new string[] { nameof(id), nameof(idTelemetry) }, $"Drill test with id: {id} and idTelemetry: {idTelemetry} does not exist.");
|
||
|
||
var dto = Convert(drillTest);
|
||
|
||
return dto;
|
||
}
|
||
|
||
public async Task<int> SaveDataAsync(int idTelemetry, IEnumerable<DrillTestBaseDto> dtos, CancellationToken token)
|
||
{
|
||
var entities = dtos.Select(dto =>
|
||
{
|
||
var entity = dto.Adapt<DrillTest>();
|
||
entity.TimeStampStart = dto.TimeStampStart.ToUniversalTime();
|
||
entity.IdTelemetry = idTelemetry;
|
||
return entity;
|
||
});
|
||
|
||
var result = await db.Database.ExecInsertOrUpdateAsync(db.Set<DrillTest>(), entities, token);
|
||
return result;
|
||
}
|
||
|
||
private static DrillTestDto Convert(DrillTest entity)
|
||
{
|
||
var dto = entity.Adapt<DrillTestDto>();
|
||
dto.TimeStampStart = dto.TimeStampStart.ToRemoteDateTime(dto.Telemetry?.TimeZone?.Hours ?? 0);
|
||
return dto;
|
||
}
|
||
|
||
//TODO: <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>.<2E><><EFBFBD><EFBFBD><EFBFBD> <20> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>.
|
||
private IQueryable<DrillTest> BuildQuery(TelemetryPartDeleteRequest request)
|
||
{
|
||
var query = db.Set<DrillTest>()
|
||
.Where(o => o.IdTelemetry == request.IdTelemetry);
|
||
|
||
if (request.LeDate is not null)
|
||
{
|
||
var leDate = request.LeDate.Value.ToUniversalTime();
|
||
query = query.Where(o => o.TimeStampStart <= leDate);
|
||
}
|
||
|
||
if (request.GeDate is not null)
|
||
{
|
||
var geDate = request.GeDate.Value.ToUniversalTime();
|
||
query = query.Where(o => o.TimeStampStart >= geDate);
|
||
}
|
||
|
||
return query;
|
||
}
|
||
|
||
public async Task<int> DeleteAsync(TelemetryPartDeleteRequest request, CancellationToken token)
|
||
{
|
||
var query = BuildQuery(request);
|
||
db.Set<DrillTest>().RemoveRange(query);
|
||
return await db.SaveChangesAsync(token);
|
||
}
|
||
}
|
||
}
|