Skip to content

Commit

Permalink
Merge pull request #383 from SaintAngeLs/organizations_events
Browse files Browse the repository at this point in the history
(#377) services udpate: posts service, events service
  • Loading branch information
SaintAngeLs authored Aug 20, 2024
2 parents 7fc8ae4 + 4c19690 commit 951f0a9
Show file tree
Hide file tree
Showing 318 changed files with 8,791 additions and 4,076 deletions.
8 changes: 4 additions & 4 deletions MiniSpace.APIGateway/src/MiniSpace.APIGateway/ntrada.yml
Original file line number Diff line number Diff line change
Expand Up @@ -163,19 +163,19 @@ modules:
method: POST
use: downstream
downstream: identity-service/access-tokens/revoke
auth: true
auth: false

- upstream: /refresh-tokens/use
method: POST
use: downstream
downstream: identity-service/refresh-tokens/use
auth: true
auth: false

- upstream: /refresh-tokens/revoke
method: POST
use: downstream
downstream: identity-service/refresh-tokens/revoke
auth: true
auth: false

services:
identity-service:
Expand Down Expand Up @@ -772,7 +772,7 @@ modules:
auth: true

- upstream: /search
method: POST
method: GET
use: downstream
downstream: posts-service/posts/search
auth: true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using Convey.CQRS.Commands;

namespace MiniSpace.Services.Comments.Application.Commands
Expand All @@ -8,21 +7,20 @@ public class CreateComment : ICommand
{
public Guid CommentId { get; set; }
public Guid ContextId { get; set; }
public string CommentContext { get; set; }
public Guid StudentId { get; set; }
public string CommentContext { get; set; }
public Guid UserId { get; set; }
public Guid ParentId { get; set; }
public string Comment { get; set; }

public string TextContent { get; set; }

public CreateComment(Guid commentId, Guid contextId, string commentContext, Guid studentId, Guid parentId,
string comment)
public CreateComment(Guid commentId, Guid contextId, string commentContext, Guid userId, Guid parentId,
string textContent)
{
CommentId = commentId == Guid.Empty ? Guid.NewGuid() : commentId;
ContextId = contextId;
CommentContext = commentContext;
StudentId = studentId;
UserId = userId;
ParentId = parentId;
Comment = comment;
TextContent = textContent;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ namespace MiniSpace.Services.Comments.Application.Commands
public class DeleteComment : ICommand
{
public Guid CommentId { get; }

public DeleteComment(Guid commentId) => CommentId = commentId;
}
public string CommentContext { get; }

public DeleteComment(Guid commentId, string commentContext)
{
CommentId = commentId;
CommentContext = commentContext;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
using System.Threading;
using System.Threading.Tasks;
using Convey.CQRS.Commands;
using MiniSpace.Services.Comments.Application.Commands;
using MiniSpace.Services.Comments.Application.Events;
using MiniSpace.Services.Comments.Application.Exceptions;
using MiniSpace.Services.Comments.Application.Services;
using MiniSpace.Services.Comments.Application.Services.Clients;
using MiniSpace.Services.Comments.Core.Entities;
using MiniSpace.Services.Comments.Core.Exceptions;
using MiniSpace.Services.Comments.Core.Repositories;
Expand All @@ -14,17 +16,30 @@ namespace MiniSpace.Services.Comments.Application.Commands.Handlers
{
public class CreateCommentHandler : ICommandHandler<CreateComment>
{
private readonly ICommentRepository _commentRepository;
private readonly IStudentRepository _studentRepository;
private readonly IOrganizationEventsCommentRepository _organizationEventsCommentRepository;
private readonly IOrganizationPostsCommentRepository _organizationPostsCommentRepository;
private readonly IUserEventsCommentRepository _userEventsCommentRepository;
private readonly IUserPostsCommentRepository _userPostsCommentRepository;
private readonly IDateTimeProvider _dateTimeProvider;
private readonly IMessageBroker _messageBroker;
private readonly IAppContext _appContext;
private readonly IStudentsServiceClient _userServiceClient;

public CreateCommentHandler(ICommentRepository commentRepository, IStudentRepository studentRepository,
IDateTimeProvider dateTimeProvider, IMessageBroker messageBroker, IAppContext appContext)
public CreateCommentHandler(
IOrganizationEventsCommentRepository organizationEventsCommentRepository,
IOrganizationPostsCommentRepository organizationPostsCommentRepository,
IUserEventsCommentRepository userEventsCommentRepository,
IUserPostsCommentRepository userPostsCommentRepository,
IStudentsServiceClient userServiceClient,
IDateTimeProvider dateTimeProvider,
IMessageBroker messageBroker,
IAppContext appContext)
{
_commentRepository = commentRepository;
_studentRepository = studentRepository;
_organizationEventsCommentRepository = organizationEventsCommentRepository;
_organizationPostsCommentRepository = organizationPostsCommentRepository;
_userEventsCommentRepository = userEventsCommentRepository;
_userPostsCommentRepository = userPostsCommentRepository;
_userServiceClient = userServiceClient;
_dateTimeProvider = dateTimeProvider;
_messageBroker = messageBroker;
_appContext = appContext;
Expand All @@ -33,42 +48,114 @@ public CreateCommentHandler(ICommentRepository commentRepository, IStudentReposi
public async Task HandleAsync(CreateComment command, CancellationToken cancellationToken = default)
{
var identity = _appContext.Identity;
if (identity.IsAuthenticated && identity.Id != command.StudentId)
if (identity.IsAuthenticated && identity.Id != command.UserId)
{
throw new UnauthorizedCommentAccessException(command.ContextId, identity.Id);
}
if (!(await _studentRepository.ExistsAsync(command.StudentId)))

var user = await _userServiceClient.GetAsync(command.UserId);
if (user == null)
{
throw new StudentNotFoundException(command.StudentId);
throw new UserNotFoundException(command.UserId);
}

if (!Enum.TryParse<CommentContext>(command.CommentContext, true, out var newCommentContext))
if (!Enum.TryParse<CommentContext>(command.CommentContext, true, out var commentContext))
{
throw new InvalidCommentContextEnumException(command.CommentContext);
}

var now = _dateTimeProvider.Now;
var comment = Comment.Create(command.CommentId, command.ContextId, newCommentContext, command.StudentId,
identity.Name, command.ParentId, command.Comment, now);

Comment comment;

switch (commentContext)
{
case CommentContext.OrganizationEvent:
comment = Comment.Create(command.CommentId, command.ContextId, commentContext, command.UserId, command.ParentId, command.TextContent, now);
await _organizationEventsCommentRepository.AddAsync(comment);
break;

case CommentContext.OrganizationPost:
comment = Comment.Create(command.CommentId, command.ContextId, commentContext, command.UserId, command.ParentId, command.TextContent, now);
await _organizationPostsCommentRepository.AddAsync(comment);
break;

case CommentContext.UserEvent:
comment = Comment.Create(command.CommentId, command.ContextId, commentContext, command.UserId, command.ParentId, command.TextContent, now);
await _userEventsCommentRepository.AddAsync(comment);
break;

case CommentContext.UserPost:
comment = Comment.Create(command.CommentId, command.ContextId, commentContext, command.UserId, command.ParentId, command.TextContent, now);
await _userPostsCommentRepository.AddAsync(comment);
break;

default:
throw new InvalidCommentContextEnumException(command.CommentContext);
}

if (command.ParentId != Guid.Empty)
{
var parentComment = await _commentRepository.GetAsync(command.ParentId);
Comment parentComment = null;
Guid replyId = Guid.NewGuid();

switch (commentContext)
{
case CommentContext.OrganizationEvent:
parentComment = await _organizationEventsCommentRepository.GetAsync(command.ParentId);
break;
case CommentContext.OrganizationPost:
parentComment = await _organizationPostsCommentRepository.GetAsync(command.ParentId);
break;
case CommentContext.UserEvent:
parentComment = await _userEventsCommentRepository.GetAsync(command.ParentId);
break;
case CommentContext.UserPost:
parentComment = await _userPostsCommentRepository.GetAsync(command.ParentId);
break;
}

if (parentComment is null)
{
throw new ParentCommentNotFoundException(command.ParentId);
}

if (parentComment.ParentId != Guid.Empty)
{
throw new InvalidParentCommentException(command.ParentId);
}
parentComment.AddReply(now);
await _commentRepository.UpdateAsync(parentComment);

parentComment.AddReply(replyId, command.UserId, command.TextContent, now);

switch (commentContext)
{
case CommentContext.OrganizationEvent:
await _organizationEventsCommentRepository.UpdateAsync(parentComment);
break;
case CommentContext.OrganizationPost:
await _organizationPostsCommentRepository.UpdateAsync(parentComment);
break;
case CommentContext.UserEvent:
await _userEventsCommentRepository.UpdateAsync(parentComment);
break;
case CommentContext.UserPost:
await _userPostsCommentRepository.UpdateAsync(parentComment);
break;
}
}

await _commentRepository.AddAsync(comment);
await _messageBroker.PublishAsync(new CommentCreated(command.CommentId));
}


await _messageBroker.PublishAsync(new CommentCreated(
comment.Id,
comment.ContextId,
comment.CommentContext.ToString(),
comment.UserId,
comment.ParentId,
comment.TextContent,
comment.CreatedAt,
comment.LastUpdatedAt,
comment.RepliesCount,
comment.IsDeleted
));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,48 +1,100 @@
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Convey.CQRS.Commands;
using MiniSpace.Services.Comments.Application.Events;
using MiniSpace.Services.Comments.Application.Exceptions;
using MiniSpace.Services.Comments.Application.Services;
using MiniSpace.Services.Comments.Core.Entities;
using MiniSpace.Services.Comments.Core.Repositories;
using MiniSpace.Services.Comments.Application.Events;

namespace MiniSpace.Services.Comments.Application.Commands.Handlers
{
public class DeleteCommentHandler : ICommandHandler<DeleteComment>
{
private readonly ICommentRepository _commentRepository;
private readonly IOrganizationEventsCommentRepository _organizationEventsCommentRepository;
private readonly IOrganizationPostsCommentRepository _organizationPostsCommentRepository;
private readonly IUserEventsCommentRepository _userEventsCommentRepository;
private readonly IUserPostsCommentRepository _userPostsCommentRepository;
private readonly IAppContext _appContext;
private readonly IMessageBroker _messageBroker;

public DeleteCommentHandler(ICommentRepository commentRepository, IAppContext appContext,

public DeleteCommentHandler(
IOrganizationEventsCommentRepository organizationEventsCommentRepository,
IOrganizationPostsCommentRepository organizationPostsCommentRepository,
IUserEventsCommentRepository userEventsCommentRepository,
IUserPostsCommentRepository userPostsCommentRepository,
IAppContext appContext,
IMessageBroker messageBroker)
{
_commentRepository = commentRepository;
_organizationEventsCommentRepository = organizationEventsCommentRepository;
_organizationPostsCommentRepository = organizationPostsCommentRepository;
_userEventsCommentRepository = userEventsCommentRepository;
_userPostsCommentRepository = userPostsCommentRepository;
_appContext = appContext;
_messageBroker = messageBroker;
}

public async Task HandleAsync(DeleteComment command, CancellationToken cancellationToken = default)
{
var comment = await _commentRepository.GetAsync(command.CommentId);
var identity = _appContext.Identity;

Comment comment = null;

switch (command.CommentContext)
{
case nameof(CommentContext.OrganizationEvent):
comment = await _organizationEventsCommentRepository.GetAsync(command.CommentId);
break;

case nameof(CommentContext.OrganizationPost):
comment = await _organizationPostsCommentRepository.GetAsync(command.CommentId);
break;

case nameof(CommentContext.UserEvent):
comment = await _userEventsCommentRepository.GetAsync(command.CommentId);
break;

case nameof(CommentContext.UserPost):
comment = await _userPostsCommentRepository.GetAsync(command.CommentId);
break;

default:
throw new InvalidCommentContextEnumException(command.CommentContext);
}

if (comment is null)
{
throw new CommentNotFoundException(command.CommentId);
}

var identity = _appContext.Identity;
if (identity.IsAuthenticated && identity.Id != comment.StudentId && !identity.IsAdmin)

if (identity.IsAuthenticated && identity.Id != comment.UserId)
{
throw new UnauthorizedCommentAccessException(command.CommentId, identity.Id);
}

comment.Delete();
await _commentRepository.UpdateAsync(comment);

switch (command.CommentContext)
{
case nameof(CommentContext.OrganizationEvent):
await _organizationEventsCommentRepository.UpdateAsync(comment);
break;

case nameof(CommentContext.OrganizationPost):
await _organizationPostsCommentRepository.UpdateAsync(comment);
break;

case nameof(CommentContext.UserEvent):
await _userEventsCommentRepository.UpdateAsync(comment);
break;

case nameof(CommentContext.UserPost):
await _userPostsCommentRepository.UpdateAsync(comment);
break;
}

await _messageBroker.PublishAsync(new CommentDeleted(command.CommentId));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public async Task HandleAsync(DeleteLike command, CancellationToken cancellation
}

var identity = _appContext.Identity;
if (identity.IsAuthenticated && identity.Id != comment.StudentId)
if (identity.IsAuthenticated && identity.Id != comment.UserId)
{
throw new UnauthorizedCommentAccessException(command.CommentId, identity.Id);
}
Expand Down
Loading

0 comments on commit 951f0a9

Please sign in to comment.