mirror of https://github.com/Squidex/squidex.git
committed by
GitHub
129 changed files with 2456 additions and 375 deletions
@ -0,0 +1,32 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschränkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using NodaTime; |
|||
using Squidex.Infrastructure; |
|||
using System; |
|||
|
|||
namespace Squidex.Domain.Apps.Core.Comments |
|||
{ |
|||
public sealed class Comment |
|||
{ |
|||
public Guid Id { get; } |
|||
|
|||
public Instant Time { get; } |
|||
|
|||
public RefToken User { get; } |
|||
|
|||
public string Text { get; } |
|||
|
|||
public Comment(Guid id, Instant time, RefToken user, string text) |
|||
{ |
|||
Id = id; |
|||
Time = time; |
|||
Text = text; |
|||
User = user; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,25 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschränkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
using Squidex.Infrastructure; |
|||
using Squidex.Infrastructure.Commands; |
|||
|
|||
namespace Squidex.Domain.Apps.Entities.Comments.Commands |
|||
{ |
|||
public abstract class CommentsCommand : SquidexCommand, IAggregateCommand, IAppCommand |
|||
{ |
|||
public Guid CommentsId { get; set; } |
|||
|
|||
public NamedId<Guid> AppId { get; set; } |
|||
|
|||
Guid IAggregateCommand.AggregateId |
|||
{ |
|||
get { return CommentsId; } |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,18 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
|
|||
namespace Squidex.Domain.Apps.Entities.Comments.Commands |
|||
{ |
|||
public sealed class CreateComment : CommentsCommand |
|||
{ |
|||
public Guid CommentId { get; } = Guid.NewGuid(); |
|||
|
|||
public string Text { get; set; } |
|||
} |
|||
} |
|||
@ -0,0 +1,18 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
|
|||
namespace Squidex.Domain.Apps.Entities.Comments.Commands |
|||
{ |
|||
public sealed class DeleteComment : CommentsCommand |
|||
{ |
|||
public Guid CommentId { get; set; } |
|||
|
|||
public string Text { get; set; } |
|||
} |
|||
} |
|||
@ -0,0 +1,18 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
|
|||
namespace Squidex.Domain.Apps.Entities.Comments.Commands |
|||
{ |
|||
public sealed class UpdateComment : CommentsCommand |
|||
{ |
|||
public Guid CommentId { get; set; } |
|||
|
|||
public string Text { get; set; } |
|||
} |
|||
} |
|||
@ -0,0 +1,126 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Threading.Tasks; |
|||
using Squidex.Domain.Apps.Entities.Comments.Commands; |
|||
using Squidex.Domain.Apps.Entities.Comments.Guards; |
|||
using Squidex.Domain.Apps.Entities.Comments.State; |
|||
using Squidex.Domain.Apps.Events.Comments; |
|||
using Squidex.Infrastructure; |
|||
using Squidex.Infrastructure.Commands; |
|||
using Squidex.Infrastructure.EventSourcing; |
|||
using Squidex.Infrastructure.Log; |
|||
using Squidex.Infrastructure.Reflection; |
|||
using Squidex.Infrastructure.States; |
|||
|
|||
namespace Squidex.Domain.Apps.Entities.Comments |
|||
{ |
|||
public sealed class CommentsGrain : DomainObjectGrainBase<CommentsState>, ICommentGrain |
|||
{ |
|||
private readonly IStore<Guid> store; |
|||
private readonly List<Envelope<CommentsEvent>> events = new List<Envelope<CommentsEvent>>(); |
|||
private CommentsState snapshot = new CommentsState { Version = EtagVersion.Empty }; |
|||
private IPersistence persistence; |
|||
|
|||
public override CommentsState Snapshot |
|||
{ |
|||
get { return snapshot; } |
|||
} |
|||
|
|||
public CommentsGrain(IStore<Guid> store, ISemanticLog log) |
|||
: base(log) |
|||
{ |
|||
Guard.NotNull(store, nameof(store)); |
|||
|
|||
this.store = store; |
|||
} |
|||
|
|||
protected override void ApplyEvent(Envelope<IEvent> @event) |
|||
{ |
|||
snapshot = new CommentsState { Version = snapshot.Version + 1 }; |
|||
|
|||
events.Add(@event.To<CommentsEvent>()); |
|||
} |
|||
|
|||
protected override void RestorePreviousSnapshot(CommentsState previousSnapshot, long previousVersion) |
|||
{ |
|||
snapshot = previousSnapshot; |
|||
} |
|||
|
|||
protected override Task ReadAsync(Type type, Guid id) |
|||
{ |
|||
persistence = store.WithEventSourcing<Guid>(GetType(), id, ApplyEvent); |
|||
|
|||
return persistence.ReadAsync(); |
|||
} |
|||
|
|||
protected override async Task WriteAsync(Envelope<IEvent>[] events, long previousVersion) |
|||
{ |
|||
if (events.Length > 0) |
|||
{ |
|||
await persistence.WriteEventsAsync(events); |
|||
} |
|||
} |
|||
|
|||
protected override Task<object> ExecuteAsync(IAggregateCommand command) |
|||
{ |
|||
switch (command) |
|||
{ |
|||
case CreateComment createComment: |
|||
return UpsertAsync(createComment, c => |
|||
{ |
|||
GuardComments.CanCreate(c); |
|||
|
|||
Create(c); |
|||
|
|||
return EntityCreatedResult.Create(createComment.CommentId, Version); |
|||
}); |
|||
|
|||
case UpdateComment updateComment: |
|||
return UpsertAsync(updateComment, c => |
|||
{ |
|||
GuardComments.CanUpdate(events, c); |
|||
|
|||
Update(c); |
|||
}); |
|||
|
|||
case DeleteComment deleteComment: |
|||
return UpsertAsync(deleteComment, c => |
|||
{ |
|||
GuardComments.CanDelete(events, c); |
|||
|
|||
Delete(c); |
|||
}); |
|||
|
|||
default: |
|||
throw new NotSupportedException(); |
|||
} |
|||
} |
|||
|
|||
public void Create(CreateComment command) |
|||
{ |
|||
RaiseEvent(SimpleMapper.Map(command, new CommentCreated())); |
|||
} |
|||
|
|||
public void Update(UpdateComment command) |
|||
{ |
|||
RaiseEvent(SimpleMapper.Map(command, new CommentUpdated())); |
|||
} |
|||
|
|||
public void Delete(DeleteComment command) |
|||
{ |
|||
RaiseEvent(SimpleMapper.Map(command, new CommentDeleted())); |
|||
} |
|||
|
|||
public Task<CommentsResult> GetCommentsAsync(long version = EtagVersion.Any) |
|||
{ |
|||
return Task.FromResult(CommentsResult.FromEvents(events, Version, (int)version)); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,96 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using Squidex.Domain.Apps.Core.Comments; |
|||
using Squidex.Domain.Apps.Events.Comments; |
|||
using Squidex.Infrastructure.EventSourcing; |
|||
|
|||
namespace Squidex.Domain.Apps.Entities.Comments |
|||
{ |
|||
public sealed class CommentsResult |
|||
{ |
|||
public List<Comment> CreatedComments { get; set; } = new List<Comment>(); |
|||
|
|||
public List<Comment> UpdatedComments { get; set; } = new List<Comment>(); |
|||
|
|||
public List<Guid> DeletedComments { get; set; } = new List<Guid>(); |
|||
|
|||
public long Version { get; set; } |
|||
|
|||
public static CommentsResult FromEvents(IEnumerable<Envelope<CommentsEvent>> events, long currentVersion, int lastVersion) |
|||
{ |
|||
var result = new CommentsResult { Version = currentVersion }; |
|||
|
|||
foreach (var @event in events.Skip(lastVersion < 0 ? 0 : lastVersion + 1)) |
|||
{ |
|||
switch (@event.Payload) |
|||
{ |
|||
case CommentDeleted deleted: |
|||
{ |
|||
var id = deleted.CommentId; |
|||
|
|||
if (result.CreatedComments.Any(x => x.Id == id)) |
|||
{ |
|||
result.CreatedComments.RemoveAll(x => x.Id == id); |
|||
} |
|||
else if (result.UpdatedComments.Any(x => x.Id == id)) |
|||
{ |
|||
result.UpdatedComments.RemoveAll(x => x.Id == id); |
|||
result.DeletedComments.Add(id); |
|||
} |
|||
else |
|||
{ |
|||
result.DeletedComments.Add(id); |
|||
} |
|||
|
|||
break; |
|||
} |
|||
|
|||
case CommentCreated created: |
|||
{ |
|||
var comment = new Comment( |
|||
created.CommentId, |
|||
@event.Headers.Timestamp(), |
|||
@event.Payload.Actor, |
|||
created.Text); |
|||
|
|||
result.CreatedComments.Add(comment); |
|||
break; |
|||
} |
|||
|
|||
case CommentUpdated updated: |
|||
{ |
|||
var id = updated.CommentId; |
|||
|
|||
var comment = new Comment( |
|||
id, |
|||
@event.Headers.Timestamp(), |
|||
@event.Payload.Actor, |
|||
updated.Text); |
|||
|
|||
if (result.CreatedComments.Any(x => x.Id == id)) |
|||
{ |
|||
result.CreatedComments.RemoveAll(x => x.Id == id); |
|||
result.CreatedComments.Add(comment); |
|||
} |
|||
else |
|||
{ |
|||
result.UpdatedComments.Add(comment); |
|||
} |
|||
|
|||
break; |
|||
} |
|||
} |
|||
} |
|||
|
|||
return result; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,88 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
using Squidex.Domain.Apps.Entities.Comments.Commands; |
|||
using Squidex.Domain.Apps.Events.Comments; |
|||
using Squidex.Infrastructure; |
|||
using Squidex.Infrastructure.EventSourcing; |
|||
|
|||
namespace Squidex.Domain.Apps.Entities.Comments.Guards |
|||
{ |
|||
public static class GuardComments |
|||
{ |
|||
public static void CanCreate(CreateComment command) |
|||
{ |
|||
Guard.NotNull(command, nameof(command)); |
|||
|
|||
Validate.It(() => "Cannot create comment.", e => |
|||
{ |
|||
if (string.IsNullOrWhiteSpace(command.Text)) |
|||
{ |
|||
e("Text is required.", nameof(command.Text)); |
|||
} |
|||
}); |
|||
} |
|||
|
|||
public static void CanUpdate(List<Envelope<CommentsEvent>> events, UpdateComment command) |
|||
{ |
|||
Guard.NotNull(command, nameof(command)); |
|||
|
|||
var comment = FindComment(events, command.CommentId); |
|||
|
|||
if (!comment.Payload.Actor.Equals(command.Actor)) |
|||
{ |
|||
throw new DomainException("Comment is created by another actor."); |
|||
} |
|||
|
|||
Validate.It(() => "Cannot update comment.", e => |
|||
{ |
|||
if (string.IsNullOrWhiteSpace(command.Text)) |
|||
{ |
|||
e("Text is required.", nameof(command.Text)); |
|||
} |
|||
}); |
|||
} |
|||
|
|||
public static void CanDelete(List<Envelope<CommentsEvent>> events, DeleteComment command) |
|||
{ |
|||
Guard.NotNull(command, nameof(command)); |
|||
|
|||
var comment = FindComment(events, command.CommentId); |
|||
|
|||
if (!comment.Payload.Actor.Equals(command.Actor)) |
|||
{ |
|||
throw new DomainException("Comment is created by another actor."); |
|||
} |
|||
} |
|||
|
|||
private static Envelope<CommentCreated> FindComment(List<Envelope<CommentsEvent>> events, Guid commentId) |
|||
{ |
|||
Envelope<CommentCreated> result = null; |
|||
|
|||
foreach (var @event in events) |
|||
{ |
|||
if (@event.Payload is CommentCreated created && created.CommentId == commentId) |
|||
{ |
|||
result = @event.To<CommentCreated>(); |
|||
} |
|||
else if (@event.Payload is CommentDeleted deleted && deleted.CommentId == commentId) |
|||
{ |
|||
result = null; |
|||
} |
|||
} |
|||
|
|||
if (result == null) |
|||
{ |
|||
throw new DomainObjectNotFoundException(commentId.ToString(), "Comments", typeof(CommentsGrain)); |
|||
} |
|||
|
|||
return result; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,18 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System.Threading.Tasks; |
|||
using Squidex.Infrastructure; |
|||
using Squidex.Infrastructure.Commands; |
|||
|
|||
namespace Squidex.Domain.Apps.Entities.Comments |
|||
{ |
|||
public interface ICommentGrain : IDomainObjectGrain |
|||
{ |
|||
Task<CommentsResult> GetCommentsAsync(long version = EtagVersion.Any); |
|||
} |
|||
} |
|||
@ -0,0 +1,13 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
namespace Squidex.Domain.Apps.Entities.Comments.State |
|||
{ |
|||
public sealed class CommentsState : DomainObjectState<CommentsState> |
|||
{ |
|||
} |
|||
} |
|||
@ -0,0 +1,20 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
using Squidex.Infrastructure.EventSourcing; |
|||
|
|||
namespace Squidex.Domain.Apps.Events.Comments |
|||
{ |
|||
[EventType(nameof(CommentCreated))] |
|||
public sealed class CommentCreated : CommentsEvent |
|||
{ |
|||
public Guid CommentId { get; set; } |
|||
|
|||
public string Text { get; set; } |
|||
} |
|||
} |
|||
@ -0,0 +1,18 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
using Squidex.Infrastructure.EventSourcing; |
|||
|
|||
namespace Squidex.Domain.Apps.Events.Comments |
|||
{ |
|||
[EventType(nameof(CommentDeleted))] |
|||
public sealed class CommentDeleted : CommentsEvent |
|||
{ |
|||
public Guid CommentId { get; set; } |
|||
} |
|||
} |
|||
@ -0,0 +1,20 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
using Squidex.Infrastructure.EventSourcing; |
|||
|
|||
namespace Squidex.Domain.Apps.Events.Comments |
|||
{ |
|||
[EventType(nameof(CommentUpdated))] |
|||
public sealed class CommentUpdated : CommentsEvent |
|||
{ |
|||
public Guid CommentId { get; set; } |
|||
|
|||
public string Text { get; set; } |
|||
} |
|||
} |
|||
@ -0,0 +1,16 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
|
|||
namespace Squidex.Domain.Apps.Events.Comments |
|||
{ |
|||
public abstract class CommentsEvent : AppEvent |
|||
{ |
|||
public Guid CommentsId { get; set; } |
|||
} |
|||
} |
|||
@ -0,0 +1,136 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschränkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
using System.Threading.Tasks; |
|||
using Microsoft.AspNetCore.Mvc; |
|||
using Orleans; |
|||
using Squidex.Areas.Api.Controllers.Comments.Models; |
|||
using Squidex.Domain.Apps.Entities.Comments; |
|||
using Squidex.Domain.Apps.Entities.Comments.Commands; |
|||
using Squidex.Infrastructure; |
|||
using Squidex.Infrastructure.Commands; |
|||
using Squidex.Pipeline; |
|||
|
|||
namespace Squidex.Areas.Api.Controllers.Comments |
|||
{ |
|||
/// <summary>
|
|||
/// Manages comments for any kind of resource.
|
|||
/// </summary>
|
|||
[ApiAuthorize] |
|||
[ApiExceptionFilter] |
|||
[AppApi] |
|||
[ApiExplorerSettings(GroupName = nameof(Comments))] |
|||
public sealed class CommentsController : ApiController |
|||
{ |
|||
private readonly IGrainFactory grainFactory; |
|||
|
|||
public CommentsController(ICommandBus commandBus, IGrainFactory grainFactory) |
|||
: base(commandBus) |
|||
{ |
|||
this.grainFactory = grainFactory; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Get all comments.
|
|||
/// </summary>
|
|||
/// <param name="app">The name of the app.</param>
|
|||
/// <param name="commentsId">The id of the comments.</param>
|
|||
/// <param name="version">The current version.</param>
|
|||
/// <remarks>
|
|||
/// When passing in a version you can retrieve all updates since then.
|
|||
/// </remarks>
|
|||
/// <returns>
|
|||
/// 200 => All comments returned.
|
|||
/// 404 => App not found.
|
|||
/// </returns>
|
|||
[HttpGet] |
|||
[Route("apps/{app}/comments/{commentsId}")] |
|||
[ProducesResponseType(typeof(CommentsDto), 200)] |
|||
[ApiCosts(0)] |
|||
public async Task<IActionResult> GetComments(string app, Guid commentsId, [FromQuery] long version = EtagVersion.Any) |
|||
{ |
|||
var result = await grainFactory.GetGrain<ICommentGrain>(commentsId).GetCommentsAsync(version); |
|||
var response = CommentsDto.FromResult(result); |
|||
|
|||
Response.Headers["ETag"] = response.Version.ToString(); |
|||
|
|||
return Ok(response); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Create a new comment.
|
|||
/// </summary>
|
|||
/// <param name="app">The name of the app.</param>
|
|||
/// <param name="commentsId">The id of the comments.</param>
|
|||
/// <param name="request">The comment object that needs to created.</param>
|
|||
/// <returns>
|
|||
/// 201 => Comment created.
|
|||
/// 400 => Comment is not valid.
|
|||
/// 404 => App not found.
|
|||
/// </returns>
|
|||
[HttpPost] |
|||
[Route("apps/{app}/comments/{commentsId}")] |
|||
[ProducesResponseType(typeof(EntityCreatedDto), 201)] |
|||
[ProducesResponseType(typeof(ErrorDto), 400)] |
|||
[ApiCosts(0)] |
|||
public async Task<IActionResult> PostComment(string app, Guid commentsId, [FromBody] UpsertCommentDto request) |
|||
{ |
|||
var command = request.ToCreateCommand(commentsId); |
|||
var context = await CommandBus.PublishAsync(command); |
|||
|
|||
var response = CommentDto.FromCommand(command); |
|||
|
|||
return CreatedAtAction(nameof(GetComments), new { commentsId }, response); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Updates the comment.
|
|||
/// </summary>
|
|||
/// <param name="app">The name of the app.</param>
|
|||
/// <param name="commentsId">The id of the comments.</param>
|
|||
/// <param name="commentId">The id of the comment.</param>
|
|||
/// <param name="request">The comment object that needs to updated.</param>
|
|||
/// <returns>
|
|||
/// 204 => Comment updated.
|
|||
/// 400 => Comment text not valid.
|
|||
/// 404 => Comment or app not found.
|
|||
/// </returns>
|
|||
[MustBeAppReader] |
|||
[HttpPut] |
|||
[Route("apps/{app}/comments/{commentsId}/{commentId}")] |
|||
[ProducesResponseType(typeof(ErrorDto), 400)] |
|||
[ApiCosts(0)] |
|||
public async Task<IActionResult> PutComment(string app, Guid commentsId, Guid commentId, [FromBody] UpsertCommentDto request) |
|||
{ |
|||
await CommandBus.PublishAsync(request.ToUpdateComment(commentsId, commentId)); |
|||
|
|||
return NoContent(); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Deletes the comment.
|
|||
/// </summary>
|
|||
/// <param name="app">The name of the app.</param>
|
|||
/// <param name="commentsId">The id of the comments.</param>
|
|||
/// <param name="commentId">The id of the comment.</param>
|
|||
/// <returns>
|
|||
/// 204 => Comment deleted.
|
|||
/// 404 => Comment or app not found.
|
|||
/// </returns>
|
|||
[HttpDelete] |
|||
[Route("apps/{app}/comments/{commentsId}/{commentId}")] |
|||
[ProducesResponseType(typeof(ErrorDto), 400)] |
|||
[ApiCosts(0)] |
|||
public async Task<IActionResult> DeleteComment(string app, Guid commentsId, Guid commentId) |
|||
{ |
|||
await CommandBus.PublishAsync(new DeleteComment { CommentsId = commentsId, CommentId = commentId }); |
|||
|
|||
return NoContent(); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,53 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
using System.ComponentModel.DataAnnotations; |
|||
using NodaTime; |
|||
using Squidex.Domain.Apps.Core.Comments; |
|||
using Squidex.Domain.Apps.Entities.Comments.Commands; |
|||
using Squidex.Infrastructure; |
|||
using Squidex.Infrastructure.Reflection; |
|||
|
|||
namespace Squidex.Areas.Api.Controllers.Comments.Models |
|||
{ |
|||
public sealed class CommentDto |
|||
{ |
|||
/// <summary>
|
|||
/// The id of the comment.
|
|||
/// </summary>
|
|||
public Guid Id { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// The time when the comment was created or updated last.
|
|||
/// </summary>
|
|||
[Required] |
|||
public Instant Time { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// The user who created or updated the comment.
|
|||
/// </summary>
|
|||
[Required] |
|||
public RefToken User { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// The text of the comment.
|
|||
/// </summary>
|
|||
[Required] |
|||
public string Text { get; set; } |
|||
|
|||
public static CommentDto FromComment(Comment comment) |
|||
{ |
|||
return SimpleMapper.Map(comment, new CommentDto()); |
|||
} |
|||
|
|||
public static CommentDto FromCommand(CreateComment command) |
|||
{ |
|||
return SimpleMapper.Map(command, new CommentDto { Id = command.CommentId, User = command.Actor, Time = SystemClock.Instance.GetCurrentInstant() }); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,48 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using Squidex.Domain.Apps.Entities.Comments; |
|||
|
|||
namespace Squidex.Areas.Api.Controllers.Comments.Models |
|||
{ |
|||
public sealed class CommentsDto |
|||
{ |
|||
/// <summary>
|
|||
/// The created comments including the updates.
|
|||
/// </summary>
|
|||
public List<CommentDto> CreatedComments { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// The updates comments since the last version.
|
|||
/// </summary>
|
|||
public List<CommentDto> UpdatedComments { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// The deleted comments since the last version.
|
|||
/// </summary>
|
|||
public List<Guid> DeletedComments { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// The current version.
|
|||
/// </summary>
|
|||
public long Version { get; set; } |
|||
|
|||
public static CommentsDto FromResult(CommentsResult result) |
|||
{ |
|||
return new CommentsDto |
|||
{ |
|||
CreatedComments = result.CreatedComments.Select(CommentDto.FromComment).ToList(), |
|||
UpdatedComments = result.UpdatedComments.Select(CommentDto.FromComment).ToList(), |
|||
DeletedComments = result.DeletedComments, |
|||
Version = result.Version |
|||
}; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,33 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
using System.ComponentModel.DataAnnotations; |
|||
using Squidex.Domain.Apps.Entities.Comments.Commands; |
|||
using Squidex.Infrastructure.Reflection; |
|||
|
|||
namespace Squidex.Areas.Api.Controllers.Comments.Models |
|||
{ |
|||
public sealed class UpsertCommentDto |
|||
{ |
|||
/// <summary>
|
|||
/// The comment text.
|
|||
/// </summary>
|
|||
[Required] |
|||
public string Text { get; set; } |
|||
|
|||
public CreateComment ToCreateCommand(Guid commentsId) |
|||
{ |
|||
return SimpleMapper.Map(this, new CreateComment { CommentsId = commentsId }); |
|||
} |
|||
|
|||
public UpdateComment ToUpdateComment(Guid commentsId, Guid commentId) |
|||
{ |
|||
return SimpleMapper.Map(this, new UpdateComment { CommentsId = commentsId, CommentId = commentId }); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1 @@ |
|||
<sqx-comments [commentsId]="commentsId"></sqx-comments> |
|||
@ -0,0 +1,2 @@ |
|||
@import '_vars'; |
|||
@import '_mixins'; |
|||
@ -0,0 +1,30 @@ |
|||
/* |
|||
* Squidex Headless CMS |
|||
* |
|||
* @license |
|||
* Copyright (c) Squidex UG (haftungsbeschränkt). All rights reserved. |
|||
*/ |
|||
|
|||
import { Component, OnInit } from '@angular/core'; |
|||
import { ActivatedRoute } from '@angular/router'; |
|||
|
|||
import { allParams } from '@app/shared'; |
|||
|
|||
@Component({ |
|||
selector: 'sqx-comments-page', |
|||
styleUrls: ['./comments-page.component.scss'], |
|||
templateUrl: './comments-page.component.html' |
|||
}) |
|||
export class CommentsPageComponent implements OnInit { |
|||
public commentsId: string; |
|||
|
|||
constructor( |
|||
private readonly route: ActivatedRoute |
|||
) { |
|||
} |
|||
|
|||
public ngOnInit() { |
|||
this.commentsId = allParams(this.route)['contentId']; |
|||
} |
|||
} |
|||
|
|||
@ -0,0 +1,46 @@ |
|||
/* |
|||
* Squidex Headless CMS |
|||
* |
|||
* @license |
|||
* Copyright (c) Squidex UG (haftungsbeschränkt). All rights reserved. |
|||
*/ |
|||
|
|||
import { HttpErrorResponse, HttpEvent, HttpHandler, HttpInterceptor, HttpRequest, HttpResponse } from '@angular/common/http'; |
|||
import { Injectable} from '@angular/core'; |
|||
import { Observable, of, throwError } from 'rxjs'; |
|||
import { catchError, tap } from 'rxjs/operators'; |
|||
|
|||
import { Types } from './../../internal'; |
|||
|
|||
@Injectable() |
|||
export class CachingInterceptor implements HttpInterceptor { |
|||
private readonly cache: { [url: string]: HttpResponse<any> } = {}; |
|||
|
|||
public intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { |
|||
if (req.method === 'GET' && req.reportProgress === false) { |
|||
const cacheEntry = this.cache[req.url]; |
|||
|
|||
if (cacheEntry) { |
|||
req = req.clone({ headers: req.headers.set('If-None-Match', cacheEntry.headers.get('Etag')!) }); |
|||
} |
|||
|
|||
return next.handle(req).pipe( |
|||
tap(response => { |
|||
if (Types.is(response, HttpResponse)) { |
|||
if (response.headers.get('Etag')) { |
|||
this.cache[req.url] = response; |
|||
} |
|||
} |
|||
}), |
|||
catchError(error => { |
|||
if (Types.is(error, HttpErrorResponse) && error.status === 304 && cacheEntry) { |
|||
return of(cacheEntry); |
|||
} else { |
|||
return throwError(error); |
|||
} |
|||
})); |
|||
} else { |
|||
return next.handle(req); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,19 @@ |
|||
<div class="comment row no-gutters"> |
|||
<div class="col col-auto"> |
|||
<img class="user-picture" [attr.title]="comment.user | sqxUserNameRef:null" [attr.src]="comment.user | sqxUserPictureRef" /> |
|||
</div> |
|||
<div class="col pl-2"> |
|||
<div class="comment-message"> |
|||
<div class="user-row"> |
|||
<div class="user-ref">{{comment.user | sqxUserNameRef:null}}</div> |
|||
|
|||
<button *ngIf="comment.user === userId" type="button" class="btn btn-sm btn-link btn-danger item-remove" (click)="deleting.emit()!"> |
|||
<i class="icon-bin2"></i> |
|||
</button> |
|||
</div> |
|||
|
|||
<div>{{comment.text}}</div> |
|||
<div class="comment-created text-muted">{{comment.time | sqxFromNow}}</div> |
|||
</div> |
|||
</div> |
|||
</div> |
|||
@ -0,0 +1,43 @@ |
|||
@import '_vars'; |
|||
@import '_mixins'; |
|||
|
|||
.user-ref { |
|||
font-weight: bold; |
|||
} |
|||
|
|||
.item-remove { |
|||
@include absolute(-5px, -15px, auto, auto); |
|||
display: none; |
|||
} |
|||
|
|||
.user-row { |
|||
& { |
|||
position: relative; |
|||
} |
|||
|
|||
&:hover { |
|||
.item-remove { |
|||
display: block; |
|||
} |
|||
} |
|||
} |
|||
|
|||
.comment { |
|||
& { |
|||
font-size: .9rem; |
|||
font-weight: normal; |
|||
margin-bottom: .75rem; |
|||
} |
|||
|
|||
&-message { |
|||
margin-bottom: .375rem; |
|||
} |
|||
|
|||
&-created { |
|||
font-size: .75rem; |
|||
} |
|||
} |
|||
|
|||
.text-muted { |
|||
color: $color-history; |
|||
} |
|||
@ -0,0 +1,38 @@ |
|||
/* |
|||
* Squidex Headless CMS |
|||
* |
|||
* @license |
|||
* Copyright (c) Squidex UG (haftungsbeschränkt). All rights reserved. |
|||
*/ |
|||
|
|||
import { ChangeDetectionStrategy, Component, EventEmitter, Input, Output } from '@angular/core'; |
|||
import { FormBuilder } from '@angular/forms'; |
|||
|
|||
import { CommentDto, UpsertCommentForm } from '@app/shared/internal'; |
|||
|
|||
@Component({ |
|||
selector: 'sqx-comment', |
|||
styleUrls: ['./comment.component.scss'], |
|||
templateUrl: './comment.component.html', |
|||
changeDetection: ChangeDetectionStrategy.OnPush |
|||
}) |
|||
export class CommentComponent { |
|||
public editForm = new UpsertCommentForm(this.formBuilder); |
|||
|
|||
@Input() |
|||
public comment: CommentDto; |
|||
|
|||
@Input() |
|||
public userId: string; |
|||
|
|||
@Output() |
|||
public deleting = new EventEmitter(); |
|||
|
|||
@Output() |
|||
public updated = new EventEmitter<string>(); |
|||
|
|||
constructor( |
|||
private readonly formBuilder: FormBuilder |
|||
) { |
|||
} |
|||
} |
|||
@ -0,0 +1,26 @@ |
|||
<sqx-panel desiredWidth="20rem" isBlank="true" [isLazyLoaded]="false" contentClass="grid"> |
|||
<ng-container title> |
|||
Comments |
|||
</ng-container> |
|||
|
|||
<ng-container content> |
|||
<div class="grid-content" #scrollMe [scrollTop]="scrollMe.scrollHeight"> |
|||
<sqx-comment *ngFor="let comment of state.comments | async; trackBy: trackByComment" |
|||
[comment]="comment" |
|||
[userId]="userId" |
|||
(updated)="update(comment, $event)" |
|||
(deleting)="delete(comment)"> |
|||
</sqx-comment> |
|||
</div> |
|||
|
|||
<div class="grid-footer"> |
|||
<form [formGroup]="commentForm.form" (submit)="comment()"> |
|||
<input class="form-control" name="text" formControlName="text" placeholder="Create a comment" /> |
|||
</form> |
|||
</div> |
|||
</ng-container> |
|||
</sqx-panel> |
|||
|
|||
|
|||
|
|||
|
|||
@ -0,0 +1,11 @@ |
|||
@import '_vars'; |
|||
@import '_mixins'; |
|||
|
|||
.grid-footer { |
|||
border-top-width: 1px; |
|||
} |
|||
|
|||
.grid-body, |
|||
.grid-footer { |
|||
padding: 1rem; |
|||
} |
|||
@ -0,0 +1,80 @@ |
|||
/* |
|||
* Squidex Headless CMS |
|||
* |
|||
* @license |
|||
* Copyright (c) Squidex UG (haftungsbeschränkt). All rights reserved. |
|||
*/ |
|||
|
|||
import { Component, Input, OnDestroy, OnInit } from '@angular/core'; |
|||
import { FormBuilder } from '@angular/forms'; |
|||
import { Subscription, timer } from 'rxjs'; |
|||
import { onErrorResumeNext, switchMap } from 'rxjs/operators'; |
|||
|
|||
import { |
|||
AppsState, |
|||
AuthService, |
|||
CommentDto, |
|||
CommentsService, |
|||
CommentsState, |
|||
DialogService, |
|||
UpsertCommentForm |
|||
} from '@app/shared/internal'; |
|||
|
|||
@Component({ |
|||
selector: 'sqx-comments', |
|||
styleUrls: ['./comments.component.scss'], |
|||
templateUrl: './comments.component.html' |
|||
}) |
|||
export class CommentsComponent implements OnDestroy, OnInit { |
|||
private timer: Subscription; |
|||
|
|||
public state: CommentsState; |
|||
|
|||
public userId: string; |
|||
|
|||
public commentForm = new UpsertCommentForm(this.formBuilder); |
|||
|
|||
@Input() |
|||
public commentsId: string; |
|||
|
|||
constructor(authService: AuthService, |
|||
private readonly appsState: AppsState, |
|||
private readonly commentsService: CommentsService, |
|||
private readonly dialogs: DialogService, |
|||
private readonly formBuilder: FormBuilder |
|||
) { |
|||
this.userId = authService.user!.token; |
|||
} |
|||
|
|||
public ngOnDestroy() { |
|||
this.timer.unsubscribe(); |
|||
} |
|||
|
|||
public ngOnInit() { |
|||
this.state = new CommentsState(this.appsState, this.commentsId, this.commentsService, this.dialogs); |
|||
|
|||
this.timer = timer(0, 4000).pipe(switchMap(() => this.state.load()), onErrorResumeNext()).subscribe(); |
|||
} |
|||
|
|||
public delete(comment: CommentDto) { |
|||
this.state.delete(comment.id).pipe(onErrorResumeNext()).subscribe(); |
|||
} |
|||
|
|||
public update(comment: CommentDto, text: string) { |
|||
this.state.update(comment.id, text).pipe(onErrorResumeNext()).subscribe(); |
|||
} |
|||
|
|||
public comment() { |
|||
const value = this.commentForm.submit(); |
|||
|
|||
if (value) { |
|||
this.state.create(value.text).pipe(onErrorResumeNext()).subscribe(); |
|||
|
|||
this.commentForm.submitCompleted({}); |
|||
} |
|||
} |
|||
|
|||
public trackByComment(index: number, comment: CommentDto) { |
|||
return comment.id; |
|||
} |
|||
} |
|||
Some files were not shown because too many files changed in this diff
Loading…
Reference in new issue