Skip to content

Commit

Permalink
refactor: create layout service to handle more explicit layout actions
Browse files Browse the repository at this point in the history
Creates a new service `LayoutService` that will be used for more fine
grained explicit operations on the layout configurations.
Adds basic operations for pages in a layout.

commit-id:d6447eac
  • Loading branch information
Jondyr committed Feb 18, 2025
1 parent 18fb83c commit dca3296
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,16 @@ public void DeleteTexts(string languageCode)
}
}

public async Task CreatePageLayoutFile(string layoutSetId, string pageId)
{
JsonObject defaultPageLayout = new()
{
["$schema"] = LayoutSchemaUrl,
["data"] = new JsonObject { ["layout"] = new JsonArray([]) }
};
await WriteObjectByRelativePathAsync(Path.Combine(LayoutsFolderName, layoutSetId, $"{LayoutsInSetFolderName}/{pageId}.json"), defaultPageLayout);
}

/// <summary>
/// Returns all the layouts for a specific layout set
/// </summary>
Expand Down
1 change: 1 addition & 0 deletions backend/src/Designer/Infrastructure/ServiceRegistration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public static IServiceCollection RegisterServiceImplementations(this IServiceCol
services.AddTransient<IInstanceService, InstanceService>();
services.AddTransient<IProcessModelingService, ProcessModelingService>();
services.AddTransient<IImagesService, ImagesService>();
services.AddTransient<ILayoutService, LayoutService>();
services.RegisterDatamodeling(configuration);

return services;
Expand Down
9 changes: 9 additions & 0 deletions backend/src/Designer/Models/Dto/Page.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Text.Json.Serialization;

namespace Altinn.Studio.Designer.Models.Dto;

public class Page
{
[JsonPropertyName("id")]
public string id { get; set; }
}
10 changes: 10 additions & 0 deletions backend/src/Designer/Models/Dto/Pages.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace Altinn.Studio.Designer.Models.Dto;

public class Pages
{
[JsonPropertyName("pages")]
public List<Page> pages { get; set; }
}
85 changes: 85 additions & 0 deletions backend/src/Designer/Services/Implementation/LayoutService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using System.Linq;
using System.Text.Json.Nodes;
using System.Threading.Tasks;
using Altinn.Studio.Designer.Infrastructure.GitRepository;
using Altinn.Studio.Designer.Models;
using Altinn.Studio.Designer.Models.Dto;
using Altinn.Studio.Designer.Services.Interfaces;

namespace Altinn.Studio.Designer.Services.Implementation
{
public class LayoutService(IAltinnGitRepositoryFactory altinnGitRepositoryFactory) : ILayoutService
{
private static Pages getPagesFromSettings(JsonNode settings)
{
Pages pages = new()
{
pages = []
};

JsonNode pagesNode = settings["pages"];
JsonArray pagesArray = pagesNode["order"] as JsonArray;
foreach (JsonNode pageNode in pagesArray)
{
string pageId = pageNode.GetValue<string>();
pages.pages.Add(new Page { id = pageId });
}

return pages;
}

public async Task<Pages> getPagesByLayoutId(AltinnRepoEditingContext editingContext, string layoutSetId)
{
AltinnAppGitRepository appRepository = altinnGitRepositoryFactory.GetAltinnAppGitRepository(editingContext.Org, editingContext.Repo, editingContext.Developer);
JsonNode jsonNode = await appRepository.GetLayoutSettingsAndCreateNewIfNotFound(layoutSetId);
Pages pages = getPagesFromSettings(jsonNode);
return pages;
}

public async Task<Page> getPageById(AltinnRepoEditingContext editingContext, string layoutSetId, string pageId)
{
AltinnAppGitRepository appRepository = altinnGitRepositoryFactory.GetAltinnAppGitRepository(editingContext.Org, editingContext.Repo, editingContext.Developer);
JsonNode jsonNode = await appRepository.GetLayoutSettingsAndCreateNewIfNotFound(layoutSetId);
Pages pages = getPagesFromSettings(jsonNode);
return pages.pages.Find(page => page.id == pageId) ?? null;
}

public async Task createPage(AltinnRepoEditingContext editingContext, string layoutSetId, string pageId)
{
AltinnAppGitRepository appRepository = altinnGitRepositoryFactory.GetAltinnAppGitRepository(editingContext.Org, editingContext.Repo, editingContext.Developer);

await appRepository.CreatePageLayoutFile(layoutSetId, pageId);

JsonNode jsonNode = await appRepository.GetLayoutSettingsAndCreateNewIfNotFound(layoutSetId);
(jsonNode["pages"]["order"] as JsonArray).Add(pageId);
await appRepository.SaveLayoutSettings(layoutSetId, jsonNode);
}

public async Task deletePage(AltinnRepoEditingContext editingContext, string layoutSetId, string pageId)
{
AltinnAppGitRepository appRepository = altinnGitRepositoryFactory.GetAltinnAppGitRepository(editingContext.Org, editingContext.Repo, editingContext.Developer);

appRepository.DeleteLayout(layoutSetId, pageId);

JsonNode jsonNode = await appRepository.GetLayoutSettingsAndCreateNewIfNotFound(layoutSetId);
JsonArray orderArray = jsonNode["pages"]["order"] as JsonArray;
JsonNode orderPage = orderArray.First(node => node.GetValue<string>().Equals(pageId));
orderArray.Remove(orderPage);
await appRepository.SaveLayoutSettings(layoutSetId, jsonNode);
}

public async Task updatePage(AltinnRepoEditingContext editingContext, string layoutSetId, string pageId, Page page)
{
AltinnAppGitRepository appRepository = altinnGitRepositoryFactory.GetAltinnAppGitRepository(editingContext.Org, editingContext.Repo, editingContext.Developer);

appRepository.UpdateFormLayoutName(layoutSetId, pageId, page.id);

JsonNode jsonNode = await appRepository.GetLayoutSettingsAndCreateNewIfNotFound(layoutSetId);
JsonArray orderArray = jsonNode["pages"]["order"] as JsonArray;
JsonNode orderPage = orderArray.First(node => node.GetValue<string>().Equals(pageId));
int pageIndex = orderArray.IndexOf(orderPage);
orderArray[pageIndex] = page.id;
await appRepository.SaveLayoutSettings(layoutSetId, jsonNode);
}
}
}
17 changes: 17 additions & 0 deletions backend/src/Designer/Services/Interfaces/ILayoutService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

using System.Threading.Tasks;
using Altinn.Studio.Designer.Models;
using Altinn.Studio.Designer.Models.Dto;

namespace Altinn.Studio.Designer.Services.Interfaces
{
public interface ILayoutService
{
public Task<Pages> getPagesByLayoutId(AltinnRepoEditingContext editingContext, string layoutSetId);
public Task<Page> getPageById(AltinnRepoEditingContext editingContext, string layoutSetId, string pageId);

public Task createPage(AltinnRepoEditingContext editingContext, string layoutSetId, string pageId);
public Task deletePage(AltinnRepoEditingContext editingContext, string layoutSetId, string pageId);
public Task updatePage(AltinnRepoEditingContext editingContext, string layoutSetId, string pageId, Page page);
}
}

0 comments on commit dca3296

Please sign in to comment.