Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update org codelist controller #14667

Merged
merged 3 commits into from
Feb 14, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ namespace Altinn.Studio.Designer.Controllers.Organisation;
public class OrgCodeListController : ControllerBase
{
private readonly IOrgCodeListService _orgCodeListService;
private const string Repo = "content";

/// <summary>
/// Initializes a new instance of the <see cref="OrgCodeListController"/> class.
Expand All @@ -47,7 +46,7 @@ public async Task<ActionResult<List<OptionListData>>> GetCodeLists(string org, C
{
string developer = AuthenticationHelper.GetDeveloperUserName(HttpContext);

List<OptionListData> codeLists = await _orgCodeListService.GetCodeLists(org, Repo, developer, cancellationToken);
List<OptionListData> codeLists = await _orgCodeListService.GetCodeLists(org, developer, cancellationToken);

return Ok(codeLists);
}
Expand All @@ -74,7 +73,7 @@ public async Task<ActionResult<List<OptionListData>>> CreateCodeList(string org,
cancellationToken.ThrowIfCancellationRequested();
string developer = AuthenticationHelper.GetDeveloperUserName(HttpContext);

List<OptionListData> codeLists = await _orgCodeListService.CreateCodeList(org, Repo, developer, codeListId, codeList, cancellationToken);
List<OptionListData> codeLists = await _orgCodeListService.CreateCodeList(org, developer, codeListId, codeList, cancellationToken);

return Ok(codeLists);
}
Expand All @@ -96,7 +95,7 @@ public async Task<ActionResult<List<OptionListData>>> UpdateCodeList(string org,
cancellationToken.ThrowIfCancellationRequested();
string developer = AuthenticationHelper.GetDeveloperUserName(HttpContext);

List<OptionListData> codeLists = await _orgCodeListService.UpdateCodeList(org, Repo, developer, codeListId, codeList, cancellationToken);
List<OptionListData> codeLists = await _orgCodeListService.UpdateCodeList(org, developer, codeListId, codeList, cancellationToken);

return Ok(codeLists);
}
Expand All @@ -116,7 +115,7 @@ public async Task<ActionResult<List<OptionListData>>> UploadCodeList(string org,

try
{
List<OptionListData> codeLists = await _orgCodeListService.UploadCodeList(org, Repo, developer, file, cancellationToken);
List<OptionListData> codeLists = await _orgCodeListService.UploadCodeList(org, developer, file, cancellationToken);
return Ok(codeLists);
}
catch (JsonException e)
Expand All @@ -140,10 +139,10 @@ public async Task<ActionResult<List<OptionListData>>> DeleteCodeList(string org,
cancellationToken.ThrowIfCancellationRequested();
string developer = AuthenticationHelper.GetDeveloperUserName(HttpContext);

bool CodeListExists = await _orgCodeListService.CodeListExists(org, Repo, developer, codeListId, cancellationToken);
bool CodeListExists = await _orgCodeListService.CodeListExists(org, developer, codeListId, cancellationToken);
if (CodeListExists)
{
List<OptionListData> codeLists = await _orgCodeListService.DeleteCodeList(org, Repo, developer, codeListId, cancellationToken);
List<OptionListData> codeLists = await _orgCodeListService.DeleteCodeList(org, developer, codeListId, cancellationToken);
return Ok(codeLists);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace Altinn.Studio.Designer.Services.Implementation.Organisation;
public class OrgCodeListService : IOrgCodeListService
{
private readonly IAltinnGitRepositoryFactory _altinnGitRepositoryFactory;
private const string Repo = "content";

/// <summary>
/// Constructor
Expand All @@ -29,17 +30,17 @@ public OrgCodeListService(IAltinnGitRepositoryFactory altinnGitRepositoryFactory
}

/// <inheritdoc />
public async Task<List<OptionListData>> GetCodeLists(string org, string repo, string developer, CancellationToken cancellationToken = default)
public async Task<List<OptionListData>> GetCodeLists(string org, string developer, CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();

string[] codeListIds = GetCodeListIds(org, repo, developer);
string[] codeListIds = GetCodeListIds(org, developer);
List<OptionListData> codeLists = [];
foreach (string codeListId in codeListIds)
{
try
{
List<Option> codeList = await GetCodeList(org, repo, developer, codeListId, cancellationToken);
List<Option> codeList = await GetCodeList(org, developer, codeListId, cancellationToken);
OptionListData codeListData = new()
{
Title = codeListId,
Expand All @@ -64,33 +65,36 @@ public async Task<List<OptionListData>> GetCodeLists(string org, string repo, st
}

/// <inheritdoc />
public async Task<List<OptionListData>> CreateCodeList(string org, string repo, string developer, string codeListId, List<Option> codeList, CancellationToken cancellationToken = default)
public async Task<List<OptionListData>> CreateCodeList(string org, string developer, string codeListId, List<Option> codeList, CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
string repo = GetStaticContentRepo(org);
AltinnOrgGitRepository altinnOrgGitRepository = _altinnGitRepositoryFactory.GetAltinnOrgGitRepository(org, repo, developer);

await altinnOrgGitRepository.CreateCodeList(codeListId, codeList, cancellationToken);

List<OptionListData> codeLists = await GetCodeLists(org, repo, developer, cancellationToken);
List<OptionListData> codeLists = await GetCodeLists(org, developer, cancellationToken);
return codeLists;
}

/// <inheritdoc />
public async Task<List<OptionListData>> UpdateCodeList(string org, string repo, string developer, string codeListId, List<Option> codeList, CancellationToken cancellationToken = default)
public async Task<List<OptionListData>> UpdateCodeList(string org, string developer, string codeListId, List<Option> codeList, CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
string repo = GetStaticContentRepo(org);
AltinnOrgGitRepository altinnOrgGitRepository = _altinnGitRepositoryFactory.GetAltinnOrgGitRepository(org, repo, developer);

await altinnOrgGitRepository.UpdateCodeList(codeListId, codeList, cancellationToken);

List<OptionListData> codeLists = await GetCodeLists(org, repo, developer, cancellationToken);
List<OptionListData> codeLists = await GetCodeLists(org, developer, cancellationToken);
return codeLists;
}

/// <inheritdoc />
public async Task<List<OptionListData>> UploadCodeList(string org, string repo, string developer, IFormFile payload, CancellationToken cancellationToken = default)
public async Task<List<OptionListData>> UploadCodeList(string org, string developer, IFormFile payload, CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
string repo = GetStaticContentRepo(org);
string codeListId = payload.FileName.Replace(".json", "");

List<Option> deserializedCodeList = JsonSerializer.Deserialize<List<Option>>(payload.OpenReadStream(),
Expand All @@ -105,30 +109,31 @@ public async Task<List<OptionListData>> UploadCodeList(string org, string repo,
AltinnOrgGitRepository altinnOrgGitRepository = _altinnGitRepositoryFactory.GetAltinnOrgGitRepository(org, repo, developer);
await altinnOrgGitRepository.CreateCodeList(codeListId, deserializedCodeList, cancellationToken);

List<OptionListData> codeLists = await GetCodeLists(org, repo, developer, cancellationToken);
List<OptionListData> codeLists = await GetCodeLists(org, developer, cancellationToken);
return codeLists;
}

/// <inheritdoc />
public async Task<List<OptionListData>> DeleteCodeList(string org, string repo, string developer, string codeListId, CancellationToken cancellationToken = default)
public async Task<List<OptionListData>> DeleteCodeList(string org, string developer, string codeListId, CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
string repo = GetStaticContentRepo(org);
AltinnOrgGitRepository altinnOrgGitRepository = _altinnGitRepositoryFactory.GetAltinnOrgGitRepository(org, repo, developer);

altinnOrgGitRepository.DeleteCodeList(codeListId, cancellationToken);

List<OptionListData> codeLists = await GetCodeLists(org, repo, developer, cancellationToken);
List<OptionListData> codeLists = await GetCodeLists(org, developer, cancellationToken);
return codeLists;
}

/// <inheritdoc />
public async Task<bool> CodeListExists(string org, string repo, string developer, string codeListId, CancellationToken cancellationToken = default)
public async Task<bool> CodeListExists(string org, string developer, string codeListId, CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();

try
{
await GetCodeList(org, repo, developer, codeListId, cancellationToken);
await GetCodeList(org, developer, codeListId, cancellationToken);
return true;
}
catch (NotFoundException)
Expand All @@ -137,9 +142,10 @@ public async Task<bool> CodeListExists(string org, string repo, string developer
}
}

private string[] GetCodeListIds(string org, string repo, string developer, CancellationToken cancellationToken = default)
private string[] GetCodeListIds(string org, string developer, CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
string repo = GetStaticContentRepo(org);
AltinnOrgGitRepository altinnOrgGitRepository = _altinnGitRepositoryFactory.GetAltinnOrgGitRepository(org, repo, developer);

try
Expand All @@ -153,9 +159,10 @@ private string[] GetCodeListIds(string org, string repo, string developer, Cance
}
}

private async Task<List<Option>> GetCodeList(string org, string repo, string developer, string codeListId, CancellationToken cancellationToken = default)
private async Task<List<Option>> GetCodeList(string org, string developer, string codeListId, CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
string repo = GetStaticContentRepo(org);
AltinnOrgGitRepository altinnOrgGitRepository = _altinnGitRepositoryFactory.GetAltinnOrgGitRepository(org, repo, developer);

try
Expand All @@ -175,4 +182,9 @@ private void ValidateOption(Option option)
var validationContext = new ValidationContext(option);
Validator.ValidateObject(option, validationContext, validateAllProperties: true);
}

private static string GetStaticContentRepo(string org)
{
return $"{org}-{Repo}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,64 +13,58 @@ public interface IOrgCodeListService
/// Gets a code list from the app repository with the specified codeListId.
/// </summary>
/// <param name="org">Organisation</param>
/// <param name="repo">Repository</param>
/// <param name="developer">Username of developer</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> that observes if operation is cancelled.</param>
/// <returns>The code list</returns>
public Task<List<OptionListData>> GetCodeLists(string org, string repo, string developer, CancellationToken cancellationToken = default);
public Task<List<OptionListData>> GetCodeLists(string org, string developer, CancellationToken cancellationToken = default);

/// <summary>
/// Creates a new code list in the app repository.
/// If the file already exists, it will be overwritten.
/// </summary>
/// <param name="org">Organisation</param>
/// <param name="repo">Repository</param>
/// <param name="developer">Username of developer</param>
/// <param name="codeListId">Name of the new code list</param>
/// <param name="codeList">The code list contents</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> that observes if operation is cancelled.</param>
public Task<List<OptionListData>> CreateCodeList(string org, string repo, string developer, string codeListId, List<Option> codeList, CancellationToken cancellationToken = default);
public Task<List<OptionListData>> CreateCodeList(string org, string developer, string codeListId, List<Option> codeList, CancellationToken cancellationToken = default);

/// <summary>
/// Adds a new option to the code list.
/// If the file already exists, it will be overwritten.
/// </summary>
/// <param name="org">Organisation</param>
/// <param name="repo">Repository</param>
/// <param name="developer">Username of developer</param>
/// <param name="codeListId">Name of the new code list</param>
/// <param name="codeList">The code list contents</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> that observes if operation is cancelled.</param>
public Task<List<OptionListData>> UpdateCodeList(string org, string repo, string developer, string codeListId, List<Option> codeList, CancellationToken cancellationToken = default);
public Task<List<OptionListData>> UpdateCodeList(string org, string developer, string codeListId, List<Option> codeList, CancellationToken cancellationToken = default);

/// <summary>
/// Adds a new option to the code list.
/// If the file already exists, it will be overwritten.
/// </summary>
/// <param name="org">Organisation</param>
/// <param name="repo">Repository</param>
/// <param name="developer">Username of developer</param>
/// <param name="payload">The code list contents</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> that observes if operation is cancelled.</param>
public Task<List<OptionListData>> UploadCodeList(string org, string repo, string developer, IFormFile payload, CancellationToken cancellationToken = default);
public Task<List<OptionListData>> UploadCodeList(string org, string developer, IFormFile payload, CancellationToken cancellationToken = default);

/// <summary>
/// Deletes an code list from the org repository.
/// </summary>
/// <param name="org">Organisation</param>
/// <param name="repo">Repository</param>
/// <param name="developer">Username of developer</param>
/// <param name="codeListId">Name of the code list</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> that observes if operation is cancelled.</param>
public Task<List<OptionListData>> DeleteCodeList(string org, string repo, string developer, string codeListId, CancellationToken cancellationToken = default);
public Task<List<OptionListData>> DeleteCodeList(string org, string developer, string codeListId, CancellationToken cancellationToken = default);

/// <summary>
/// Checks if a code list exists in the org repository.
/// </summary>
/// <param name="org">Organisation</param>
/// <param name="repo">Repository</param>
/// <param name="developer">Username of developer</param>
/// <param name="codeListId">Name of the code list</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> that observes if operation is cancelled.</param>
public Task<bool> CodeListExists(string org, string repo, string developer, string codeListId, CancellationToken cancellationToken = default);
public Task<bool> CodeListExists(string org, string developer, string codeListId, CancellationToken cancellationToken = default);
}