Skip to content

Commit

Permalink
Feature(graphql):Implement Notification and newsletter query and muta…
Browse files Browse the repository at this point in the history
…tion resolvers (hngprojects#385)
  • Loading branch information
Godhanded authored Aug 31, 2024
2 parents ad679c1 + ffecfb6 commit f2cd9c6
Show file tree
Hide file tree
Showing 19 changed files with 184 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using Hng.Application.Features.Blogs.Dtos;
using Hng.Application.Features.Blogs.Handlers;
using Hng.Application.Features.Blogs.Queries;
using Hng.Application.Features.SuperAdmin.Dto;
using Hng.Application.Features.UserManagement.Dtos;
using Hng.Domain.Entities;
using Hng.Domain.Enums;
using Hng.Infrastructure.Repository.Interface;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public GetUsersBySearchQueryHandlerShould()
var config = new MapperConfiguration(cfg =>
{
// Add your AutoMapper profiles here
cfg.CreateMap<User, UserDto>();
cfg.CreateMap<User, UserSuperDto>();
});

_mapper = config.CreateMapper();
Expand Down
2 changes: 1 addition & 1 deletion src/Hng.Application/Features/SuperAdmin/Dto/UserDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Hng.Application.Features.SuperAdmin.Dto
{
public class UserDto
public class UserSuperDto
{
[JsonPropertyName("id")]
public Guid Id { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace Hng.Application.Features.SuperAdmin.Handlers
{
public class GetUsersBySearchQueryHandler : IRequestHandler<GetUsersBySearchQuery, PagedListDto<UserDto>>
public class GetUsersBySearchQueryHandler : IRequestHandler<GetUsersBySearchQuery, PagedListDto<UserSuperDto>>
{
private readonly IRepository<User> _userRepository;
private readonly IMapper _mapper;
Expand All @@ -19,15 +19,15 @@ public GetUsersBySearchQueryHandler(IRepository<User> userRepository, IMapper ma
_mapper = mapper;
}

public async Task<PagedListDto<UserDto>> Handle(GetUsersBySearchQuery request, CancellationToken cancellationToken)
public async Task<PagedListDto<UserSuperDto>> Handle(GetUsersBySearchQuery request, CancellationToken cancellationToken)
{
var users = await _userRepository.GetAllAsync();
users = users.Where(v => request.usersQueryParameters.Email == null || v.Email.ToLower().Equals(request.usersQueryParameters.Email.ToLower())).ToList();
users = users.Where(v => request.usersQueryParameters.Firstname == null || v.FirstName.ToLower().Equals(request.usersQueryParameters.Firstname.ToLower())).ToList();
users = users.Where(v => request.usersQueryParameters.Lastname == null || v.LastName.ToLower().Equals(request.usersQueryParameters.Lastname.ToLower())).ToList();

var mappedusers = _mapper.Map<IEnumerable<UserDto>>(users);
var userSearchResult = PagedListDto<UserDto>.ToPagedList(mappedusers, request.usersQueryParameters.Offset, request.usersQueryParameters.Limit);
var mappedusers = _mapper.Map<IEnumerable<UserSuperDto>>(users);
var userSearchResult = PagedListDto<UserSuperDto>.ToPagedList(mappedusers, request.usersQueryParameters.Offset, request.usersQueryParameters.Limit);

return userSearchResult;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class AdminUsersMappingProfile : AutoMapper.Profile
{
public AdminUsersMappingProfile()
{
CreateMap<User, UserDto>().ReverseMap();
CreateMap<User, UserSuperDto>().ReverseMap();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Hng.Application.Features.SuperAdmin.Queries
{
public class GetUsersBySearchQuery : IRequest<PagedListDto<UserDto>>
public class GetUsersBySearchQuery : IRequest<PagedListDto<UserSuperDto>>
{
public GetUsersBySearchQuery(UsersQueryParameters parameters)
{
Expand Down
File renamed without changes.
File renamed without changes.
15 changes: 15 additions & 0 deletions src/Hng.Graphql/Features/Mutations/Mutations.NewsLetter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Hng.Application.Features.NewsLetterSubscription.Commands;
using Hng.Application.Features.NewsLetterSubscription.Dtos;
using MediatR;
using Microsoft.AspNetCore.Mvc;

namespace Hng.Graphql
{
public partial class Mutations
{
public async Task<NewsLetterSubscriptionDto> RegisterNewsLetterSubscriber(NewsLetterSubscriptionDto subscriber, [FromServices] IMediator mediator)
{
return await mediator.Send(new AddSubscriberCommand(subscriber));
}
}
}
69 changes: 69 additions & 0 deletions src/Hng.Graphql/Features/Mutations/Mutations.Notification.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using Hng.Application.Features.Notifications.Commands;
using Hng.Application.Features.Notifications.Dtos;
using HotChocolate.Authorization;
using MediatR;
using Microsoft.AspNetCore.Mvc;

namespace Hng.Graphql.Features.Mutations
{
public partial class Mutations
{
/// <summary>
/// Notification Settings - Create User notification
/// </summary>
[Authorize]
public async Task<NotificationResult> CreateNotification(CreateNotificationDto command, [FromServices] IMediator mediator)
{
var createCommand = new CreateNotificationCommand(command);
return await mediator.Send(createCommand);
}


/// <summary>
/// Mark a single notification as read
/// </summary>
/// <param name="notification_id">The ID of the notification</param>
/// <param name="request">The request body containing is_read flag</param>
/// <returns>Response indicating the result of the operation</returns>
[Authorize]
public async Task<NotificationDto> MarkNotificationAsRead(Guid notification_id, UpdateNotificationDto request, [FromServices] IMediator mediator)
{
var command = new UpdateNotificationCommand(notification_id, request.IsRead);
return await mediator.Send(command);
}

/// <summary>
/// Mark all notification as read
/// </summary>
/// <param name="request">The request body containing is_read flag</param>
/// <returns>Response indicating the result of the operation</returns>
[Authorize]
public async Task<List<NotificationDto>> MarkAllNotificationAsRead(UpdateNotificationDto request, [FromServices] IMediator mediator)
{
var command = new MarkAllCommand(request.IsRead);
return await mediator.Send(command);

}

/// <summary>
/// Clear all user's notifications (Read or Unread)
/// </summary>
[Authorize]
public async Task<bool> DeleteAllNotifications([FromServices] IMediator mediator)
{
var command = new DeleteAllNotificationsCommand();
return await mediator.Send(command);

}

/// <summary>
/// Clear notification (Read or Unread)
/// </summary>
[Authorize]
public async Task<bool> DeleteNotificationById(Guid notification_id, [FromServices] IMediator mediator)
{
var command = new DeleteNotificationByIdCommand(notification_id);
return await mediator.Send(command);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Hng.Application.Features.Notifications.Commands;
using Hng.Application.Features.Notifications.Dtos;
using HotChocolate.Authorization;
using MediatR;
using Microsoft.AspNetCore.Mvc;

namespace Hng.Graphql
{
public partial class Mutations
{
/// <summary>
/// Notification Settings - User notification settings
/// </summary>
[Authorize]
public async Task<NotificationSettingsDto> CreateNotificationSettings([FromBody] CreateNotificationSettingsDto command, [FromServices] IMediator mediator)
{
{
var createCommand = new CreateNotificationSettingsCommand(command);
return await mediator.Send(createCommand);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,12 @@ namespace Hng.Graphql
{
public partial class Mutations
{
//[Authorize]
//public async Task<SubscribeFreePlanResponse> SubscribeFreePlan(SubscribeFreePlan command, [FromServices] IMediator mediator)
//{
// //var command = new SubscribeFreePlan();
// // return await mediator.Send(command);


// // return await mediator.Send(command);
//}
[Authorize]
public async Task<SubscribeFreePlanResponse> SubscribeFreePlan(SubscribeFreePlan command, [FromServices] IMediator mediator)
{
var response = await mediator.Send(command);
return response.Value;
}

[Authorize]
public async Task<SubscriptionDto> ActivateSubscription(Guid subscriptionId, [FromServices] IMediator mediator)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
using Hng.Application.Features.SuperAdmin.Dto;
using Hng.Application.Features.SuperAdmin.Queries;
using Hng.Application.Shared.Dtos;
using HotChocolate.Authorization;
using MediatR;
using Microsoft.AspNetCore.Mvc;

namespace Hng.Graphql
{
public partial class Queries
{
public async Task<PagedListDto<UserDto>> GetUsersBySearch(UsersQueryParameters parameters, [FromServices] IMediator mediator)
[Authorize]
public async Task<PagedListDto<UserSuperDto>> GetUsersBySearch(UsersQueryParameters parameters, [FromServices] IMediator mediator)
{
var users = new GetUsersBySearchQuery(parameters);
return await mediator.Send(users);
Expand Down
File renamed without changes.
File renamed without changes.
31 changes: 31 additions & 0 deletions src/Hng.Graphql/Features/Queries/Queries.Notification.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Hng.Application.Features.Notifications.Dtos;
using Hng.Application.Features.Notifications.Queries;
using HotChocolate.Authorization;
using MediatR;
using Microsoft.AspNetCore.Mvc;

namespace Hng.Graphql.Features.Queries
{
public partial class Queries
{
/// <summary>
/// Retrieve user's notifications (Read + Unread)
/// </summary>
[Authorize]
public async Task<GetNotificationsResponseDto> GetAllNotifications([FromServices] IMediator mediator)
{
var query = new GetAllNotificationsQuery();
return await mediator.Send(query);
}

/// <summary>
/// Retrieve user's notifications (Read or Unread)
/// </summary>
[Authorize]
public async Task<GetNotificationsResponseDto> GetNotifications(bool? is_read, [FromServices] IMediator mediator)
{
var query = new GetNotificationsQuery(is_read);
return await mediator.Send(query);
}
}
}
27 changes: 27 additions & 0 deletions src/Hng.Graphql/Features/Queries/Queries.NotificationSetting.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Hng.Application.Features.Notifications.Dtos;
using Hng.Application.Features.Notifications.Queries;
using HotChocolate.Authorization;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Hng.Graphql
{
public partial class Queries
{
/// <summary>
/// Get Notification Settings by User ID
/// </summary>
[Authorize]
public async Task<NotificationSettingsDto> GetNotificationSettings(Guid user_id, [FromServices] IMediator mediator)
{
var query = new GetNotificationSettingsQuery(user_id);
return await mediator.Send(query);

}
}
}
File renamed without changes.
2 changes: 1 addition & 1 deletion src/Hng.Web/Controllers/AdminController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public AdminController(IMediator mediator)
public async Task<ActionResult> GetUsersBySearch([FromQuery] UsersQueryParameters parameters)
{
var users = await _mediator.Send(new GetUsersBySearchQuery(parameters));
return Ok(new PaginatedResponseDto<PagedListDto<UserDto>> { Data = users, Metadata = users.MetaData });
return Ok(new PaginatedResponseDto<PagedListDto<UserSuperDto>> { Data = users, Metadata = users.MetaData });
}
}
}

0 comments on commit f2cd9c6

Please sign in to comment.