diff --git a/backend/src/Designer/Controllers/Organisation/OrgCodeListController.cs b/backend/src/Designer/Controllers/Organisation/OrgCodeListController.cs new file mode 100644 index 00000000000..71949cbb65a --- /dev/null +++ b/backend/src/Designer/Controllers/Organisation/OrgCodeListController.cs @@ -0,0 +1,156 @@ +using System.Collections.Generic; +using System.Text.Json; +using System.Threading; +using System.Threading.Tasks; +using Altinn.Studio.Designer.Helpers; +using Altinn.Studio.Designer.Models; +using Altinn.Studio.Designer.Models.Dto; +using Altinn.Studio.Designer.Services.Interfaces; +using Altinn.Studio.Designer.Services.Interfaces.Organisation; +using LibGit2Sharp; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; + +namespace Altinn.Studio.Designer.Controllers.Organisation; + +/// +/// Controller containing actions related to code lists on organisation level. +/// +[ApiController] +[Authorize] +[Route("designer/api/{org}/code-lists")] +public class OrgCodeListController : ControllerBase +{ + private readonly IOrgCodeListService _orgCodeListService; + private readonly ISourceControl _sourceControl; + + /// + /// Initializes a new instance of the class. + /// + /// The CodeList service for organisation level + /// The source control service. + public OrgCodeListController(IOrgCodeListService orgCodeListService, ISourceControl sourceControl) + { + _orgCodeListService = orgCodeListService; + _sourceControl = sourceControl; + } + + /// + /// Fetches the contents of all the code lists belonging to the organisation. + /// + /// Unique identifier of the organisation. + /// A that observes if operation is cancelled. + /// List of objects with all code lists belonging to the organisation with data + /// set if code list is valid, or hasError set if code list is invalid. + [HttpGet] + public async Task>> GetCodeLists(string org, CancellationToken cancellationToken = default) + { + try + { + await _sourceControl.VerifyCloneExists(org, $"{org}-content"); + string developer = AuthenticationHelper.GetDeveloperUserName(HttpContext); + + List codeLists = await _orgCodeListService.GetCodeLists(org, developer, cancellationToken); + + return Ok(codeLists); + } + catch (NotFoundException) + { + return NoContent(); + } + } + + /// + /// Creates or overwrites a code list. + /// + /// Unique identifier of the organisation. + /// Name of the code list. + /// Contents of the code list. + /// A that observes if operation is cancelled. + [HttpPost] + [Produces("application/json")] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status400BadRequest)] + [Route("{codeListId}")] + public async Task>> CreateCodeList(string org, [FromRoute] string codeListId, [FromBody] List