2023-07-10 16:56:55 +05:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using AsbCloudApp.Data;
|
|
|
|
using AsbCloudApp.Repositories;
|
2023-07-11 18:57:25 +05:00
|
|
|
using AsbCloudApp.Requests;
|
2023-07-10 16:56:55 +05:00
|
|
|
using AsbCloudDb;
|
|
|
|
using AsbCloudDb.Model;
|
|
|
|
using Mapster;
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2023-07-11 18:57:25 +05:00
|
|
|
using Microsoft.Extensions.Caching.Memory;
|
2023-07-10 16:56:55 +05:00
|
|
|
|
|
|
|
namespace AsbCloudInfrastructure.Repository;
|
|
|
|
|
2023-07-11 18:57:25 +05:00
|
|
|
public class NotificationRepository : CrudCacheRepositoryBase<NotificationDto, Notification>, INotificationRepository
|
2023-07-10 16:56:55 +05:00
|
|
|
{
|
2023-07-11 18:57:25 +05:00
|
|
|
private static IQueryable<Notification> MakeQueryNotification(DbSet<Notification> dbSet)
|
|
|
|
=> dbSet.AsNoTracking()
|
|
|
|
.Include(n => n.NotificationCategory);
|
2023-07-10 16:56:55 +05:00
|
|
|
|
2023-07-12 13:31:55 +05:00
|
|
|
public NotificationRepository(IAsbCloudDbContext dbContext, IMemoryCache memoryCache)
|
2023-07-11 18:57:25 +05:00
|
|
|
: base(dbContext, memoryCache, MakeQueryNotification)
|
2023-07-10 16:56:55 +05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task<IEnumerable<NotificationDto>> GetUnsentNotificationsAsync(int idUser,
|
2023-07-11 18:57:25 +05:00
|
|
|
NotificationTransport notificationTransport,
|
2023-07-10 16:56:55 +05:00
|
|
|
CancellationToken cancellationToken)
|
|
|
|
{
|
2023-07-11 18:57:25 +05:00
|
|
|
var notifications = (await GetCacheAsync(cancellationToken))
|
2023-07-10 16:56:55 +05:00
|
|
|
.Where(x => x.IdUser == idUser &&
|
2023-07-11 18:57:25 +05:00
|
|
|
x.NotificationTransport == notificationTransport.ToString() &&
|
|
|
|
x.SentDate == null);
|
2023-07-10 16:56:55 +05:00
|
|
|
|
2023-07-11 18:57:25 +05:00
|
|
|
return notifications.Select(Convert);
|
2023-07-10 16:56:55 +05:00
|
|
|
}
|
|
|
|
|
2023-07-11 18:57:25 +05:00
|
|
|
public async Task<PaginationContainer<NotificationDto>> GetNotificationsAsync(int idUser,
|
|
|
|
NotificationRequest request,
|
2023-07-10 16:56:55 +05:00
|
|
|
CancellationToken cancellationToken)
|
|
|
|
{
|
2023-07-11 18:57:25 +05:00
|
|
|
request.Skip ??= 0;
|
|
|
|
request.Take ??= 10;
|
2023-07-10 16:56:55 +05:00
|
|
|
|
|
|
|
var query = dbContext.Notifications
|
2023-07-11 18:57:25 +05:00
|
|
|
.Where(x => x.NotificationTransport == request.NotificationTransport.ToString() &&
|
2023-07-10 16:56:55 +05:00
|
|
|
x.IdUser == idUser &&
|
2023-07-11 18:57:25 +05:00
|
|
|
x.SentDate != null);
|
2023-07-10 16:56:55 +05:00
|
|
|
|
|
|
|
var result = new PaginationContainer<NotificationDto>()
|
|
|
|
{
|
2023-07-11 18:57:25 +05:00
|
|
|
Skip = request.Skip.Value,
|
|
|
|
Take = request.Take.Value,
|
2023-07-10 16:56:55 +05:00
|
|
|
Count = await query.CountAsync(cancellationToken),
|
|
|
|
};
|
|
|
|
|
|
|
|
if (result.Count == 0)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
result.Items = await query
|
2023-07-12 13:31:55 +05:00
|
|
|
.OrderBy(x => x.SentDate)
|
2023-07-11 18:57:25 +05:00
|
|
|
.SortBy(request.SortFields)
|
|
|
|
.SkipTake(request.Skip, request.Take)
|
2023-07-10 16:56:55 +05:00
|
|
|
.Include(x => x.NotificationCategory)
|
|
|
|
.Select(x => x.Adapt<NotificationDto>())
|
|
|
|
.ToListAsync(cancellationToken);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2023-07-11 18:57:25 +05:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2023-07-10 16:56:55 +05:00
|
|
|
}
|