diff --git a/MiniSpace.Services.Events/src/MiniSpace.Services.Events.Api/Program.cs b/MiniSpace.Services.Events/src/MiniSpace.Services.Events.Api/Program.cs index dd284e1c6..19256117f 100644 --- a/MiniSpace.Services.Events/src/MiniSpace.Services.Events.Api/Program.cs +++ b/MiniSpace.Services.Events/src/MiniSpace.Services.Events.Api/Program.cs @@ -58,6 +58,7 @@ public static async Task Main(string[] args) .Delete("events/{eventId}/rate") .Get>>("events/student/{studentId}") .Get("events/{eventId}/participants") + .Get("events/{eventId}/rating") .Post("events/{eventId}/participants") .Delete("events/{eventId}/participants") ) diff --git a/MiniSpace.Services.Events/src/MiniSpace.Services.Events.Application/DTO/EventDto.cs b/MiniSpace.Services.Events/src/MiniSpace.Services.Events.Application/DTO/EventDto.cs index aa1352062..22881081b 100644 --- a/MiniSpace.Services.Events/src/MiniSpace.Services.Events.Application/DTO/EventDto.cs +++ b/MiniSpace.Services.Events/src/MiniSpace.Services.Events.Application/DTO/EventDto.cs @@ -26,7 +26,7 @@ public class EventDto public DateTime UpdatedAt { get; set; } public bool IsSignedUp { get; set; } public bool IsInterested { get; set; } - public bool HasRated { get; set; } + public int? StudentRating { get; set; } public IEnumerable FriendsInterestedIn { get; set; } public IEnumerable FriendsSignedUp { get; set; } @@ -52,7 +52,7 @@ public EventDto(Event @event, Guid studentId) PublishDate = @event.PublishDate; IsSignedUp = @event.SignedUpStudents.Any(x => x.StudentId == studentId); IsInterested = @event.InterestedStudents.Any(x => x.StudentId == studentId); - HasRated = @event.Ratings.Any(x => x.StudentId == studentId); + StudentRating = @event.Ratings.FirstOrDefault(x => x.StudentId == studentId)?.Value; FriendsInterestedIn = Enumerable.Empty(); FriendsSignedUp = Enumerable.Empty(); } diff --git a/MiniSpace.Services.Events/src/MiniSpace.Services.Events.Application/DTO/EventRatingDto.cs b/MiniSpace.Services.Events/src/MiniSpace.Services.Events.Application/DTO/EventRatingDto.cs new file mode 100644 index 000000000..fa5e434fd --- /dev/null +++ b/MiniSpace.Services.Events/src/MiniSpace.Services.Events.Application/DTO/EventRatingDto.cs @@ -0,0 +1,11 @@ +using System; + +namespace MiniSpace.Services.Events.Application.DTO +{ + public class EventRatingDto + { + public Guid EventId { get; set; } + public int TotalRatings { get; set; } + public double AverageRating { get; set; } + } +} \ No newline at end of file diff --git a/MiniSpace.Services.Events/src/MiniSpace.Services.Events.Application/Queries/GetEventRating.cs b/MiniSpace.Services.Events/src/MiniSpace.Services.Events.Application/Queries/GetEventRating.cs new file mode 100644 index 000000000..68b209d7e --- /dev/null +++ b/MiniSpace.Services.Events/src/MiniSpace.Services.Events.Application/Queries/GetEventRating.cs @@ -0,0 +1,11 @@ +using System; +using Convey.CQRS.Queries; +using MiniSpace.Services.Events.Application.DTO; + +namespace MiniSpace.Services.Events.Application.Queries +{ + public class GetEventRating : IQuery + { + public Guid EventId { get; set; } + } +} \ No newline at end of file diff --git a/MiniSpace.Services.Events/src/MiniSpace.Services.Events.Infrastructure/Mongo/Documents/Extensions.cs b/MiniSpace.Services.Events/src/MiniSpace.Services.Events.Infrastructure/Mongo/Documents/Extensions.cs index 5464f8410..855a05bcc 100644 --- a/MiniSpace.Services.Events/src/MiniSpace.Services.Events.Infrastructure/Mongo/Documents/Extensions.cs +++ b/MiniSpace.Services.Events/src/MiniSpace.Services.Events.Infrastructure/Mongo/Documents/Extensions.cs @@ -28,7 +28,7 @@ public static EventDto AsDto(this EventDocument document, Guid studentId) UpdatedAt = document.UpdatedAt, IsSignedUp = document.SignedUpStudents.Any(x => x.StudentId == studentId), IsInterested = document.InterestedStudents.Any(x => x.StudentId == studentId), - HasRated = document.Ratings.Any(x => x.StudentId == studentId) + StudentRating = document.Ratings.FirstOrDefault(x => x.StudentId == studentId)?.Value, }; public static EventDto AsDtoWithFriends(this EventDocument document, Guid studentId, IEnumerable friends) @@ -76,6 +76,14 @@ public static EventParticipantsDto AsDto(this EventDocument document) InterestedStudents = document.InterestedStudents.Select(p => p.AsDto()), SignedUpStudents = document.SignedUpStudents.Select(p => p.AsDto()) }; + + public static EventRatingDto AsRatingDto(this EventDocument document) + => new () + { + EventId = document.Id, + TotalRatings = document.Ratings.Count(), + AverageRating = document.Ratings.Any() ? document.Ratings.Average(x => x.Value) : 0 + }; public static AddressDto AsDto(this Address entity) => new () diff --git a/MiniSpace.Services.Events/src/MiniSpace.Services.Events.Infrastructure/Mongo/Queries/Handlers/GetEventRatingHandler.cs b/MiniSpace.Services.Events/src/MiniSpace.Services.Events.Infrastructure/Mongo/Queries/Handlers/GetEventRatingHandler.cs new file mode 100644 index 000000000..2e947f53d --- /dev/null +++ b/MiniSpace.Services.Events/src/MiniSpace.Services.Events.Infrastructure/Mongo/Queries/Handlers/GetEventRatingHandler.cs @@ -0,0 +1,42 @@ +using System; +using System.Threading; +using System.Threading.Tasks; +using Convey.CQRS.Queries; +using Convey.Persistence.MongoDB; +using MiniSpace.Services.Events.Application; +using MiniSpace.Services.Events.Application.DTO; +using MiniSpace.Services.Events.Application.Queries; +using MiniSpace.Services.Events.Infrastructure.Mongo.Documents; + +namespace MiniSpace.Services.Events.Infrastructure.Mongo.Queries.Handlers +{ + public class GetEventRatingHandler : IQueryHandler + { + private readonly IMongoRepository _eventRepository; + private readonly IAppContext _appContext; + + public GetEventRatingHandler(IMongoRepository eventRepository, + IAppContext appContext) + { + _eventRepository = eventRepository; + _appContext = appContext; + } + + public async Task HandleAsync(GetEventRating query, CancellationToken cancellationToken) + { + var document = await _eventRepository.GetAsync(p => p.Id == query.EventId); + if(document is null) + { + return null; + } + var identity = _appContext.Identity; + if(identity.IsAuthenticated && identity.Id != document.Organizer.Id && !identity.IsAdmin) + { + return null; + } + + return document.AsRatingDto(); + } + + } +} \ No newline at end of file