forked from ddrilling/AsbCloudServer
Степанов Дмитрий Александрович
96786b1be7
1. Добавил репозиторий для уведомлений 2. Добавил сервисы для уведомлений
75 lines
2.0 KiB
C#
75 lines
2.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using AsbCloudApp.Data;
|
|
using AsbCloudApp.Repositories;
|
|
using AsbCloudDb;
|
|
using AsbCloudDb.Model;
|
|
using Mapster;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace AsbCloudInfrastructure.Repository;
|
|
|
|
public class NotificationRepository : CrudRepositoryBase<NotificationDto, Notification>, INotificationRepository
|
|
{
|
|
public NotificationRepository(IAsbCloudDbContext context) : base(context)
|
|
{
|
|
}
|
|
|
|
public NotificationRepository(IAsbCloudDbContext context,
|
|
Func<DbSet<Notification>, IQueryable<Notification>> makeQuery) : base(context, makeQuery)
|
|
{
|
|
}
|
|
|
|
public async Task<IEnumerable<NotificationDto>> GetUnsentNotificationsAsync(int idUser,
|
|
int idNotificationTransport,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
var notifications = await dbContext.Notifications
|
|
.Where(x => x.IdUser == idUser &&
|
|
x.IdNotificationTransport == idNotificationTransport &&
|
|
x.SentDateAtUtc == null)
|
|
.Include(x => x.NotificationTransport)
|
|
.Include(x => x.NotificationCategory)
|
|
.ToListAsync(cancellationToken);
|
|
|
|
return notifications.Select(x => x.Adapt<NotificationDto>());
|
|
}
|
|
|
|
public async Task<PaginationContainer<NotificationDto>> GetNotificationsAsync(int? skip,
|
|
int? take,
|
|
int idUser,
|
|
int idNotificationTransport,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
skip ??= 0;
|
|
take ??= 10;
|
|
|
|
var query = dbContext.Notifications
|
|
.Where(x => x.IdNotificationTransport == idNotificationTransport &&
|
|
x.IdUser == idUser &&
|
|
x.SentDateAtUtc != null);
|
|
|
|
var result = new PaginationContainer<NotificationDto>()
|
|
{
|
|
Skip = skip.Value,
|
|
Take = take.Value,
|
|
Count = await query.CountAsync(cancellationToken),
|
|
};
|
|
|
|
if (result.Count == 0)
|
|
return result;
|
|
|
|
result.Items = await query
|
|
.OrderBy(x => x.SentDateAtUtc)
|
|
.SkipTake(skip, take)
|
|
.Include(x => x.NotificationCategory)
|
|
.Include(x => x.NotificationTransport)
|
|
.Select(x => x.Adapt<NotificationDto>())
|
|
.ToListAsync(cancellationToken);
|
|
|
|
return result;
|
|
}
|
|
} |