From 16e537caa6169d0740901c2499452fae09d99184 Mon Sep 17 00:00:00 2001 From: Andrii Voznesenskyi Date: Fri, 31 May 2024 12:44:03 +0200 Subject: [PATCH] (#125) (#126) (#229) udpate the modal razor component, update the invitation notification --- .../Handlers/NotificationCreatedHandler.cs | 4 +- .../Handlers/CreateNotificationHandler.cs | 30 ++++---- .../Dialogs/InviteFriendsToEventDialog.razor | 68 ++++++++++++++----- 3 files changed, 69 insertions(+), 33 deletions(-) diff --git a/MiniSpace.Services.Email/src/MiniSpace.Services.Email.Application/Events/External/Handlers/NotificationCreatedHandler.cs b/MiniSpace.Services.Email/src/MiniSpace.Services.Email.Application/Events/External/Handlers/NotificationCreatedHandler.cs index 6b76f2fa8..223284423 100644 --- a/MiniSpace.Services.Email/src/MiniSpace.Services.Email.Application/Events/External/Handlers/NotificationCreatedHandler.cs +++ b/MiniSpace.Services.Email/src/MiniSpace.Services.Email.Application/Events/External/Handlers/NotificationCreatedHandler.cs @@ -25,12 +25,14 @@ public class NotificationCreatedHandler : IEventHandler IStudentsServiceClient studentsServiceClient, IMessageBroker messageBroker, IEmailService emailService, - IStudentEmailsRepository studentEmailsRepository) + IStudentEmailsRepository studentEmailsRepository, + ILogger logger) { _studentsServiceClient = studentsServiceClient; _messageBroker = messageBroker; _emailService = emailService; _studentEmailsRepository = studentEmailsRepository; + _logger = logger; } public async Task HandleAsync(NotificationCreated @event, CancellationToken cancellationToken) diff --git a/MiniSpace.Services.Notifications/src/MiniSpace.Services.Notifications.Application/Commands/Handlers/CreateNotificationHandler.cs b/MiniSpace.Services.Notifications/src/MiniSpace.Services.Notifications.Application/Commands/Handlers/CreateNotificationHandler.cs index f6ab93042..552039180 100644 --- a/MiniSpace.Services.Notifications/src/MiniSpace.Services.Notifications.Application/Commands/Handlers/CreateNotificationHandler.cs +++ b/MiniSpace.Services.Notifications/src/MiniSpace.Services.Notifications.Application/Commands/Handlers/CreateNotificationHandler.cs @@ -5,6 +5,7 @@ using MiniSpace.Services.Notifications.Application.Services.Clients; using MiniSpace.Services.Notifications.Application.Dto; using MiniSpace.Services.Notifications.Application.Events.External; +using System; using System.Text.Json; namespace MiniSpace.Services.Notifications.Application.Commands.Handlers @@ -14,36 +15,39 @@ public class CreateNotificationHandler : ICommandHandler private readonly IStudentNotificationsRepository _studentNotificationsRepository; private readonly IFriendsServiceClient _friendsServiceClient; private readonly IEventsServiceClient _eventsServiceClient; - private readonly IEventMapper _eventMapper; private readonly IMessageBroker _messageBroker; public CreateNotificationHandler( IStudentNotificationsRepository studentNotificationsRepository, IFriendsServiceClient friendsServiceClient, IEventsServiceClient eventsServiceClient, - IEventMapper eventMapper, IMessageBroker messageBroker ) { _studentNotificationsRepository = studentNotificationsRepository; _friendsServiceClient = friendsServiceClient; - _eventsServiceClient = eventsServiceClient; - _eventMapper = eventMapper; + _eventsServiceClient = eventsServiceClient; _messageBroker = messageBroker; } public async Task HandleAsync(CreateNotification command, CancellationToken cancellationToken = default) { - var commandJson = JsonSerializer.Serialize(command, new JsonSerializerOptions { WriteIndented = true }); - Console.WriteLine($"Received command: {commandJson}"); var eventDetails = await _eventsServiceClient.GetEventAsync(command.EventId); - + var eventLink = $"https://minispace.itsharppro.com/events/{eventDetails.Id}"; + foreach (var userId in command.StudentIds) { + var notificationNotDetailed = $"

You have been invited to the event '{eventDetails.Name}' " + + $"Learn more: Click here.

"; + var notificationMessage = $"

You have been invited to the event '{eventDetails.Name}' organized by {eventDetails.Organizer.Name}. " + + $"This event will take place from {eventDetails.StartDate:yyyy-MM-dd} to {eventDetails.EndDate:yyyy-MM-dd} at {eventDetails.Location.Street}, {eventDetails.Location.City}. " + + $"The event has a capacity of {eventDetails.Capacity} and a registration fee of ${eventDetails.Fee}. " + + $"Learn more: Click here.

"; + var notification = new Notification( command.NotificationId, userId, - command.Message, + notificationMessage, NotificationStatus.Unread, DateTime.UtcNow, null, @@ -60,21 +64,19 @@ public async Task HandleAsync(CreateNotification command, CancellationToken canc studentNotifications.AddNotification(notification); - var notificationCreatedEvent = new NotificationCreated( + var notificationCreatedEvent = new NotificationCreated( notificationId: notification.NotificationId, userId: userId, - message: notification.Message, + message: notificationNotDetailed, createdAt: notification.CreatedAt, eventType: notification.EventType.ToString(), relatedEntityId: eventDetails.Id, - details: notification.Message + details: notificationMessage ); await _messageBroker.PublishAsync(notificationCreatedEvent); - - var events = _eventMapper.MapAll(notification.Events); - await _messageBroker.PublishAsync(events.ToArray()); + await _studentNotificationsRepository.UpdateAsync(studentNotifications); } } } diff --git a/MiniSpace.Web/src/MiniSpace.Web/Pages/Events/Dialogs/InviteFriendsToEventDialog.razor b/MiniSpace.Web/src/MiniSpace.Web/Pages/Events/Dialogs/InviteFriendsToEventDialog.razor index 3d80811b4..57f23ac54 100644 --- a/MiniSpace.Web/src/MiniSpace.Web/Pages/Events/Dialogs/InviteFriendsToEventDialog.razor +++ b/MiniSpace.Web/src/MiniSpace.Web/Pages/Events/Dialogs/InviteFriendsToEventDialog.razor @@ -13,22 +13,35 @@ @inject IMediaFilesService MediaFilesService @inject INotificationsService NotificationService - - - - FriendTemplate(friend)) Style="width:100%; height:300px;" Multiple="true"/> - - - +
+ + + + + @foreach (var friend in friends) + { + + + + + Friend + @($"{friend.StudentDetails.FirstName} {friend.StudentDetails.LastName}") + + + + + } +
@code { [Parameter] public Guid EventId { get; set; } - private List friends = new List(); private Dictionary images = new Dictionary(); private List selectedFriends = new List(); + private bool selectAll = false; protected override async Task OnInitializedAsync() { @@ -52,19 +65,39 @@ } } - private RenderFragment FriendTemplate(FriendDto friend) => builder => + private string GetImage(Guid friendId) { - builder.OpenComponent(0); - builder.AddAttribute(1, "Src", GetImage(friend.FriendId)); - builder.AddAttribute(2, "Style", "width:50px; height:50px; border-radius:50%; margin-right: 10px;"); - builder.CloseComponent(); + return images.TryGetValue(friendId, out var image) ? $"data:image/webp;base64,{image}" : "images/user_default.png"; + } + + private void ToggleSelectAll(bool isSelected) + { + selectAll = isSelected; + if (isSelected) + { + selectedFriends = friends.Select(f => f.FriendId).ToList(); + } + else + { + selectedFriends.Clear(); + } + } - builder.AddContent(3, $"{friend.StudentDetails.FirstName} {friend.StudentDetails.LastName}"); - }; + private bool IsFriendSelected(Guid friendId) + { + return selectedFriends.Contains(friendId); + } - private string GetImage(Guid friendId) + private void SetFriendSelected(Guid friendId, bool isSelected) { - return images.TryGetValue(friendId, out var image) ? $"data:image/webp;base64,{image}" : "images/user_default.png"; + if (isSelected && !selectedFriends.Contains(friendId)) + { + selectedFriends.Add(friendId); + } + else if (!isSelected && selectedFriends.Contains(friendId)) + { + selectedFriends.Remove(friendId); + } } private async Task SendInvitations() @@ -82,4 +115,3 @@ NavigationManager.NavigateTo($"/events/{EventId}"); } } -