// ========================================================================== // 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 { /// /// Manages comments for any kind of resource. /// [ApiExceptionFilter] [ApiExplorerSettings(GroupName = nameof(Languages))] public sealed class CommentsController : ApiController { private readonly IGrainFactory grainFactory; public CommentsController(ICommandBus commandBus, IGrainFactory grainFactory) : base(commandBus) { this.grainFactory = grainFactory; } /// /// Get all comments. /// /// The id of the comments. /// /// 200 => All comments returned. /// [HttpGet] [Route("comments/{commentsId}")] [ProducesResponseType(typeof(CommentsDto), 200)] [ApiCosts(0)] public async Task GetComments(Guid commentsId) { if (!long.TryParse(Request.Headers["X-Since"], out var version)) { version = EtagVersion.Any; } var result = await grainFactory.GetGrain(commentsId).GetCommentsAsync(version); var response = CommentsDto.FromResult(result); Response.Headers["ETag"] = response.Version.ToString(); return Ok(response); } /// /// Create a new comment. /// /// The id of the comments. /// The comment object that needs to created. /// /// 201 => Comment created. /// 400 => Comment is not valid. /// [HttpPost] [Route("comments/{commentdsId}")] [ProducesResponseType(typeof(EntityCreatedDto), 201)] [ProducesResponseType(typeof(ErrorDto), 400)] [ApiCosts(0)] public async Task PostComment(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); } /// /// Updates the comment. /// /// The id of the comments. /// The id of the comment. /// The comment object that needs to updated. /// /// 204 => Comment updated. /// 400 => Comment text not valid. /// 404 => Comment not found. /// [MustBeAppReader] [HttpPut] [Route("comments/{commentdsId}/{commentId}")] [ProducesResponseType(typeof(ErrorDto), 400)] [ApiCosts(0)] public async Task PutComment(Guid commentsId, Guid commentId, [FromBody] UpsertCommentDto request) { await CommandBus.PublishAsync(request.ToUpdateComment(commentsId, commentId)); return NoContent(); } /// /// Deletes the comment. /// /// The id of the comments. /// The id of the comment. /// /// 204 => Comment deleted. /// 404 => Comment not found. /// [HttpDelete] [Route("comments/{commentdsId}/{commentId}")] [ProducesResponseType(typeof(ErrorDto), 400)] [ApiCosts(0)] public async Task DeleteComment(Guid commentsId, Guid commentId) { await CommandBus.PublishAsync(new DeleteComment { CommentsId = commentsId, CommentId = commentId }); return NoContent(); } } }