// ========================================================================== // Squidex Headless CMS // ========================================================================== // Copyright (c) Squidex UG (haftungsbeschraenkt) // All rights reserved. Licensed under the MIT license. // ========================================================================== using System.Collections.Generic; using Squidex.Domain.Apps.Entities.Comments.Commands; using Squidex.Domain.Apps.Events.Comments; using Squidex.Infrastructure; using Squidex.Infrastructure.EventSourcing; using Squidex.Infrastructure.Translations; using Squidex.Infrastructure.Validation; namespace Squidex.Domain.Apps.Entities.Comments.DomainObject.Guards { public static class GuardComments { public static void CanCreate(CreateComment command) { Guard.NotNull(command, nameof(command)); Validate.It(e => { if (string.IsNullOrWhiteSpace(command.Text)) { e(Not.Defined("Text"), nameof(command.Text)); } }); } public static void CanUpdate(UpdateComment command, string commentsId, List> events) { Guard.NotNull(command, nameof(command)); var comment = FindComment(events, command.CommentId); if (!string.Equals(commentsId, command.Actor.Identifier) && !comment.Payload.Actor.Equals(command.Actor)) { throw new DomainException(T.Get("comments.notUserComment")); } Validate.It(e => { if (string.IsNullOrWhiteSpace(command.Text)) { e(Not.Defined("Text"), nameof(command.Text)); } }); } public static void CanDelete(DeleteComment command, string commentsId, List> events) { Guard.NotNull(command, nameof(command)); var comment = FindComment(events, command.CommentId); if (!string.Equals(commentsId, command.Actor.Identifier) && !comment.Payload.Actor.Equals(command.Actor)) { throw new DomainException(T.Get("comments.notUserComment")); } } private static Envelope FindComment(List> events, DomainId commentId) { Envelope? result = null; foreach (var @event in events) { if (@event.Payload is CommentCreated created && created.CommentId == commentId) { result = @event.To(); } else if (@event.Payload is CommentDeleted deleted && deleted.CommentId == commentId) { result = null; } } if (result == null) { throw new DomainObjectNotFoundException(commentId.ToString()); } return result; } } }