-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(pages-api): create layout service to handle more explicit la…
…yout actions (#14684)
- Loading branch information
Showing
6 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
85
backend/src/Designer/Services/Implementation/LayoutService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> GetPagesByLayoutSetId(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); | ||
} | ||
|
||
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
17
backend/src/Designer/Services/Interfaces/ILayoutService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> GetPagesByLayoutSetId(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); | ||
} | ||
} |