-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #383 from SaintAngeLs/organizations_events
(#377) services udpate: posts service, events service
- Loading branch information
Showing
318 changed files
with
8,791 additions
and
4,076 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 65 additions & 13 deletions
78
...nts/src/MiniSpace.Services.Comments.Application/Commands/Handlers/DeleteCommentHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.