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 GetQueryReadOnly() => db.Set(); public async Task> 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() { Skip = request.Skip, Take = request.Take, Count = entities.Count, Items = entities.Select(e => e.Adapt()) }; return dto; } public async Task 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> 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 InsertRange(IEnumerable dtos, CancellationToken token) { var entities = dtos.Select(d => d.Adapt()); await db.Set().AddRangeAsync(entities, token); var result = await db.SaveChangesAsync(token); return result; } } }