forked from ddrilling/AsbCloudServer
Степанов Дмитрий Александрович
b1d3da5f80
1. Обновил классы модели и dto уведомления. 2. Удалил лишние сервисы. 3. Накатил новую миграцию. 4. Поправил репозиторий. 5. Поправил сервис уведомлений.
72 lines
1.9 KiB
C#
72 lines
1.9 KiB
C#
using System.Linq;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using AsbCloudApp.Data;
|
|
using AsbCloudApp.Repositories;
|
|
using AsbCloudApp.Requests;
|
|
using AsbCloudDb;
|
|
using AsbCloudDb.Model;
|
|
using Mapster;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Caching.Memory;
|
|
|
|
namespace AsbCloudInfrastructure.Repository;
|
|
|
|
public class NotificationRepository : CrudCacheRepositoryBase<NotificationDto, Notification>, INotificationRepository
|
|
{
|
|
private static IQueryable<Notification> MakeQueryNotification(DbSet<Notification> dbSet)
|
|
=> dbSet.AsNoTracking()
|
|
.Include(n => n.NotificationCategory);
|
|
|
|
public NotificationRepository(IAsbCloudDbContext dbContext, IMemoryCache memoryCache)
|
|
: base(dbContext, memoryCache, MakeQueryNotification)
|
|
{
|
|
}
|
|
|
|
public async Task<PaginationContainer<NotificationDto>> GetNotificationsAsync(int idUser,
|
|
NotificationRequest request,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
var skip = request.Skip ?? 0;
|
|
var take = request.Take ?? 10;
|
|
|
|
var query = BuildQuery(idUser, request);
|
|
|
|
var result = new PaginationContainer<NotificationDto>()
|
|
{
|
|
Skip = skip,
|
|
Take = take,
|
|
Count = await query.CountAsync(cancellationToken),
|
|
};
|
|
|
|
if (result.Count == 0)
|
|
return result;
|
|
|
|
result.Items = await query
|
|
.SortBy(request.SortFields)
|
|
.Skip(skip)
|
|
.Take(take)
|
|
.Include(x => x.NotificationCategory)
|
|
.Select(x => x.Adapt<NotificationDto>())
|
|
.ToListAsync(cancellationToken);
|
|
|
|
return result;
|
|
}
|
|
|
|
private IQueryable<Notification> BuildQuery(int? idUser,
|
|
NotificationRequest request)
|
|
{
|
|
var query = dbContext.Notifications.AsQueryable();
|
|
|
|
if (!request.SentDate.HasValue)
|
|
query = query.Where(n => n.SentDate == null);
|
|
|
|
if (idUser.HasValue)
|
|
query = query.Where(n => n.IdUser == idUser);
|
|
|
|
if (request.IdTransportType.HasValue)
|
|
query = query.Where(n => n.IdTransportType == request.IdTransportType);
|
|
|
|
return query;
|
|
}
|
|
} |