// ========================================================================== // 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 Squidex.Areas.Api.Controllers.Apps.Models; using Squidex.Domain.Apps.Entities.Apps.Commands; using Squidex.Infrastructure; using Squidex.Infrastructure.Commands; using Squidex.Pipeline; using Squidex.Shared; namespace Squidex.Areas.Api.Controllers.Apps { /// /// Manages and configures apps. /// [ApiExplorerSettings(GroupName = nameof(Apps))] public sealed class AppLanguagesController : ApiController { public AppLanguagesController(ICommandBus commandBus) : base(commandBus) { } /// /// Get app languages. /// /// The name of the app. /// /// 200 => Language configuration returned. /// 404 => App not found. /// [HttpGet] [Route("apps/{app}/languages/")] [ProducesResponseType(typeof(AppLanguageDto[]), 200)] [ApiPermission(Permissions.AppLanguagesRead)] [ApiCosts(0)] public IActionResult GetLanguages(string app) { var response = AppLanguageDto.FromApp(App); Response.Headers["ETag"] = App.Version.ToString(); return Ok(response); } /// /// Attaches an app language. /// /// The name of the app. /// The language to add to the app. /// /// 201 => Language created. /// 400 => Language request not valid. /// 404 => App not found. /// [HttpPost] [Route("apps/{app}/languages/")] [ProducesResponseType(typeof(AppLanguageDto), 201)] [ProducesResponseType(typeof(ErrorDto), 400)] [ApiPermission(Permissions.AppLanguagesCreate)] [ApiCosts(1)] public async Task PostLanguage(string app, [FromBody] AddAppLanguageDto request) { var command = request.ToCommand(); await CommandBus.PublishAsync(command); var response = AppLanguageDto.FromCommand(command); return CreatedAtAction(nameof(GetLanguages), new { app }, response); } /// /// Updates an app language. /// /// The name of the app. /// The language to update. /// The language object. /// /// 204 => Language updated. /// 400 => Language request not valid. /// 404 => Language or app not found. /// [HttpPut] [Route("apps/{app}/languages/{language}/")] [ApiPermission(Permissions.AppLanguagesUpdate)] [ApiCosts(1)] public async Task Update(string app, string language, [FromBody] UpdateAppLanguageDto request) { await CommandBus.PublishAsync(request.ToCommand(ParseLanguage(language))); return NoContent(); } /// /// Deletes an app language. /// /// The name of the app. /// The language to delete from the app. /// /// 204 => Language deleted. /// 404 => Language or app not found. /// [HttpDelete] [Route("apps/{app}/languages/{language}/")] [ApiPermission(Permissions.AppLanguagesDelete)] [ApiCosts(1)] public async Task DeleteLanguage(string app, string language) { await CommandBus.PublishAsync(new RemoveLanguage { Language = ParseLanguage(language) }); return NoContent(); } private static Language ParseLanguage(string language) { try { return Language.GetLanguage(language); } catch (NotSupportedException) { throw new ValidationException($"Language '{language}' is not valid."); } } } }