2023-10-17 11:24:52 +05:00
using AsbCloudApp.Data.SAUB ;
2023-10-23 13:43:29 +05:00
using AsbCloudApp.Exceptions ;
2023-10-17 11:24:52 +05:00
using AsbCloudApp.Repositories ;
2023-10-20 11:24:04 +05:00
using AsbCloudApp.Requests ;
2023-10-17 11:24:52 +05:00
using AsbCloudDb.Model ;
using Mapster ;
2023-10-20 11:24:04 +05:00
using Microsoft.EntityFrameworkCore ;
using System ;
2023-10-23 13:43:29 +05:00
using System.Collections.Generic ;
2023-10-20 11:24:04 +05:00
using System.Linq ;
2023-10-17 11:24:52 +05:00
using System.Threading ;
using System.Threading.Tasks ;
namespace AsbCloudInfrastructure.Repository
{
public class DrillTestRepository : IDrillTestRepository
{
private readonly IAsbCloudDbContext db ;
2023-10-23 14:15:48 +05:00
public DrillTestRepository ( IAsbCloudDbContext db )
2023-10-17 11:24:52 +05:00
{
this . db = db ;
}
2023-10-23 13:43:29 +05:00
public async Task < IEnumerable < DrillTestDto > > GetAllAsync ( int idTelemetry , FileReportRequest request , CancellationToken cancellationToken )
2023-10-20 11:24:04 +05:00
{
var query = db . DrillTests
. Where ( d = > d . IdTelemetry = = idTelemetry )
2023-10-23 15:54:29 +05:00
. Include ( d = > d . Telemetry )
2023-10-20 11:24:04 +05:00
. AsNoTracking ( ) ;
if ( request . GeDate . HasValue )
{
2023-10-23 13:43:29 +05:00
var startDateUTC = new DateTimeOffset ( request . GeDate . Value . Year , request . GeDate . Value . Month , request . GeDate . Value . Day , 0 , 0 , 0 , TimeSpan . Zero ) ;
2023-10-20 11:41:55 +05:00
query = query . Where ( q = > q . TimeStampStart > = startDateUTC ) ;
2023-10-20 11:24:04 +05:00
}
if ( request . LeDate . HasValue )
{
2023-10-23 13:43:29 +05:00
var finishDateUTC = new DateTimeOffset ( request . LeDate . Value . Year , request . LeDate . Value . Month , request . LeDate . Value . Day , 0 , 0 , 0 , TimeSpan . Zero ) ;
2023-10-20 11:41:55 +05:00
query = query . Where ( q = > q . TimeStampStart < = finishDateUTC ) ;
2023-10-20 11:24:04 +05:00
}
var entities = await query . ToListAsync ( cancellationToken ) ;
2023-10-23 15:54:29 +05:00
var dtos = entities . Select ( e = > Convert ( e ) ) ;
2023-10-20 11:24:04 +05:00
return dtos ;
}
public async Task < DrillTestDto > GetAsync ( int idTelemetry , int id , CancellationToken cancellationToken )
{
var drillTest = await db . DrillTests
. Where ( d = > d . Id = = id )
2023-10-23 13:43:29 +05:00
. Include ( d = > d . Telemetry )
. Where ( d = > d . Telemetry . Id = = idTelemetry )
2023-10-20 11:24:04 +05:00
. FirstOrDefaultAsync ( cancellationToken ) ;
if ( drillTest is null )
2023-10-23 13:43:29 +05:00
throw new ArgumentInvalidException ( new string [ ] { nameof ( id ) , nameof ( idTelemetry ) } , $"Drill test with id: {id} and idTelemetry: {idTelemetry} does not exist." ) ;
2023-10-20 11:24:04 +05:00
2023-10-23 15:54:29 +05:00
var dto = Convert ( drillTest ) ;
2023-10-23 13:43:29 +05:00
2023-10-20 11:24:04 +05:00
return dto ;
}
2024-02-19 10:32:15 +05:00
public async Task < int > SaveDataAsync ( int idTelemetry , DrillTestBaseDto dto , CancellationToken token )
2023-10-17 11:24:52 +05:00
{
var entity = dto . Adapt < DrillTest > ( ) ;
entity . IdTelemetry = idTelemetry ;
db . DrillTests . Add ( entity ) ;
var result = await db . SaveChangesAsync ( token ) ;
return result ;
}
2023-10-23 15:54:29 +05:00
private DrillTestDto Convert ( DrillTest entity )
{
var dto = entity . Adapt < DrillTestDto > ( ) ;
dto . TimeStampStart = dto . TimeStampStart . ToRemoteDateTime ( dto . Telemetry ? . TimeZone ? . Hours ? ? 0 ) ;
return dto ;
}
2023-10-17 11:24:52 +05:00
}
}