From 8c2e276d1c872d4554bf91fd231e5bf6dace60ce Mon Sep 17 00:00:00 2001 From: Fransolet Thomas Date: Fri, 25 Mar 2022 17:38:32 +0100 Subject: [PATCH] Update export to download json file --- .../Controllers/ConfigurationController.cs | 29 +++++++++++++------ 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/ManagerService/Controllers/ConfigurationController.cs b/ManagerService/Controllers/ConfigurationController.cs index b0be595..7dba07b 100644 --- a/ManagerService/Controllers/ConfigurationController.cs +++ b/ManagerService/Controllers/ConfigurationController.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Text; using System.Threading.Tasks; using Manager.Interfaces.DTO; using Manager.Interfaces.Models; @@ -228,17 +229,18 @@ namespace ManagerService.Controllers } } - + /// /// Export a configuration /// /// Id of configuration to export - [ProducesResponseType(typeof(ExportConfigurationDTO), 200)] + [AllowAnonymous] + [ProducesResponseType(typeof(FileContentResult), 200)] [ProducesResponseType(typeof(string), 400)] [ProducesResponseType(typeof(string), 404)] [ProducesResponseType(typeof(string), 500)] [HttpGet("{id}/export")] - public ObjectResult Export(string id) + public FileContentResult Export(string id) { try { @@ -331,21 +333,30 @@ namespace ManagerService.Controllers break; } } - - return new OkObjectResult(configuration.ToExportDTO(sectionDTOs, resourceDTOs)); - + ExportConfigurationDTO toDownload = configuration.ToExportDTO(sectionDTOs, resourceDTOs); + string jsonString = JsonConvert.SerializeObject(toDownload); + var fileName = $"{configuration.Label}.json"; + var mimeType = "application/json"; + var fileBytes = Encoding.ASCII.GetBytes(jsonString); + return new FileContentResult(fileBytes, mimeType) + { + FileDownloadName = fileName + }; } catch (ArgumentNullException ex) { - return new BadRequestObjectResult(ex.Message) { }; + return null; + //return new BadRequestObjectResult(ex.Message) { }; } catch (KeyNotFoundException ex) { - return new NotFoundObjectResult(ex.Message) { }; + return null; + //return new NotFoundObjectResult(ex.Message) { }; } catch (Exception ex) { - return new ObjectResult(ex.Message) { StatusCode = 500 }; + return null; + //return new ObjectResult(ex.Message) { StatusCode = 500 }; } }