33// See README in the root project for more information.
44// ============================================================================
55
6- using System . Text . Json ;
76using Microsoft . AspNetCore . Http ;
87using Microsoft . EntityFrameworkCore ;
98using NXTBackend . API . Core . Services . Interface ;
1211using NXTBackend . API . Domain . Entities ;
1312using NXTBackend . API . Domain . Enums ;
1413using NXTBackend . API . Infrastructure . Database ;
15- using NXTBackend . API . Models ;
1614
1715namespace NXTBackend . API . Core . Services . Implementation ;
1816
@@ -24,27 +22,36 @@ public NotificationService(DatabaseContext ctx) : base(ctx)
2422 {
2523 DefineFilter < bool > ( "read" , ( q , read ) => q . Where ( n => read ? n . ReadAt != null : n . ReadAt == null ) ) ;
2624 DefineFilter < Guid > ( "user_id" , ( q , userId ) => q . Where ( n => n . NotifiableId == userId ) ) ;
27- DefineFilter < NotificationState > ( "state" , ( q , state ) => q . Where ( n => n . State == state ) ) ;
28- DefineFilter < NotificationKind > ( "kind" , ( q , kind ) => q . Where ( n => ( n . Descriptor & kind ) != 0 ) ) ;
29- DefineFilter < NotificationKind > ( "not[kind]" , ( q , kind ) => q . Where ( n => ( n . Descriptor & kind ) == 0 ) ) ;
25+ DefineFilter < NotificationKind > ( "kind" , ( q , kind ) =>
26+ kind == NotificationKind . None
27+ ? q // Skip the filter
28+ : q . Where ( n => ( n . Descriptor & kind ) != 0 ) ) ;
29+
30+ DefineFilter < NotificationKind > ( "not[kind]" , ( q , kind ) =>
31+ kind == NotificationKind . None
32+ ? q // Skip the filter
33+ : q . Where ( n => ( n . Descriptor & kind ) == 0 ) ) ;
3034 }
3135
32- public override async Task < PaginatedList < Notification > > GetAllAsync ( PaginationParams pagination , SortingParams sorting , FilterDictionary ? filters = null )
36+ public override async Task < PaginatedList < Notification > > GetAllAsync ( PaginationParams pagination ,
37+ SortingParams sorting , FilterDictionary ? filters = null )
3338 {
3439 var query = ApplyFilters ( _dbSet . AsQueryable ( ) , filters ) ;
3540 query = SortedList < Notification > . Apply ( query , sorting ) ;
3641 return await PaginatedList < Notification > . CreateAsync ( query , pagination . Page , pagination . Size ) ;
3742 }
3843
39- /// <inheritdoc/>
44+ /// <inheritdoc />
4045 public async Task MarkAsReadAsync ( Guid userId , IEnumerable < Guid > ? notificationIds = null )
4146 {
4247 if ( notificationIds is not null && notificationIds . Any ( ) )
4348 {
4449 var idList = notificationIds . ToList ( ) ;
45- bool hasUnauthorizedNotifications = await _context . Notifications . AnyAsync ( n => idList . Contains ( n . Id ) && n . NotifiableId != userId ) ;
50+ var hasUnauthorizedNotifications =
51+ await _context . Notifications . AnyAsync ( n => idList . Contains ( n . Id ) && n . NotifiableId != userId ) ;
4652 if ( hasUnauthorizedNotifications )
47- throw new ServiceException ( StatusCodes . Status403Forbidden , "One or more notification IDs do not belong to the current user" ) ;
53+ throw new ServiceException ( StatusCodes . Status403Forbidden ,
54+ "One or more notification IDs do not belong to the current user" ) ;
4855 }
4956
5057 var updateQuery = _context . Notifications . Where ( un => un . NotifiableId == userId ) ;
@@ -53,17 +60,18 @@ public async Task MarkAsReadAsync(Guid userId, IEnumerable<Guid>? notificationId
5360 await updateQuery . ExecuteUpdateAsync ( s => s . SetProperty ( un => un . ReadAt , DateTimeOffset . UtcNow ) ) ;
5461 }
5562
56- /// <inheritdoc/>
63+ /// <inheritdoc />
5764 public async Task MarkAsUnreadAsync ( Guid userId , IEnumerable < Guid > ? notificationIds = null )
5865 {
5966 if ( notificationIds is not null && notificationIds . Any ( ) )
6067 {
6168 var idList = notificationIds . ToList ( ) ;
62- bool hasUnauthorizedNotifications = await _context . Notifications
69+ var hasUnauthorizedNotifications = await _context . Notifications
6370 . AnyAsync ( n => idList . Contains ( n . Id ) && n . NotifiableId != userId ) ;
6471
6572 if ( hasUnauthorizedNotifications )
66- throw new ServiceException ( StatusCodes . Status403Forbidden , "One or more notification IDs do not belong to the current user" ) ;
73+ throw new ServiceException ( StatusCodes . Status403Forbidden ,
74+ "One or more notification IDs do not belong to the current user" ) ;
6775 }
6876
6977 var updateQuery = _context . Notifications . Where ( un => un . NotifiableId == userId ) ;
0 commit comments