Ejemplo n.º 1
0
        public async Task <IActionResult> Download(DateTime fromDate, DateTime toDate)
        {
            try
            {
                //TODO: Move this validation logic to a reusable method
                if (fromDate == DateTime.MinValue || toDate == DateTime.MinValue)
                {
                    ViewData[ApplicationConstants.ViewDataInvalidInputErrorKey] = ApplicationConstants.InvalidInputErrorString;

                    return(View(ApplicationConstants.InvalidInputErrorView));
                }

                //Format the dates
                FormatDates(fromDate, toDate, out string formattedFromDate, out string formattedToDate);

                //Service composition
                var data = await _intervalRepository.GetIntervalData(formattedFromDate, formattedToDate);

                var stream = await _excelService.GetDownloadStream(data);

                string excelFileName = $"{_settings.Value.FileName}{DateTime.Now.ToString(_settings.Value.FileNameDateFormatString)}{_settings.Value.ExcelExtension}";

                return(File(stream, $"{_settings.Value.ContentType}", excelFileName));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, nameof(Download), new string[] { fromDate.ToString(), toDate.ToString() });
            }

            ViewData[ApplicationConstants.ViewDataDownloadErrorKey] = ApplicationConstants.DownloadFailedErrorString;

            return(View(ApplicationConstants.ErrorView));
        }