-
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: create api endpoints for layout actions based on new layout…
… service Creates a new controller for layout operations based on the new service LayoutService. This controller will be responsible for more explicit operations on the layout configurations, allowing the frontend to do less work. commit-id:2ac25caa
- Loading branch information
Showing
1 changed file
with
127 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
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 Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Routing; | ||
|
||
namespace Altinn.Studio.Designer.Controllers | ||
{ | ||
[ApiController] | ||
[Authorize] | ||
[AutoValidateAntiforgeryToken] | ||
[Route("designer/api/{org}/{app}/layouts/layoutSet/{layoutSetId}/")] | ||
public class LayoutController(ILayoutService layoutService) : Controller | ||
{ | ||
[EndpointSummary("Retrieve pages")] | ||
[ProducesResponseType<Pages>(StatusCodes.Status200OK)] | ||
[ProducesResponseType(StatusCodes.Status404NotFound)] | ||
[HttpGet("pages")] | ||
public async Task<ActionResult<Pages>> GetPages( | ||
[FromRoute] string org, | ||
[FromRoute] string app, | ||
[FromRoute] string layoutSetId | ||
) | ||
{ | ||
string developer = AuthenticationHelper.GetDeveloperUserName(HttpContext); | ||
var editingContext = AltinnRepoEditingContext.FromOrgRepoDeveloper(org, app, developer); | ||
Pages pages = await layoutService.getPagesByLayoutSetId(editingContext, layoutSetId); | ||
return Ok(pages); | ||
} | ||
|
||
[EndpointSummary("Create page")] | ||
[ProducesResponseType<Pages>(StatusCodes.Status201Created)] | ||
[ProducesResponseType(StatusCodes.Status409Conflict)] | ||
[HttpPost("pages")] | ||
public async Task<ActionResult<Page>> CreatePage( | ||
[FromRoute] string org, | ||
[FromRoute] string app, | ||
[FromRoute] string layoutSetId, | ||
[FromBody] Page page | ||
) | ||
{ | ||
string developer = AuthenticationHelper.GetDeveloperUserName(HttpContext); | ||
var editingContext = AltinnRepoEditingContext.FromOrgRepoDeveloper(org, app, developer); | ||
|
||
Page existingPage = await layoutService.getPageById(editingContext, layoutSetId, page.id); | ||
if (existingPage != null) | ||
{ | ||
return Conflict(); | ||
} | ||
await layoutService.createPage(editingContext, layoutSetId, page.id); | ||
return Created(); | ||
} | ||
|
||
[EndpointSummary("Retrieve page")] | ||
[ProducesResponseType<Page>(StatusCodes.Status200OK)] | ||
[ProducesResponseType(StatusCodes.Status404NotFound)] | ||
[HttpGet("pages/{pageId}")] | ||
[ProducesResponseType<Page>(StatusCodes.Status200OK)] | ||
public async Task<ActionResult<Page>> GetPage( | ||
[FromRoute] string org, | ||
[FromRoute] string app, | ||
[FromRoute] string layoutSetId, | ||
[FromRoute] string pageId | ||
) | ||
{ | ||
string developer = AuthenticationHelper.GetDeveloperUserName(HttpContext); | ||
var editingContext = AltinnRepoEditingContext.FromOrgRepoDeveloper(org, app, developer); | ||
Page page = await layoutService.getPageById(editingContext, layoutSetId, pageId); | ||
if (page == null) | ||
{ | ||
return NotFound(); | ||
} | ||
return Ok(page); | ||
} | ||
|
||
[EndpointSummary("Modify page")] | ||
[ProducesResponseType<Page>(StatusCodes.Status200OK)] | ||
[ProducesResponseType(StatusCodes.Status404NotFound)] | ||
[HttpPost("pages/{pageId}")] | ||
public async Task<ActionResult<Page>> ModifyPageAsync( | ||
[FromRoute] string org, | ||
[FromRoute] string app, | ||
[FromRoute] string layoutSetId, | ||
[FromRoute] string pageId, | ||
[FromBody] Page page | ||
) | ||
{ | ||
string developer = AuthenticationHelper.GetDeveloperUserName(HttpContext); | ||
var editingContext = AltinnRepoEditingContext.FromOrgRepoDeveloper(org, app, developer); | ||
Page existingPage = await layoutService.getPageById(editingContext, layoutSetId, pageId); | ||
if (existingPage == null) | ||
{ | ||
return NotFound(); | ||
} | ||
|
||
await layoutService.updatePage(editingContext, layoutSetId, pageId, page); | ||
return Ok(); | ||
} | ||
|
||
[EndpointSummary("Delete page")] | ||
[ProducesResponseType(StatusCodes.Status200OK)] | ||
[ProducesResponseType(StatusCodes.Status404NotFound)] | ||
[HttpDelete("pages/{pageId}")] | ||
public async Task<ActionResult> DeletePage( | ||
[FromRoute] string org, | ||
[FromRoute] string app, | ||
[FromRoute] string layoutSetId, | ||
[FromRoute] string pageId | ||
) | ||
{ | ||
string developer = AuthenticationHelper.GetDeveloperUserName(HttpContext); | ||
var editingContext = AltinnRepoEditingContext.FromOrgRepoDeveloper(org, app, developer); | ||
Page page = await layoutService.getPageById(editingContext, layoutSetId, pageId); | ||
if (page == null) | ||
{ | ||
return NotFound(); | ||
} | ||
|
||
await layoutService.deletePage(editingContext, layoutSetId, pageId); | ||
return Ok(); | ||
} | ||
} | ||
} |