2023-07-14 11:40:57 +05:00
|
|
|
using System.Collections.Generic;
|
2023-07-17 11:48:52 +05:00
|
|
|
using System.ComponentModel.DataAnnotations;
|
2023-07-10 16:56:55 +05:00
|
|
|
using System.Linq;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using AsbCloudApp.Data;
|
2023-07-14 11:40:57 +05:00
|
|
|
using AsbCloudApp.Data.SAUB;
|
2023-07-10 16:56:55 +05:00
|
|
|
using AsbCloudApp.Repositories;
|
2023-07-11 18:57:25 +05:00
|
|
|
using AsbCloudApp.Requests;
|
2023-07-14 11:40:57 +05:00
|
|
|
using AsbCloudApp.Services;
|
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)
|
2023-07-14 11:40:57 +05:00
|
|
|
=> dbSet.Include(n => n.NotificationCategory)
|
|
|
|
.AsNoTracking();
|
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
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-07-14 11:40:57 +05:00
|
|
|
public async Task<int> UpdateRangeAsync(IEnumerable<NotificationDto> notifications, CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
if (!notifications.Any())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
var ids = notifications.Select(d => d.Id).ToArray();
|
|
|
|
|
|
|
|
var existingEntities = await dbSet
|
|
|
|
.Where(d => ids.Contains(d.Id))
|
|
|
|
.AsNoTracking()
|
|
|
|
.Select(d => d.Id)
|
|
|
|
.ToArrayAsync(cancellationToken);
|
|
|
|
|
|
|
|
if (ids.Length > existingEntities.Length)
|
|
|
|
return ICrudRepository<SetpointsRequestDto>.ErrorIdNotFound;
|
|
|
|
|
|
|
|
var entities = notifications.Select(Convert);
|
|
|
|
|
|
|
|
dbContext.Notifications.UpdateRange(entities);
|
|
|
|
|
2023-07-17 11:48:52 +05:00
|
|
|
var result = await dbContext.SaveChangesAsync(cancellationToken);
|
|
|
|
DropCache();
|
|
|
|
return result;
|
2023-07-14 11:40:57 +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-13 14:44:40 +05:00
|
|
|
var skip = request.Skip ?? 0;
|
|
|
|
var take = request.Take ?? 10;
|
2023-07-10 16:56:55 +05:00
|
|
|
|
2023-07-13 14:44:40 +05:00
|
|
|
var query = BuildQuery(idUser, request);
|
2023-07-10 16:56:55 +05:00
|
|
|
|
|
|
|
var result = new PaginationContainer<NotificationDto>()
|
|
|
|
{
|
2023-07-13 14:44:40 +05:00
|
|
|
Skip = skip,
|
|
|
|
Take = take,
|
2023-07-10 16:56:55 +05:00
|
|
|
Count = await query.CountAsync(cancellationToken),
|
|
|
|
};
|
|
|
|
|
2023-07-17 11:48:52 +05:00
|
|
|
if (result.Count < skip)
|
2023-07-10 16:56:55 +05:00
|
|
|
return result;
|
2023-07-18 14:41:20 +05:00
|
|
|
|
2023-07-10 16:56:55 +05:00
|
|
|
result.Items = await query
|
2023-07-11 18:57:25 +05:00
|
|
|
.SortBy(request.SortFields)
|
2023-07-13 14:44:40 +05:00
|
|
|
.Skip(skip)
|
|
|
|
.Take(take)
|
2023-07-14 11:40:57 +05:00
|
|
|
.AsNoTracking()
|
2023-07-10 16:56:55 +05:00
|
|
|
.Select(x => x.Adapt<NotificationDto>())
|
2023-07-18 14:41:20 +05:00
|
|
|
.ToArrayAsync(cancellationToken);
|
2023-07-10 16:56:55 +05:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2023-07-11 18:57:25 +05:00
|
|
|
|
2023-07-14 11:40:57 +05:00
|
|
|
private IQueryable<Notification> BuildQuery(int idUser,
|
2023-07-13 14:44:40 +05:00
|
|
|
NotificationRequest request)
|
2023-07-11 18:57:25 +05:00
|
|
|
{
|
2023-07-14 11:40:57 +05:00
|
|
|
var query = dbContext.Notifications
|
|
|
|
.Include(x => x.NotificationCategory)
|
2023-07-17 11:48:52 +05:00
|
|
|
.Where(n => n.IdUser == idUser);
|
2023-07-11 18:57:25 +05:00
|
|
|
|
2023-07-17 11:48:52 +05:00
|
|
|
if (request.IsSent.HasValue)
|
|
|
|
{
|
|
|
|
if(request.IsSent.Value)
|
|
|
|
query = query.Where(n => n.SentDate != null);
|
|
|
|
else
|
|
|
|
query = query.Where(n => n.SentDate == null);
|
|
|
|
}
|
2023-07-11 18:57:25 +05:00
|
|
|
|
2023-07-13 14:44:40 +05:00
|
|
|
if (request.IdTransportType.HasValue)
|
|
|
|
query = query.Where(n => n.IdTransportType == request.IdTransportType);
|
2023-07-11 18:57:25 +05:00
|
|
|
|
2023-07-13 14:44:40 +05:00
|
|
|
return query;
|
2023-07-11 18:57:25 +05:00
|
|
|
}
|
2023-07-10 16:56:55 +05:00
|
|
|
}
|