Skip to content

Commit

Permalink
(#125) (#126) (#229) udpate the modal razor component, update the inv…
Browse files Browse the repository at this point in the history
…itation notification
  • Loading branch information
SaintAngeLs committed May 31, 2024
1 parent 0069ecc commit 16e537c
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ public class NotificationCreatedHandler : IEventHandler<NotificationCreated>
IStudentsServiceClient studentsServiceClient,
IMessageBroker messageBroker,
IEmailService emailService,
IStudentEmailsRepository studentEmailsRepository)
IStudentEmailsRepository studentEmailsRepository,
ILogger<NotificationCreatedHandler> logger)
{
_studentsServiceClient = studentsServiceClient;
_messageBroker = messageBroker;
_emailService = emailService;
_studentEmailsRepository = studentEmailsRepository;
_logger = logger;
}

public async Task HandleAsync(NotificationCreated @event, CancellationToken cancellationToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -14,36 +15,39 @@ public class CreateNotificationHandler : ICommandHandler<CreateNotification>
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 = $"<p>You have been invited to the event '{eventDetails.Name}' " +
$"Learn more: <a href='{eventLink}'>Click here</a>.</p>";
var notificationMessage = $"<p>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: <a href='{eventLink}'>Click here</a>.</p>";

var notification = new Notification(
command.NotificationId,
userId,
command.Message,
notificationMessage,
NotificationStatus.Unread,
DateTime.UtcNow,
null,
Expand All @@ -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);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,35 @@
@inject IMediaFilesService MediaFilesService
@inject INotificationsService NotificationService

<RadzenTemplateForm TItem="InvitationModel">
<RadzenFieldset>
<RadzenLabel Text="Select Friends" />
<RadzenListBox @bind-Value="selectedFriends" Data="@friends" TextProperty="FullName" ValueProperty="FriendId" Template=@((friend) => FriendTemplate(friend)) Style="width:100%; height:300px;" Multiple="true"/>
</RadzenFieldset>
<RadzenButton ButtonType="ButtonType.Submit" Text="Send Invitations" Click="@SendInvitations"/>
</RadzenTemplateForm>
<div class="rz-p-0 rz-p-md-12">
<RadzenStack Orientation="Orientation.Horizontal" AlignItems="AlignItems.Center" Gap="0.5rem" Class="rz-p-4 rz-mb-6 rz-border-radius-1" Style="border: var(--rz-grid-cell-border);">
<RadzenCheckBox @bind-Value="selectAll" Change="@((bool value) => ToggleSelectAll(value))" Text="Select All" />
<RadzenButton Text="Send Invitations" ButtonType="ButtonType.Submit" Click="@SendInvitations" Class="rz-ml-auto"/>
</RadzenStack>

@foreach (var friend in friends)
{
<RadzenCard Class="rz-my-3 rz-mx-auto" Style="max-width: 420px">
<RadzenStack Orientation="Orientation.Horizontal" JustifyContent="JustifyContent.Start" Gap="1rem" Class="rz-p-4">
<RadzenImage Path=@GetImage(friend.FriendId) Style="width: 100px; height: 100px; border-radius: 50%;" />
<RadzenStack Gap="0">
<RadzenText TextStyle="TextStyle.Overline" class="rz-display-flex rz-mt-2 rz-my-0">Friend</RadzenText>
<RadzenText TextStyle="TextStyle.Body1"><b>@($"{friend.StudentDetails.FirstName} {friend.StudentDetails.LastName}")</b></RadzenText>
<RadzenCheckBox Value="@IsFriendSelected(friend.FriendId)"
Change="@((bool value) => SetFriendSelected(friend.FriendId, value))" />
</RadzenStack>
</RadzenStack>
</RadzenCard>
}
</div>

@code {
[Parameter]
public Guid EventId { get; set; }

private List<FriendDto> friends = new List<FriendDto>();
private Dictionary<Guid, string> images = new Dictionary<Guid, string>();
private List<Guid> selectedFriends = new List<Guid>();
private bool selectAll = false;

protected override async Task OnInitializedAsync()
{
Expand All @@ -52,19 +65,39 @@
}
}

private RenderFragment FriendTemplate(FriendDto friend) => builder =>
private string GetImage(Guid friendId)
{
builder.OpenComponent<RadzenImage>(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()
Expand All @@ -82,4 +115,3 @@
NavigationManager.NavigateTo($"/events/{EventId}");
}
}

0 comments on commit 16e537c

Please sign in to comment.