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.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
}
}
/// <summary>
/// Export a configuration
/// </summary>
/// <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), 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 };
}
}