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

refactor(pages-api): change parameter for layout operations to just use layout name #14683

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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 @@ -42,15 +42,17 @@
{
bool hasChanges = false;
string[] layoutNames = repository.GetLayoutNames(notification.LayoutSetName);
foreach (var layoutName in layoutNames)
foreach (var layoutFileName in layoutNames)
{

string layoutName = layoutFileName.Replace(".json", "");
var layout = await repository.GetLayout(notification.LayoutSetName, layoutName, cancellationToken);
if (TryChangeComponentId(layout, notification.OldComponentId, notification.NewComponentId))
{
await repository.SaveLayout(notification.LayoutSetName, layoutName, layout, cancellationToken);
hasChanges = true;
}
}

Check notice

Code scanning / CodeQL

Missed opportunity to use Select Note

This foreach loop immediately
maps its iteration variable to another variable
- consider mapping the sequence explicitly using '.Select(...)'.

List<Reference> referencesToUpdate = [new Reference(ReferenceType.Component, notification.LayoutSetName, notification.OldComponentId, notification.NewComponentId)];
hasChanges |= await _appDevelopmentService.UpdateLayoutReferences(notification.EditingContext, referencesToUpdate, cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@
private const string SchemaFilePatternJson = "*.schema.json";
private const string SchemaFilePatternXsd = "*.xsd";

public static readonly string InitialLayoutFileName = "Side1.json";
public static readonly string InitialLayoutFileName = "Side1";

public readonly JsonNode InitialLayout = new JsonObject { ["$schema"] = LayoutSchemaUrl, ["data"] = new JsonObject { ["layout"] = new JsonArray([]) } };

public readonly JsonNode InitialLayoutSettings = new JsonObject { ["$schema"] = LayoutSettingsSchemaUrl, ["pages"] = new JsonObject { ["order"] = new JsonArray([InitialLayoutFileName.Replace(".json", "")]) } };
public readonly JsonNode InitialLayoutSettings = new JsonObject { ["$schema"] = LayoutSettingsSchemaUrl, ["pages"] = new JsonObject { ["order"] = new JsonArray([InitialLayoutFileName]) } };

private static readonly JsonSerializerOptions JsonOptions = new()
{
Expand Down Expand Up @@ -370,11 +370,12 @@
cancellationToken.ThrowIfCancellationRequested();
Dictionary<string, JsonNode> formLayouts = new();
string[] layoutNames = GetLayoutNames(layoutSetName);
foreach (string layoutName in layoutNames)
foreach (string layoutFileName in layoutNames)
{
string layoutName = layoutFileName.Replace(".json", "");
JsonNode layout = await GetLayout(layoutSetName, layoutName, cancellationToken);
formLayouts[layoutName.Replace(".json", "")] = layout;
formLayouts[layoutName] = layout;
}

Check notice

Code scanning / CodeQL

Missed opportunity to use Select Note

This foreach loop immediately
maps its iteration variable to another variable
- consider mapping the sequence explicitly using '.Select(...)'.

return formLayouts;
}
Expand Down Expand Up @@ -479,7 +480,7 @@
{
foreach (string layoutPath in GetFilesByRelativeDirectory(layoutSetPath))
{
layoutNames.Add(Path.GetFileName(layoutPath));
layoutNames.Add(Path.GetFileNameWithoutExtension(layoutPath));
}
}

Expand Down Expand Up @@ -652,11 +653,11 @@
await WriteTextByRelativePathAsync(layoutFilePath, serializedLayout, true, cancellationToken);
}

public void UpdateFormLayoutName(string layoutSetName, string layoutFileName, string newFileName)
public void UpdateFormLayoutName(string layoutSetName, string layoutName, string newLayoutName)
{
string currentFilePath = GetPathToLayoutFile(layoutSetName, layoutFileName);
string newFilePath = GetPathToLayoutFile(layoutSetName, newFileName);
MoveFileByRelativePath(currentFilePath, newFilePath, newFileName);
string currentFilePath = GetPathToLayoutFile(layoutSetName, layoutName);
string newFilePath = GetPathToLayoutFile(layoutSetName, newLayoutName);
MoveFileByRelativePath(currentFilePath, newFilePath, newLayoutName);
}

public async Task<LayoutSets> GetLayoutSetsFile(CancellationToken cancellationToken = default)
Expand Down Expand Up @@ -1067,11 +1068,11 @@
}

// can be null if app does not use layout set
private static string GetPathToLayoutFile(string layoutSetName, string fileName)
private static string GetPathToLayoutFile(string layoutSetName, string layoutName)
{
return string.IsNullOrEmpty(layoutSetName) ?
Path.Combine(LayoutsFolderName, LayoutsInSetFolderName, fileName) :
Path.Combine(LayoutsFolderName, layoutSetName, LayoutsInSetFolderName, fileName);
Path.Combine(LayoutsFolderName, LayoutsInSetFolderName, $"{layoutName}.json") :
Path.Combine(LayoutsFolderName, layoutSetName, LayoutsInSetFolderName, $"{layoutName}.json");
}

// can be null if app does not use layout set
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ public async Task SaveFormLayout(AltinnRepoEditingContext altinnRepoEditingConte
string layoutName, JsonNode formLayout, CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
string layoutFileName = $"{layoutName}.json";
AltinnAppGitRepository altinnAppGitRepository =
_altinnGitRepositoryFactory.GetAltinnAppGitRepository(altinnRepoEditingContext.Org,
altinnRepoEditingContext.Repo, altinnRepoEditingContext.Developer);
Expand All @@ -79,14 +78,13 @@ public async Task SaveFormLayout(AltinnRepoEditingContext altinnRepoEditingConte
"This app uses layout sets, but no layout set name was provided for this request");
}

await altinnAppGitRepository.SaveLayout(layoutSetName, layoutFileName, formLayout, cancellationToken);
await altinnAppGitRepository.SaveLayout(layoutSetName, layoutName, formLayout, cancellationToken);
}

/// <inheritdoc />
public void DeleteFormLayout(AltinnRepoEditingContext altinnRepoEditingContext, string layoutSetName,
string layoutName)
{
string layoutFileName = $"{layoutName}.json";
AltinnAppGitRepository altinnAppGitRepository =
_altinnGitRepositoryFactory.GetAltinnAppGitRepository(altinnRepoEditingContext.Org,
altinnRepoEditingContext.Repo, altinnRepoEditingContext.Developer);
Expand All @@ -97,7 +95,7 @@ public void DeleteFormLayout(AltinnRepoEditingContext altinnRepoEditingContext,
"This app uses layout sets, but no layout set name was provided for this request");
}

altinnAppGitRepository.DeleteLayout(layoutSetName, layoutFileName);
altinnAppGitRepository.DeleteLayout(layoutSetName, layoutName);
}

/// <inheritdoc />
Expand All @@ -114,7 +112,7 @@ public void UpdateFormLayoutName(AltinnRepoEditingContext altinnRepoEditingConte
"This app uses layout sets, but no layout set name was provided for this request");
}

altinnAppGitRepository.UpdateFormLayoutName(layoutSetName, $"{layoutName}.json", $"{newName}.json");
altinnAppGitRepository.UpdateFormLayoutName(layoutSetName, layoutName, newName);
}

/// <inheritdoc />
Expand Down Expand Up @@ -586,7 +584,7 @@ public async Task AddComponentToLayout(
editingContext.Repo,
editingContext.Developer
);
JsonNode formLayout = await altinnAppGitRepository.GetLayout(layoutSetName, $"{layoutName}.json", cancellationToken);
JsonNode formLayout = await altinnAppGitRepository.GetLayout(layoutSetName, layoutName, cancellationToken);
if (formLayout["data"] is not JsonObject data || data["layout"] is not JsonArray layoutArray)
{
throw new InvalidOperationException("Invalid form layout structure");
Expand Down Expand Up @@ -802,7 +800,7 @@ private async Task<bool> UpdateLayoutReferences(AltinnAppGitRepository altinnApp

if (hasLayoutChanges)
{
await altinnAppGitRepository.SaveLayout(layoutSet.Id, $"{layout.Key}.json", layout.Value, cancellationToken);
await altinnAppGitRepository.SaveLayout(layoutSet.Id, layout.Key, layout.Value, cancellationToken);
hasChanges = true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ private async Task<List<string>> UpdateKeysInLayoutsInLayoutSet(string org, stri
if (hasMutated)
{
await altinnAppGitRepository.SaveLayout(layoutSetName, layoutName, layout);
updatedFiles.Add($"App/ui/{layoutSetName}/{layoutName}");
updatedFiles.Add($"App/ui/{layoutSetName}/{layoutName}.json");
}
}
return updatedFiles.Count > 0 ? ["App/ui/layouts"] : [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,19 @@ public async Task GetOptionListsReferences_Returns200OK_WithValidOptionsReferenc
new OptionListIdSource
{
ComponentIds = ["component-using-same-options-id-in-same-set-and-another-layout"],
LayoutName = "layoutWithOneOptionListIdRef.json",
LayoutName = "layoutWithOneOptionListIdRef",
LayoutSetId = "layoutSet1"
},
new OptionListIdSource
{
ComponentIds = ["component-using-test-options-id", "component-using-test-options-id-again"],
LayoutName = "layoutWithFourCheckboxComponentsAndThreeOptionListIdRefs.json",
LayoutName = "layoutWithFourCheckboxComponentsAndThreeOptionListIdRefs",
LayoutSetId = "layoutSet1"
},
new OptionListIdSource
{
ComponentIds = ["component-using-same-options-id-in-another-set"],
LayoutName = "layoutWithTwoOptionListIdRefs.json",
LayoutName = "layoutWithTwoOptionListIdRefs",
LayoutSetId = "layoutSet2"
}
]
Expand All @@ -65,7 +65,7 @@ public async Task GetOptionListsReferences_Returns200OK_WithValidOptionsReferenc
new OptionListIdSource
{
ComponentIds = ["component-using-other-options-id"],
LayoutName = "layoutWithFourCheckboxComponentsAndThreeOptionListIdRefs.json",
LayoutName = "layoutWithFourCheckboxComponentsAndThreeOptionListIdRefs",
LayoutSetId = "layoutSet1"
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ public async Task GetLayout_WithAppThatUsesLayoutSet_ShouldReturnLayout()
string repository = "app-with-layoutsets";
string developer = "testUser";
string layoutSetName = "layoutSet1";
string layoutName = "layoutFile1InSet1.json";
string layoutName = "layoutFile1InSet1";
AltinnAppGitRepository altinnAppGitRepository = PrepareRepositoryForTest(org, repository, developer);

JsonNode formLayout = await altinnAppGitRepository.GetLayout(layoutSetName, layoutName);
Expand All @@ -216,7 +216,7 @@ public async Task GetLayout_WithAppThatNotUsesLayoutSet_ShouldReturnLayout()
string org = "ttd";
string repository = "app-without-layoutsets";
string developer = "testUser";
string layoutName = "layoutFile1.json";
string layoutName = "layoutFile1";
AltinnAppGitRepository altinnAppGitRepository = PrepareRepositoryForTest(org, repository, developer);

JsonNode formLayout = await altinnAppGitRepository.GetLayout(null, layoutName);
Expand All @@ -233,7 +233,7 @@ public async Task SaveLayout_ShouldUpdateLayoutInApp()
string repository = "app-with-layoutsets";
string developer = "testUser";
string layoutSetName = "layoutSet1";
string layoutName = "layoutFile2InSet1.json";
string layoutName = "layoutFile2InSet1";
string targetRepository = TestDataHelper.GenerateTestRepoName();

try
Expand Down
8 changes: 4 additions & 4 deletions backend/tests/Designer.Tests/Services/OptionsServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -268,19 +268,19 @@ public async Task GetAllOptionListReferences_ShouldReturnAllReferences_WhenOptio
new OptionListIdSource
{
ComponentIds = ["component-using-same-options-id-in-same-set-and-another-layout"],
LayoutName = "layoutWithOneOptionListIdRef.json",
LayoutName = "layoutWithOneOptionListIdRef",
LayoutSetId = "layoutSet1"
},
new OptionListIdSource
{
ComponentIds = ["component-using-test-options-id", "component-using-test-options-id-again"],
LayoutName = "layoutWithFourCheckboxComponentsAndThreeOptionListIdRefs.json",
LayoutName = "layoutWithFourCheckboxComponentsAndThreeOptionListIdRefs",
LayoutSetId = "layoutSet1"
},
new OptionListIdSource
{
ComponentIds = ["component-using-same-options-id-in-another-set"],
LayoutName = "layoutWithTwoOptionListIdRefs.json",
LayoutName = "layoutWithTwoOptionListIdRefs",
LayoutSetId = "layoutSet2"
}
]
Expand All @@ -292,7 +292,7 @@ public async Task GetAllOptionListReferences_ShouldReturnAllReferences_WhenOptio
new OptionListIdSource
{
ComponentIds = ["component-using-other-options-id"],
LayoutName = "layoutWithFourCheckboxComponentsAndThreeOptionListIdRefs.json",
LayoutName = "layoutWithFourCheckboxComponentsAndThreeOptionListIdRefs",
LayoutSetId = "layoutSet1"
}
]
Expand Down
4 changes: 2 additions & 2 deletions backend/tests/Designer.Tests/Services/TextsServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public class TextsServiceTest : IDisposable
private const string developer = "testUser";
private const string layoutSetName1 = "layoutSet1";
private const string layoutSetName2 = "layoutSet2";
private const string layoutName1 = "layoutFile1InSet1.json";
private const string layoutName2 = "layoutFile1InSet2.json";
private const string layoutName1 = "layoutFile1InSet1";
private const string layoutName2 = "layoutFile1InSet2";

private async Task<(string targetRepository, AltinnGitRepositoryFactory altinnGitRepositoryFactory, TextsService textsService)>
SetupRepository()
Expand Down