persistence/Persistence.Repository/Repositories/TechMessagesRepository.cs

71 lines
1.8 KiB
C#
Raw Normal View History

using Mapster;
using Microsoft.EntityFrameworkCore;
using Persistence.Database.Entity;
using Persistence.Models;
using Persistence.Repositories;
namespace Persistence.Repository.Repositories
{
public class TechMessagesRepository : ITechMessagesRepository
{
private DbContext db;
public TechMessagesRepository(DbContext db)
{
this.db = db;
}
protected virtual IQueryable<TechMessage> GetQueryReadOnly() => db.Set<TechMessage>();
public async Task<PaginationContainer<TechMessageDto>> GetPage(RequestDto request, CancellationToken token)
{
var query = GetQueryReadOnly();
var entities = await query
.SortBy(request.SortSettings)
.Skip(request.Skip)
.Take(request.Take)
.ToListAsync();
var dto = new PaginationContainer<TechMessageDto>()
{
Skip = request.Skip,
Take = request.Take,
Count = entities.Count,
Items = entities.Select(e => e.Adapt<TechMessageDto>())
};
return dto;
}
public async Task<int> GetStatistics(int importantId, string autoDrillingSystem, CancellationToken token)
{
var query = GetQueryReadOnly();
var count = await query
.Where(e => e.ImportantId == importantId && e.AutoDrillingSystem == autoDrillingSystem)
.CountAsync();
return count;
}
public async Task<IEnumerable<string>> GetSystems(CancellationToken token)
{
var query = GetQueryReadOnly();
var entities = await query
.Select(e => e.AutoDrillingSystem ?? string.Empty)
.Distinct()
.ToArrayAsync(token);
var dtos = entities.Order();
return dtos;
}
public async Task<int> InsertRange(IEnumerable<TechMessageDto> dtos, CancellationToken token)
{
var entities = dtos.Select(d => d.Adapt<TechMessage>());
await db.Set<TechMessage>().AddRangeAsync(entities, token);
var result = await db.SaveChangesAsync(token);
return result;
}
}
}