// ==========================================================================
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex UG (haftungsbeschränkt)
// All rights reserved. Licensed under the MIT license.
// ==========================================================================
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Net.Http.Headers;
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.Infrastructure.Security;
using Squidex.Infrastructure.Translations;
using Squidex.Web;
namespace Squidex.Areas.Api.Controllers.Comments.Notifications
{
///
/// Manages user notifications.
///
[ApiExplorerSettings(GroupName = nameof(Notifications))]
public sealed class UserNotificationsController : ApiController
{
private static readonly NamedId NoApp = NamedId.Of(DomainId.Empty, "none");
private readonly ICommentsLoader commentsLoader;
public UserNotificationsController(ICommandBus commandBus, ICommentsLoader commentsLoader)
: base(commandBus)
{
this.commentsLoader = commentsLoader;
}
///
/// Get all notifications.
///
/// The user id.
/// The current version.
///
/// When passing in a version you can retrieve all updates since then.
///
///
/// 200 => All comments returned.
///
[HttpGet]
[Route("users/{userId}/notifications")]
[ProducesResponseType(typeof(CommentsDto), StatusCodes.Status200OK)]
[ApiPermission]
public async Task GetNotifications(DomainId userId, [FromQuery] long version = EtagVersion.Any)
{
CheckPermissions(userId);
var result = await commentsLoader.GetCommentsAsync(userId, version);
var response = Deferred.Response(() =>
{
return CommentsDto.FromResult(result);
});
Response.Headers[HeaderNames.ETag] = result.Version.ToString();
return Ok(response);
}
///
/// Delete a notification.
///
/// The user id.
/// The id of the comment.
///
/// 204 => Comment deleted.
/// 404 => Comment not found.
///
[HttpDelete]
[Route("users/{userId}/notifications/{commentId}")]
[ApiPermission]
public async Task DeleteComment(DomainId userId, DomainId commentId)
{
CheckPermissions(userId);
var commmand = new DeleteComment
{
AppId = NoApp,
CommentsId = userId,
CommentId = commentId
};
await CommandBus.PublishAsync(commmand);
return NoContent();
}
private void CheckPermissions(DomainId userId)
{
if (!string.Equals(userId.ToString(), User.OpenIdSubject()))
{
throw new DomainForbiddenException(T.Get("comments.noPermissions"));
}
}
}
}