Update export to download json file

This commit is contained in:
Fransolet Thomas 2022-03-25 17:38:32 +01:00
parent e5bfed2ff6
commit 8c2e276d1c

View File

@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Manager.Interfaces.DTO; using Manager.Interfaces.DTO;
using Manager.Interfaces.Models; using Manager.Interfaces.Models;
@ -233,12 +234,13 @@ namespace ManagerService.Controllers
/// Export a configuration /// Export a configuration
/// </summary> /// </summary>
/// <param name="id">Id of configuration to export</param> /// <param name="id">Id of configuration to export</param>
[ProducesResponseType(typeof(ExportConfigurationDTO), 200)] [AllowAnonymous]
[ProducesResponseType(typeof(FileContentResult), 200)]
[ProducesResponseType(typeof(string), 400)] [ProducesResponseType(typeof(string), 400)]
[ProducesResponseType(typeof(string), 404)] [ProducesResponseType(typeof(string), 404)]
[ProducesResponseType(typeof(string), 500)] [ProducesResponseType(typeof(string), 500)]
[HttpGet("{id}/export")] [HttpGet("{id}/export")]
public ObjectResult Export(string id) public FileContentResult Export(string id)
{ {
try try
{ {
@ -331,21 +333,30 @@ namespace ManagerService.Controllers
break; break;
} }
} }
ExportConfigurationDTO toDownload = configuration.ToExportDTO(sectionDTOs, resourceDTOs);
return new OkObjectResult(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) catch (ArgumentNullException ex)
{ {
return new BadRequestObjectResult(ex.Message) { }; return null;
//return new BadRequestObjectResult(ex.Message) { };
} }
catch (KeyNotFoundException ex) catch (KeyNotFoundException ex)
{ {
return new NotFoundObjectResult(ex.Message) { }; return null;
//return new NotFoundObjectResult(ex.Message) { };
} }
catch (Exception ex) catch (Exception ex)
{ {
return new ObjectResult(ex.Message) { StatusCode = 500 }; return null;
//return new ObjectResult(ex.Message) { StatusCode = 500 };
} }
} }