Untitled

mail@pastecode.io avatar
unknown
csharp
2 years ago
6.9 kB
6
Indexable
Never
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Swashbuckle.AspNetCore.Annotations;
using System.IO;
using TeamSystem.AlyCE.Services.Common.DTO;
using TeamSystem.AlyCE.Services.Controllers.Attribute;
using TeamSystem.AlyCE.Services.FW_GeneralData.DTO;
using TeamSystem.AlyCE.Services.FW_GeneralData.Managers;

namespace TeamSystem.AlyCE.Services.FW_GeneralData.Controllers
{
    public partial class LicenseFWController
    {
        internal const string ImportOp = "import";

        internal const string DeleteAllOp = "delete_all";

        internal const string DownloadAllOP = "download_all";

        internal const string DownloadImportOP = "download_import";

        internal const string DownloadImportPolyedroOP = "download_importpolyedro";

        public override bool LicenseCheck => false;

        [HttpPost(ImportOp)]
        [ValidateMimeMultipartContentFilter]
        [SwaggerOperation("Import License File")]
        public IActionResult Upload([FromForm] FileFormData formData, [FromForm, SwaggerParameter("Overwrite parameter", Required = false)] bool? overwrite)
        {
            if (formData.File == null)
                return StatusCode(StatusCodes.Status415UnsupportedMediaType);

            //// Check if the request contains multipart/form-data.  
            //if (!MultipartHelper.IsMultipartContentType(Request.ContentType))
            //{
            //    return StatusCode(StatusCodes.Status415UnsupportedMediaType);
            //}

            if (formData.File.ContentType != "text/plain")
            {
                string err = $"Expected 'Content-Type=text/plain' not found in the request currrent: {formData.File.ContentType}";
                Logger.Error(err);
                return BadRequest(err);
            }
            using (Stream ms = formData.File.OpenReadStream())
            {
                using (var reader = new StreamReader(ms))
                {
                    string content = reader.ReadToEnd();
                    ImportLicensesOperationResult result = Manager.ImportLicenses(content, (bool)overwrite);
                    return StatusCode(result.StatusCode, result.Content);
                }
            }
        }

        [HttpPost(DeleteAllOp)]
        [SwaggerOperation(Summary = "Delete more licences together", Description = "Delete more licences together")]
        [SwaggerResponse(StatusCodes.Status200OK, "If status of operation is ok")]
        [SwaggerResponse(StatusCodes.Status400BadRequest, "If there are errors in the request")]
        [SwaggerResponse(StatusCodes.Status409Conflict, "In case of errors in the business logic: the content of the response contains the validation messages", typeof(ValidateDTO))]
        public IActionResult DeleteAll()
        {
            DeleteAllLicensesResult result = Manager.DeleteAll();
            return StatusCode(result.StatusCode, result.Content);
        }

        [HttpPost(DownloadImportOP)]
        [SwaggerOperation(Summary = "Download and import licenses", Description = "Download and import licenses")]
        [SwaggerResponse(StatusCodes.Status200OK, "If the operation is successful")]
        [SwaggerResponse(StatusCodes.Status400BadRequest, "If there are errors in the request")]
        [SwaggerResponse(StatusCodes.Status409Conflict, "In case of errors in the business logic: the content of the response contains the validation messages", typeof(ValidateDTO))]
        public IActionResult DownloadImport([FromBody, SwaggerRequestBody("Request paramters", Required = true)] LicensesParams @params)
        {
            if (@params == null)
                return BadRequest("Missing body parameter");
            if (@params.VatCode == null)
                return BadRequest("DownloadLicensesParams.VatCode not set");
            if (@params.VarVatCode == null)
                return BadRequest("DownloadLicensesParams.VarVatCode not set");

            ImportLicensesOperationResult result = Manager.DownloadImportLicenses(@params);
            if (result.StatusCode == StatusCodes.Status200OK)
                return StatusCode(result.StatusCode, result.Licenses ?? "OK");
            return StatusCode(result.StatusCode, result.Content);
        }

        [HttpPost(DownloadAllOP)]
        [SwaggerOperation(Summary = "Download and import all licenses", Description = "Download and import all licenses")]
        [SwaggerResponse(StatusCodes.Status200OK, "If the operation is successful")]
        [SwaggerResponse(StatusCodes.Status400BadRequest, "If there are errors in the request")]
        [SwaggerResponse(StatusCodes.Status409Conflict, "In case of errors in the business logic: the content of the response contains the validation messages", typeof(ValidateDTO))]
        public IActionResult DownloadAll([FromBody, SwaggerRequestBody("Parametri della richiesta", Required = true)] LicensesParams @params)
        {
            if (@params == null)
                return BadRequest("Missing body parameter");
            if (@params.VatCode == null)
                return BadRequest("DownloadLicensesParams.VatCode not set");
            if (@params.VarVatCode == null)
                return BadRequest("DownloadLicensesParams.VarVatCode not set");

            ImportLicensesOperationResult result = Manager.DownloadAllLicenses(@params);
            if (result.StatusCode == StatusCodes.Status200OK)
                return StatusCode(result.StatusCode, result.Licenses ?? "OK");
            return StatusCode(result.StatusCode, result.Content);
        }

        [HttpPost(DownloadImportPolyedroOP)]
        [SwaggerOperation(Summary = "Download and import Polyedro licenses", Description = "Download and import Polyedro licenses")]
        [SwaggerResponse(StatusCodes.Status200OK, "If the operation is successful")]
        [SwaggerResponse(StatusCodes.Status400BadRequest, "If there are errors in the request")]
        [SwaggerResponse(StatusCodes.Status409Conflict, "In case of errors in the business logic: the content of the response contains the validation messages", typeof(ValidateDTO))]
        public IActionResult DownloadImportPolyedro([FromBody, SwaggerRequestBody("Parametri della richiesta", Required = true)] LicensesParams @params)
        {
            if (@params == null)
                return BadRequest("Missing body parameter");
            if (@params.VatCode == null)
                return BadRequest("DownloadLicensesParams.VatCode not set");
            if (@params.VarVatCode == null)
                return BadRequest("DownloadLicensesParams.VarVatCode not set");

            ImportLicensesOperationResult result = Manager.DownloadImportPolyedroLicenses(@params);
            return StatusCode(result.StatusCode, result.Content);
        }
    }
}