Skip to content

Commit

Permalink
(#408) notifications: udpate infrastructure
Browse files Browse the repository at this point in the history
  • Loading branch information
SaintAngeLs committed Sep 7, 2024
1 parent 38f897b commit c3c51ce
Show file tree
Hide file tree
Showing 15 changed files with 246 additions and 387 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,9 @@ public static IConveyBuilder AddInfrastructure(this IConveyBuilder builder)
// builder.Services.AddSingleton<ISignalRConnectionManager, SignalRConnectionManager>();
builder.Services.AddTransient<INotificationRepository, NotificationMongoRepository>();
builder.Services.AddTransient<IFriendEventRepository, FriendEventMongoRepository>();
builder.Services.AddTransient<IStudentNotificationsRepository, StudentNotificationsMongoRepository>();
builder.Services.AddTransient<IStudentRepository, StudentMongoRepository>();
builder.Services.AddTransient<IExtendedStudentNotificationsRepository, StudentNotificationsMongoRepository>();
builder.Services.AddSingleton<IDateTimeProvider, DateTimeProvider>();
builder.Services.AddTransient<IUserNotificationsRepository, UserNotificationsMongoRepository>();
builder.Services.AddTransient<IExtendedUserNotificationsRepository, UserNotificationsMongoRepository>();
builder.Services.AddSingleton<IBaseUrlService, BaseUrlService>();
builder.Services.AddSingleton<IEventMapper, EventMapper>();
builder.Services.AddTransient<IMessageBroker, MessageBroker>();
builder.Services.AddTransient<IAppContextFactory, AppContextFactory>();
Expand Down Expand Up @@ -93,8 +92,7 @@ public static IConveyBuilder AddInfrastructure(this IConveyBuilder builder)
.AddHandlersLogging()
.AddMongoRepository<NotificationDocument, Guid>("notifications")
.AddMongoRepository<FriendEventDocument, Guid>("friend-service")
.AddMongoRepository<StudentDocument, Guid>("students")
.AddMongoRepository<StudentNotificationsDocument, Guid>("students-notifications")
.AddMongoRepository<UserNotificationsDocument, Guid>("user_notifications")
// .AddMongoRepository<FriendEventDocument, Guid>("events-service")
.AddSignalRInfrastructure()
.AddWebApiSwaggerDocs()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@

namespace MiniSpace.Services.Notifications.Infrastructure.Mongo.Repositories
{
public interface IExtendedStudentNotificationsRepository : IStudentNotificationsRepository
public interface IExtendedUserNotificationsRepository : IUserNotificationsRepository
{
Task<UpdateResult> BulkUpdateAsync(FilterDefinition<StudentNotificationsDocument> filter, UpdateDefinition<StudentNotificationsDocument> update);
Task<UpdateResult> BulkUpdateAsync(FilterDefinition<UserNotificationsDocument> filter,
UpdateDefinition<UserNotificationsDocument> update);
Task<int> GetNotificationCount(Guid studentId);
Task<List<NotificationDocument>> GetRecentNotifications(Guid studentId, int count);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,69 +101,9 @@ public static FriendEventDto AsDto(this FriendEventDocument document)
};
}

public static Student AsEntity(this StudentDocument document)
public static UserNotifications AsEntity(this UserNotificationsDocument document)
{
return new Student(
document.Id,
document.Email,
document.FirstName,
document.LastName,
document.NumberOfFriends,
document.ProfileImage,
document.Description,
document.DateOfBirth,
document.EmailNotifications,
document.IsBanned,
document.IsOrganizer,
document.State,
document.CreatedAt
);
}

public static StudentDocument AsDocument(this Student entity)
{
return new StudentDocument
{
Id = entity.Id,
Email = entity.Email,
FirstName = entity.FirstName,
LastName = entity.LastName,
NumberOfFriends = entity.NumberOfFriends,
ProfileImage = entity.ProfileImage,
Description = entity.Description,
DateOfBirth = entity.DateOfBirth,
EmailNotifications = entity.EmailNotifications,
IsBanned = entity.IsBanned,
IsOrganizer = entity.IsOrganizer,
State = entity.State,
CreatedAt = entity.CreatedAt,
InterestedInEvents = entity.InterestedInEvents,
SignedUpEvents = entity.SignedUpEvents
};
}

public static StudentDto AsDto(this StudentDocument document)
{
return new StudentDto
{
Id = document.Id,
Email = document.Email,
FirstName = document.FirstName,
LastName = document.LastName,
Description = document.Description,
DateOfBirth = document.DateOfBirth,
EmailNotifications = document.EmailNotifications,
IsBanned = document.IsBanned,
State = document.State,
CreatedAt = document.CreatedAt,
InterestedInEvents = document.InterestedInEvents,
SignedUpEvents = document.SignedUpEvents
};
}

public static StudentNotifications AsEntity(this StudentNotificationsDocument document)
{
var studentNotifications = new StudentNotifications(document.StudentId);
var studentNotifications = new UserNotifications(document.UserId);
foreach (var notificationDocument in document.Notifications)
{
var notification = notificationDocument.AsEntity();
Expand All @@ -172,23 +112,23 @@ public static StudentNotifications AsEntity(this StudentNotificationsDocument do
return studentNotifications;
}

public static StudentNotificationsDocument AsDocument(this StudentNotifications entity)
public static UserNotificationsDocument AsDocument(this UserNotifications entity)
{
var notifications = new List<NotificationDocument>();
foreach (var notification in entity.Notifications)
{
notifications.Add(notification.AsDocument());
}

return new StudentNotificationsDocument
return new UserNotificationsDocument
{
Id = Guid.NewGuid(),
StudentId = entity.StudentId,
UserId = entity.UserId,
Notifications = notifications
};
}

public static IEnumerable<NotificationDto> AsDto(this StudentNotificationsDocument document)
public static IEnumerable<NotificationDto> AsDto(this UserNotificationsDocument document)
{
return document.Notifications.Select(nd => nd.AsDto());
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

namespace MiniSpace.Services.Notifications.Infrastructure.Mongo.Documents
{
public class StudentNotificationsDocument : IIdentifiable<Guid>
public class UserNotificationsDocument : IIdentifiable<Guid>
{
[BsonId]
[BsonRepresentation(BsonType.String)]
public Guid Id { get; set; }
public Guid StudentId { get; set; }
public Guid UserId { get; set; }
public List<NotificationDocument> Notifications { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ namespace MiniSpace.Services.Notifications.Infrastructure.Mongo.Queries.Handlers
{
public class GetNotificationHandler : IQueryHandler<GetNotification, NotificationDto>
{
private readonly IMongoRepository<StudentNotificationsDocument, Guid> _repository;
private readonly IMongoRepository<UserNotificationsDocument, Guid> _repository;

public GetNotificationHandler(IMongoRepository<StudentNotificationsDocument, Guid> repository)
public GetNotificationHandler(IMongoRepository<UserNotificationsDocument, Guid> repository)
{
_repository = repository;
}

public async Task<NotificationDto> HandleAsync(GetNotification query, CancellationToken cancellationToken)
{
var document = await _repository.GetAsync(d => d.StudentId == query.UserId);
var document = await _repository.GetAsync(d => d.UserId == query.UserId);

if (document == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,34 @@ namespace MiniSpace.Services.Notifications.Infrastructure.Mongo.Queries.Handlers
{
public class GetNotificationsByUserHandler : IQueryHandler<GetNotificationsByUser, Application.Queries.PagedResult<NotificationDto>>
{
private readonly IMongoRepository<StudentNotificationsDocument, Guid> _repository;
private readonly IMongoRepository<UserNotificationsDocument, Guid> _repository;
private const string BaseUrl = "notifications";

public GetNotificationsByUserHandler(IMongoRepository<StudentNotificationsDocument, Guid> repository)
public GetNotificationsByUserHandler(IMongoRepository<UserNotificationsDocument, Guid> repository)
{
_repository = repository;
}

public async Task<Application.Queries.PagedResult<NotificationDto>> HandleAsync(GetNotificationsByUser query, CancellationToken cancellationToken)
{
var filter = Builders<StudentNotificationsDocument>.Filter.Eq(doc => doc.StudentId, query.UserId);
var filter = Builders<UserNotificationsDocument>.Filter.Eq(doc => doc.UserId, query.UserId);

if (!string.IsNullOrEmpty(query.Status))
{
var regex = new MongoDB.Bson.BsonRegularExpression($"^{Regex.Escape(query.Status)}$", "i");
var statusFilter = Builders<StudentNotificationsDocument>.Filter.ElemMatch(x => x.Notifications,
var statusFilter = Builders<UserNotificationsDocument>.Filter.ElemMatch(x => x.Notifications,
Builders<NotificationDocument>.Filter.Regex("Status", regex));

filter = Builders<StudentNotificationsDocument>.Filter.And(filter, statusFilter);
filter = Builders<UserNotificationsDocument>.Filter.And(filter, statusFilter);

// Console.WriteLine($"Applying status filter with regex: {regex}");
}

var sortBy = Builders<StudentNotificationsDocument>.Sort.Descending(doc => doc.Notifications[-1].CreatedAt);
var sortBy = Builders<UserNotificationsDocument>.Sort.Descending(doc => doc.Notifications[-1].CreatedAt);

if (query.SortOrder.ToLower() == "asc")
{
sortBy = Builders<StudentNotificationsDocument>.Sort.Ascending(doc => doc.Notifications[-1].CreatedAt);
sortBy = Builders<UserNotificationsDocument>.Sort.Ascending(doc => doc.Notifications[-1].CreatedAt);
}

var notificationsList = new List<NotificationDto>();
Expand Down

This file was deleted.

Loading

0 comments on commit c3c51ce

Please sign in to comment.