Skip to content

Commit

Permalink
(#380) events: update comments and reactions user history
Browse files Browse the repository at this point in the history
  • Loading branch information
SaintAngeLs committed Aug 30, 2024
1 parent fc1eb40 commit 8fada4e
Show file tree
Hide file tree
Showing 11 changed files with 328 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using Convey.Types;

namespace MiniSpace.Services.Events.Infrastructure.Mongo.Documents
{
public class CommentDocument : IIdentifiable<Guid>
{
public Guid Id { get; set; }
public Guid ContextId { get; set; }
public string CommentContext { get; set; }
public Guid UserId { get; set; }
public Guid ParentId { get; set; }
public string TextContent { get; set; }
public DateTime CreatedAt { get; set; }
public DateTime LastUpdatedAt { get; set; }
public int RepliesCount { get; set; }
public bool IsDeleted { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -240,5 +240,95 @@ public static RatingDto AsRatingDto(this RatingDocument document)

public static Rating AsEntity(this RatingDocument document)
=> new (document.StudentId, document.Value);


public static CommentDocument AsDocument(this Comment comment)
{
return new CommentDocument
{
Id = comment.Id,
ContextId = comment.ContextId,
CommentContext = comment.CommentContext,
UserId = comment.UserId,
ParentId = comment.ParentId,
TextContent = comment.TextContent,
CreatedAt = comment.CreatedAt,
LastUpdatedAt = comment.LastUpdatedAt,
RepliesCount = comment.RepliesCount,
IsDeleted = comment.IsDeleted
};
}

public static Comment AsEntity(this CommentDocument document)
{
return new Comment(
document.Id,
document.ContextId,
document.CommentContext,
document.UserId,
document.ParentId,
document.TextContent,
document.CreatedAt,
document.LastUpdatedAt,
document.RepliesCount,
document.IsDeleted
);
}

public static UserCommentsDocument AsDocument(this IEnumerable<Comment> comments, Guid userId)
{
return new UserCommentsDocument
{
Id = Guid.NewGuid(),
UserId = userId,
Comments = comments.Select(comment => comment.AsDocument()).ToList()
};
}

public static IEnumerable<Comment> AsEntities(this UserCommentsDocument document)
{
return document.Comments.Select(doc => doc.AsEntity());
}

public static ReactionDocument AsDocument(this Reaction reaction)
{
return new ReactionDocument
{
Id = reaction.Id,
UserId = reaction.UserId,
ContentId = reaction.ContentId,
ContentType = reaction.ContentType,
ReactionType = reaction.Type,
TargetType = reaction.TargetType,
CreatedAt = reaction.CreatedAt
};
}

public static Reaction AsEntity(this ReactionDocument document)
{
return Reaction.Create(
document.Id,
document.UserId,
document.ReactionType,
document.ContentId,
document.ContentType,
document.TargetType
);
}

public static UserReactionDocument AsDocument(this IEnumerable<Reaction> reactions, Guid userId)
{
return new UserReactionDocument
{
Id = Guid.NewGuid(),
UserId = userId,
Reactions = reactions.Select(reaction => reaction.AsDocument()).ToList()
};
}

public static IEnumerable<Reaction> AsEntities(this UserReactionDocument document)
{
return document.Reactions.Select(doc => doc.AsEntity());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;

namespace MiniSpace.Services.Events.Infrastructure.Mongo.Documents
{
public class ReactionDocument
{
public Guid Id { get; set; }
public Guid UserId { get; set; }
public Guid ContentId { get; set; }
public string ContentType { get; set; }
public string ReactionType { get; set; }
public string TargetType { get; set; }
public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using Convey.Types;
using MongoDB.Bson.Serialization.Attributes;

namespace MiniSpace.Services.Events.Infrastructure.Mongo.Documents
{
public class UserCommentsDocument : IIdentifiable<Guid>
{
[BsonId]
[BsonRepresentation(MongoDB.Bson.BsonType.String)]
public Guid Id { get; set; }
public Guid UserId { get; set; }
public IEnumerable<CommentDocument> Comments { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using Convey.Types;
using MongoDB.Bson.Serialization.Attributes;

namespace MiniSpace.Services.Events.Infrastructure.Mongo.Documents
{
public class UserReactionDocument : IIdentifiable<Guid>
{
[BsonId]
[BsonRepresentation(MongoDB.Bson.BsonType.String)]
public Guid Id { get; set; }
public Guid UserId { get; set; }
public IEnumerable<ReactionDocument> Reactions { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
using MiniSpace.Services.Events.Application.DTO;
using MiniSpace.Services.Events.Application.Queries;
using MiniSpace.Services.Events.Core.Repositories;
using MiniSpace.Services.Events.Core.Wrappers;
using MiniSpace.Services.Events.Infrastructure.Mongo.Documents;

namespace MiniSpace.Services.Events.Infrastructure.Mongo.Queries.Handlers
{
public class GetPaginatedEventsHandler : IQueryHandler<GetPaginatedEvents, MiniSpace.Services.Events.Application.DTO.PagedResult<EventDto>>
public class GetPaginatedEventsHandler : IQueryHandler<GetPaginatedEvents, PagedResponse<EventDto>>
{
private readonly IEventRepository _eventRepository;
private readonly IAppContext _appContext;
Expand All @@ -22,30 +23,28 @@ public GetPaginatedEventsHandler(IEventRepository eventRepository, IAppContext a
_appContext = appContext;
}

public async Task<MiniSpace.Services.Events.Application.DTO.PagedResult<EventDto>> HandleAsync(GetPaginatedEvents query, CancellationToken cancellationToken)
public async Task<PagedResponse<EventDto>> HandleAsync(GetPaginatedEvents query, CancellationToken cancellationToken)
{
// Fetch the paginated events from the repository
var (events, pageNumber, pageSize, totalPages, totalElements) = await _eventRepository.BrowseEventsAsync(
pageNumber: query.Page,
pageSize: query.PageSize,
name: string.Empty, // Assuming no filtering by name
organizer: string.Empty, // Assuming no filtering by organizer
dateFrom: default, // Assuming no date filter
dateTo: default, // Assuming no date filter
category: null, // Assuming no category filter
state: null, // Assuming no state filter
organizations: Enumerable.Empty<Guid>(), // Assuming no organization filter
friends: Enumerable.Empty<Guid>(), // Assuming no friends filter
friendsEngagementType: null, // Assuming no engagement type filter
sortBy: Enumerable.Empty<string>(), // Assuming no sorting
direction: string.Empty // Assuming no sorting direction
name: string.Empty,
organizer: string.Empty,
dateFrom: default,
dateTo: default,
category: null,
state: null,
organizations: Enumerable.Empty<Guid>(),
friends: Enumerable.Empty<Guid>(),
friendsEngagementType: null,
sortBy: Enumerable.Empty<string>(),
direction: string.Empty
);

var studentId = _appContext.Identity.Id;
var eventDtos = events.Select(e => e.AsDto(studentId)).ToList();

// Return a paged result with the fetched data
return new MiniSpace.Services.Events.Application.DTO.PagedResult<EventDto>(eventDtos, pageNumber, pageSize, totalElements);
return new PagedResponse<EventDto>(eventDtos, pageNumber, pageSize, totalElements);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
using MiniSpace.Services.Events.Application.DTO;
using MiniSpace.Services.Events.Application.Queries;
using MiniSpace.Services.Events.Core.Repositories;
using MiniSpace.Services.Events.Core.Wrappers;
using MiniSpace.Services.Events.Infrastructure.Mongo.Documents;

namespace MiniSpace.Services.Events.Infrastructure.Mongo.Queries.Handlers
{
public class GetPaginatedOrganizerEventsHandler : IQueryHandler<GetPaginatedOrganizerEvents, MiniSpace.Services.Events.Application.DTO.PagedResult<EventDto>>
public class GetPaginatedOrganizerEventsHandler : IQueryHandler<GetPaginatedOrganizerEvents, PagedResponse<EventDto>>
{
private readonly IEventRepository _eventRepository;
private readonly IAppContext _appContext;
Expand All @@ -22,7 +23,7 @@ public GetPaginatedOrganizerEventsHandler(IEventRepository eventRepository, IApp
_appContext = appContext;
}

public async Task<MiniSpace.Services.Events.Application.DTO.PagedResult<EventDto>> HandleAsync(GetPaginatedOrganizerEvents query, CancellationToken cancellationToken)
public async Task<PagedResponse<EventDto>> HandleAsync(GetPaginatedOrganizerEvents query, CancellationToken cancellationToken)
{
var (events, pageNumber, pageSize, totalPages, totalElements) = await _eventRepository.BrowseOrganizerEventsAsync(
pageNumber: query.Page,
Expand All @@ -39,7 +40,7 @@ public GetPaginatedOrganizerEventsHandler(IEventRepository eventRepository, IApp
var studentId = _appContext.Identity.Id;
var eventDtos = events.Select(e => e.AsDto(studentId)).ToList();

return new MiniSpace.Services.Events.Application.DTO.PagedResult<EventDto>(eventDtos, pageNumber, pageSize, totalElements);
return new PagedResponse<EventDto>(eventDtos, pageNumber, pageSize, totalElements);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
using MiniSpace.Services.Events.Application.Queries;
using MiniSpace.Services.Events.Core.Entities;
using MiniSpace.Services.Events.Core.Repositories;
using MiniSpace.Services.Events.Core.Wrappers;
using MiniSpace.Services.Events.Infrastructure.Mongo.Documents;

namespace MiniSpace.Services.Events.Infrastructure.Mongo.Queries.Handlers
{
public class GetPaginatedSearchEventsHandler : IQueryHandler<GetSearchEvents, MiniSpace.Services.Events.Application.DTO.PagedResult<EventDto>>
public class GetPaginatedSearchEventsHandler : IQueryHandler<GetSearchEvents, PagedResponse<EventDto>>
{
private readonly IEventRepository _eventRepository;
private readonly IAppContext _appContext;
Expand All @@ -25,14 +26,13 @@ public GetPaginatedSearchEventsHandler(IEventRepository eventRepository, IAppCon
_appContext = appContext;
}

public async Task<MiniSpace.Services.Events.Application.DTO.PagedResult<EventDto>> HandleAsync(GetSearchEvents query, CancellationToken cancellationToken)
public async Task<PagedResponse<EventDto>> HandleAsync(GetSearchEvents query, CancellationToken cancellationToken)
{
var jsonOptionsx = new JsonSerializerOptions { WriteIndented = true };
var jsonOptionsx = new JsonSerializerOptions { WriteIndented = true };
var queryJson = JsonSerializer.Serialize(query, jsonOptionsx);
Console.WriteLine("Query Object: ");
Console.WriteLine(queryJson);

// Convert string values to corresponding enum types
Category? category = null;
State? state = null;
EventEngagementType? engagementType = null;
Expand All @@ -52,15 +52,12 @@ public GetPaginatedSearchEventsHandler(IEventRepository eventRepository, IAppCon
engagementType = parsedEngagementType;
}

// Use PageableDto for pagination and sorting
var pageNumber = query.Pageable?.Page ?? 1;
var pageSize = query.Pageable?.Size ?? 10;

// Handle sorting
var sortBy = query.Pageable?.Sort?.SortBy ?? Enumerable.Empty<string>();
var sortDirection = query.Pageable?.Sort?.Direction ?? string.Empty;

// Fetch the events based on the query parameters
var (events, returnedPageNumber, returnedPageSize, totalPages, totalElements) = await _eventRepository.BrowseEventsAsync(
pageNumber: pageNumber,
pageSize: pageSize,
Expand All @@ -77,19 +74,16 @@ public GetPaginatedSearchEventsHandler(IEventRepository eventRepository, IAppCon
direction: sortDirection
);

// Map events to DTOs
var studentId = _appContext.Identity.Id;
var eventDtos = events.Select(e => e.AsDto(studentId)).ToList();

var pagedResult = new MiniSpace.Services.Events.Application.DTO.PagedResult<EventDto>(eventDtos, pageNumber, pageSize, totalElements);
var pagedResult = new PagedResponse<EventDto>(eventDtos, pageNumber, pageSize, totalElements);

// Serialize the result to JSON and log it
var jsonOptions = new JsonSerializerOptions { WriteIndented = true };
var jsonResult = JsonSerializer.Serialize(pagedResult, jsonOptions);
Console.WriteLine("Search Results: ");
Console.WriteLine(jsonResult);

// Return the paginated result
return pagedResult;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@
using MiniSpace.Services.Events.Application.Services;
using MiniSpace.Services.Events.Application.Services.Clients;
using MiniSpace.Services.Events.Core.Entities;
using MiniSpace.Services.Events.Core.Wrappers;
using MiniSpace.Services.Events.Core.Repositories;

namespace MiniSpace.Services.Events.Infrastructure.Mongo.Queries.Handlers
{
[ExcludeFromCodeCoverage]
public class GetUserEventsHandler : IQueryHandler<GetUserEvents, MiniSpace.Services.Events.Application.DTO.PagedResult<EventDto>>
public class GetUserEventsHandler : IQueryHandler<GetUserEvents, PagedResponse<EventDto>>
{
private readonly IEventRepository _eventRepository;
private readonly IStudentsServiceClient _studentsServiceClient;
Expand All @@ -32,12 +33,12 @@ public GetUserEventsHandler(IEventRepository eventRepository,
_appContext = appContext;
}

public async Task<MiniSpace.Services.Events.Application.DTO.PagedResult<EventDto>> HandleAsync(GetUserEvents query, CancellationToken cancellationToken)
public async Task<PagedResponse<EventDto>> HandleAsync(GetUserEvents query, CancellationToken cancellationToken)
{
var identity = _appContext.Identity;
if (identity.IsAuthenticated && identity.Id != query.UserId)
{
return new MiniSpace.Services.Events.Application.DTO.PagedResult<EventDto>(Enumerable.Empty<EventDto>(), 1, query.NumberOfResults, 0);
return new PagedResponse<EventDto>(Enumerable.Empty<EventDto>(), 1, query.NumberOfResults, 0);
}

int pageSize = query.NumberOfResults > 0 ? query.NumberOfResults : 10;
Expand All @@ -55,7 +56,7 @@ public GetUserEventsHandler(IEventRepository eventRepository,
var result = await _eventRepository.BrowseStudentEventsAsync(query.Page,
pageSize, studentEventIds, Enumerable.Empty<string>(), "asc");

return new MiniSpace.Services.Events.Application.DTO.PagedResult<EventDto>(
return new PagedResponse<EventDto>(
result.events.Select(e => new EventDto(e, identity.Id)),
result.pageNumber,
result.pageSize,
Expand Down
Loading

0 comments on commit 8fada4e

Please sign in to comment.