Browse Source

feat: Add user notification query conditions

pull/1445/head
colin 10 hours ago
parent
commit
8fcb105be2
  1. 5
      aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.Application.Contracts/LINGYUN/Abp/Notifications/Dto/UserNotificationGetByPagedDto.cs
  2. 38
      aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.Application/LINGYUN/Abp/Notifications/MyNotificationAppService.cs
  3. 81
      aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.EntityFrameworkCore/LINGYUN/Abp/Notifications/EntityFrameworkCore/EfCoreUserNotificationRepository.cs

5
aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.Application.Contracts/LINGYUN/Abp/Notifications/Dto/UserNotificationGetByPagedDto.cs

@ -1,4 +1,5 @@
using System.ComponentModel;
using System;
using System.ComponentModel;
using Volo.Abp.Application.Dtos;
namespace LINGYUN.Abp.Notifications;
@ -9,4 +10,6 @@ public class UserNotificationGetByPagedDto : PagedAndSortedResultRequestDto
[DisplayName("Notifications:State")]
public NotificationReadState? ReadState { get; set; }
public DateTime? BeginCreationTime { get; set; }
public DateTime? EndCreationTime { get; set; }
}

38
aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.Application/LINGYUN/Abp/Notifications/MyNotificationAppService.cs

@ -1,5 +1,8 @@
using Microsoft.AspNetCore.Authorization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Users;
@ -56,17 +59,34 @@ public class MyNotificationAppService : AbpNotificationsApplicationServiceBase,
public async virtual Task<PagedResultDto<UserNotificationDto>> GetListAsync(UserNotificationGetByPagedDto input)
{
var totalCount = await UserNotificationRepository
.GetCountAsync(
CurrentUser.GetId(),
input.Filter,
input.ReadState);
Expression<Func<UserNotificationInfo, bool>> expression = _ => true;
if (input.ReadState.HasValue)
{
expression = expression.And(x => x.State == input.ReadState);
}
if (input.BeginCreationTime.HasValue)
{
expression = expression.And(x => x.CreationTime >= input.BeginCreationTime);
}
if (input.EndCreationTime.HasValue)
{
expression = expression.And(x => x.CreationTime <= input.EndCreationTime);
}
if (!input.Filter.IsNullOrWhiteSpace())
{
expression = expression.And(x =>
x.Name.Contains(input.Filter) ||
x.NotificationTypeName.Contains(input.Filter));
}
var specification = new Volo.Abp.Specifications.ExpressionSpecification<UserNotificationInfo>(expression);
var userId = CurrentUser.GetId();
var totalCount = await UserNotificationRepository.GetCountAsync(userId, specification);
var notifications = await UserNotificationRepository
.GetListAsync(
CurrentUser.GetId(),
input.Filter, input.Sorting,
input.ReadState, input.SkipCount, input.MaxResultCount);
.GetListAsync(userId, specification, input.Sorting, input.SkipCount, input.MaxResultCount);
return new PagedResultDto<UserNotificationDto>(totalCount,
ObjectMapper.Map<List<UserNotificationInfo>, List<UserNotificationDto>>(notifications));

81
aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.EntityFrameworkCore/LINGYUN/Abp/Notifications/EntityFrameworkCore/EfCoreUserNotificationRepository.cs

@ -1,13 +1,16 @@
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading;
using System.Threading.Tasks;
using System.Xml;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Repositories.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.Specifications;
namespace LINGYUN.Abp.Notifications.EntityFrameworkCore;
@ -151,6 +154,35 @@ public class EfCoreUserNotificationRepository : EfCoreRepository<INotificationsD
.CountAsync(GetCancellationToken(cancellationToken));
}
public async virtual Task<int> GetCountAsync(
Guid userId,
ISpecification<UserNotificationInfo> specification,
CancellationToken cancellationToken = default)
{
var dbContext = await GetDbContextAsync();
var notifilerQuery = from un in dbContext.Set<UserNotification>()
join n in dbContext.Set<Notification>()
on un.NotificationId equals n.NotificationId
where un.UserId == userId
select new UserNotificationInfo
{
NotificationId = n.NotificationId,
TenantId = n.TenantId,
Name = n.NotificationName,
ExtraProperties = n.ExtraProperties,
CreationTime = n.CreationTime,
NotificationTypeName = n.NotificationTypeName,
Severity = n.Severity,
State = un.ReadStatus,
Type = n.Type,
ContentType = n.ContentType
};
return await notifilerQuery
.Where(specification.ToExpression())
.CountAsync(GetCancellationToken(cancellationToken));
}
public async virtual Task<List<UserNotificationInfo>> GetListAsync(
Guid userId,
string filter = "",
@ -165,14 +197,6 @@ public class EfCoreUserNotificationRepository : EfCoreRepository<INotificationsD
sorting = $"{nameof(Notification.CreationTime)} DESC";
}
var dbContext = await GetDbContextAsync();
//var userNotifilerQuery = dbContext.Set<UserNotification>()
// .Where(x => x.UserId == userId)
// .WhereIf(readState.HasValue, x => x.ReadStatus == readState.Value);
//var notificationQuery = dbContext.Set<Notification>()
// .WhereIf(!filter.IsNullOrWhiteSpace(), nf =>
// nf.NotificationName.Contains(filter) ||
// nf.NotificationTypeName.Contains(filter));
var notifilerQuery = from un in dbContext.Set<UserNotification>()
join n in dbContext.Set<Notification>()
@ -202,4 +226,45 @@ public class EfCoreUserNotificationRepository : EfCoreRepository<INotificationsD
.AsNoTracking()
.ToListAsync(GetCancellationToken(cancellationToken));
}
public async virtual Task<List<UserNotificationInfo>> GetListAsync(
Guid userId,
ISpecification<UserNotificationInfo> specification,
string sorting = nameof(Notification.CreationTime),
int skipCount = 0,
int maxResultCount = 10,
CancellationToken cancellationToken = default)
{
if (sorting.IsNullOrWhiteSpace())
{
sorting = $"{nameof(Notification.CreationTime)} DESC";
}
var dbContext = await GetDbContextAsync();
var notifilerQuery = from un in dbContext.Set<UserNotification>()
join n in dbContext.Set<Notification>()
on un.NotificationId equals n.NotificationId
where un.UserId == userId
select new UserNotificationInfo
{
NotificationId = n.NotificationId,
TenantId = n.TenantId,
Name = n.NotificationName,
ExtraProperties = n.ExtraProperties,
CreationTime = n.CreationTime,
NotificationTypeName = n.NotificationTypeName,
Severity = n.Severity,
State = un.ReadStatus,
Type = n.Type,
ContentType = n.ContentType,
Id = un.Id,
};
return await notifilerQuery
.Where(specification.ToExpression())
.OrderBy(sorting)
.PageBy(skipCount, maxResultCount)
.AsNoTracking()
.ToListAsync(GetCancellationToken(cancellationToken));
}
}

Loading…
Cancel
Save