// ==========================================================================
// AppContributorsController.cs
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex Group
// All rights reserved.
// ==========================================================================
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using NSwag.Annotations;
using Squidex.Infrastructure.CQRS.Commands;
using Squidex.Infrastructure.Reflection;
using Squidex.Modules.Api.Apps.Models;
using Squidex.Pipeline;
using Squidex.Read.Apps.Services;
using Squidex.Write.Apps.Commands;
namespace Squidex.Modules.Api.Apps
{
[Authorize(Roles = "app-owner")]
[ApiExceptionFilter]
[ServiceFilter(typeof(AppFilterAttribute))]
public class AppContributorsController : ControllerBase
{
private readonly IAppProvider appProvider;
public AppContributorsController(ICommandBus commandBus, IAppProvider appProvider)
: base(commandBus)
{
this.appProvider = appProvider;
}
///
/// Get contributors for the app.
///
/// The name of the app.
[HttpGet]
[Route("apps/{app}/contributors/")]
[SwaggerTags("Apps")]
public async Task GetContributors(string app)
{
var entity = await appProvider.FindAppByNameAsync(app);
if (entity == null)
{
return NotFound();
}
var model = entity.Contributors.Select(x => SimpleMapper.Map(x, new ContributorDto())).ToList();
return Ok(model);
}
///
/// Assign contributor to the app.
///
/// The name of the app.
/// Contributor object that needs to be added to the app.
[HttpPost]
[Route("apps/{app}/contributors/")]
[SwaggerTags("Apps")]
[DescribedResponseType(400, typeof(ErrorDto), "User is already assigned to the app or not found.")]
public async Task PostContributor(string app, [FromBody] AssignContributorDto model)
{
await CommandBus.PublishAsync(SimpleMapper.Map(model, new AssignContributor()));
return Ok();
}
///
/// Removes contributor from app.
///
/// The name of the app.
///
[HttpDelete]
[Route("apps/{app}/contributors/{contributorId}/")]
[SwaggerTags("Apps")]
[DescribedResponseType(400, typeof(ErrorDto), "User is not assigned to the app.")]
public async Task DeleteContributor(string app, string contributorId)
{
await CommandBus.PublishAsync(new RemoveContributor { ContributorId = contributorId });
return Ok();
}
}
}