Skip to content

Commit

Permalink
(#30) add endpoint for getting event rating
Browse files Browse the repository at this point in the history
  • Loading branch information
eggwhat committed May 15, 2024
1 parent 03a49c3 commit 2aa43be
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public static async Task Main(string[] args)
.Delete<CancelRateEvent>("events/{eventId}/rate")
.Get<GetStudentEvents, PagedResponse<IEnumerable<EventDto>>>("events/student/{studentId}")
.Get<GetEventParticipants, EventParticipantsDto>("events/{eventId}/participants")
.Get<GetEventRating, EventRatingDto>("events/{eventId}/rating")
.Post<AddEventParticipant>("events/{eventId}/participants")
.Delete<RemoveEventParticipant>("events/{eventId}/participants")
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ParticipantDto> FriendsInterestedIn { get; set; }
public IEnumerable<ParticipantDto> FriendsSignedUp { get; set; }

Expand All @@ -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<ParticipantDto>();
FriendsSignedUp = Enumerable.Empty<ParticipantDto>();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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; }
}
}
Original file line number Diff line number Diff line change
@@ -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<EventRatingDto>
{
public Guid EventId { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<FriendDto> friends)
Expand Down Expand Up @@ -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 ()
Expand Down
Original file line number Diff line number Diff line change
@@ -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<GetEventRating, EventRatingDto>
{
private readonly IMongoRepository<EventDocument, Guid> _eventRepository;
private readonly IAppContext _appContext;

public GetEventRatingHandler(IMongoRepository<EventDocument, Guid> eventRepository,
IAppContext appContext)
{
_eventRepository = eventRepository;
_appContext = appContext;
}

public async Task<EventRatingDto> 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();
}

}
}

0 comments on commit 2aa43be

Please sign in to comment.