forked from ddrilling/AsbCloudServer
Степанов Дмитрий Александрович
985c0489d0
1. Изменил сущность уведомления. Добавил состояние уведомления 2. Удалил сущность для доставки уведомлений. 3. Изменение DTO уведомления. 4. Добавил миграцию. 5. Поправил DbContext.
91 lines
2.7 KiB
C#
91 lines
2.7 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 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<IEnumerable<NotificationDto>> GetUnsentNotificationsAsync(int idUser,
|
|
NotificationTransport notificationTransport,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
var notifications = (await GetCacheAsync(cancellationToken))
|
|
.Where(x => x.IdUser == idUser &&
|
|
x.NotificationTransport == notificationTransport.ToString() &&
|
|
x.SentDate == null);
|
|
|
|
return notifications.Select(Convert);
|
|
}
|
|
|
|
public async Task<PaginationContainer<NotificationDto>> GetNotificationsAsync(int idUser,
|
|
NotificationRequest request,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
request.Skip ??= 0;
|
|
request.Take ??= 10;
|
|
|
|
var query = dbContext.Notifications
|
|
.Where(x => x.NotificationTransport == request.NotificationTransport.ToString() &&
|
|
x.IdUser == idUser &&
|
|
x.SentDate != null);
|
|
|
|
var result = new PaginationContainer<NotificationDto>()
|
|
{
|
|
Skip = request.Skip.Value,
|
|
Take = request.Take.Value,
|
|
Count = await query.CountAsync(cancellationToken),
|
|
};
|
|
|
|
if (result.Count == 0)
|
|
return result;
|
|
|
|
result.Items = await query
|
|
.SortBy(request.SortFields)
|
|
.SkipTake(request.Skip, request.Take)
|
|
.Include(x => x.NotificationCategory)
|
|
.Select(x => x.Adapt<NotificationDto>())
|
|
.ToListAsync(cancellationToken);
|
|
|
|
return result;
|
|
}
|
|
|
|
protected override Notification Convert(NotificationDto src)
|
|
{
|
|
var entity = src.Adapt<Notification>();
|
|
|
|
entity.NotificationState = src.NotificationState.ToString();
|
|
entity.NotificationTransport = src.NotificationTransport.ToString();
|
|
|
|
return entity;
|
|
}
|
|
|
|
protected override NotificationDto Convert(Notification src)
|
|
{
|
|
var dto = src.Adapt<NotificationDto>();
|
|
|
|
dto.NotificationState = (NotificationState)Enum.Parse(typeof(NotificationState), src.NotificationState);
|
|
dto.NotificationTransport = (NotificationTransport)Enum.Parse(typeof(NotificationTransport), src.NotificationTransport);
|
|
|
|
return dto;
|
|
}
|
|
} |