Exemple #1
0
        public async Task <IActionResult> OnUpload(IList <IFormFile> files)
        {
            if (files.Any())
            {
                var file = files[0];

                try
                {
                    var format = file.FileName.Substring(file.FileName.LastIndexOf(".") + 1);
                    _uploadedFIleInfoValidator.Validate(format, file.Length);

                    var result = new StringBuilder();

                    using (var reader = new StreamReader(file.OpenReadStream()))
                    {
                        var transactionsModel = _parser.ParseValidate(reader, format);
                        if (transactionsModel.Any())
                        {
                            await _transactionsService.SaveTransactions(transactionsModel.ToList());

                            return(Ok());
                        }
                        else
                        {
                            return(BadRequest("No records found"));
                        }
                    }
                }
                catch (InvalidFileFormatException ex)
                {
                    return(BadRequest(ex.Message));
                }
                catch (MaxFileSizeException ex)
                {
                    return(BadRequest(ex.Message));
                }
                catch (FileParseValidationException fpex)
                {
                    return(StatusCode(StatusCodes.Status500InternalServerError, fpex.Message));
                }
                catch (Exception ex)
                {
                    return(StatusCode(StatusCodes.Status500InternalServerError, ex.Message));
                }
            }

            return(BadRequest("No input file"));
        }