Ejemplo n.º 1
0
        public async Task <IApplicationResult> CreateForInventionsAsync(ReportInventionDto reportInventionDto)
        {
            return(await ExecuteAsync(async() =>
            {
                var byPriceRange = _inventionPredicateFactory.CreateByPriceRange(reportInventionDto.MinPrice, reportInventionDto.MaxPrice);
                var inventions = await _unitOfWork.Inventions.GetAsync(byPriceRange);

                var reportTemplate = await _templateFactory.CreateForInventionReportAsync(inventions);
                var reportTemplateRendered = await _templateService.RenderAsync <byte[]>(reportTemplate);

                if (reportTemplateRendered == null)
                {
                    return new EmptyResult
                    {
                        Status = ApplicationResultStatus.InternalServerError,
                        Message = "Report couldn't be generated"
                    };
                }

                if (reportInventionDto.SendByEmail)
                {
                    var byName = _userPredicateFactory.CreateByEmail(_inventAppContext.UserEmail);
                    var user = await _unitOfWork.Users.GetFirstAsync(byName);

                    var email = await _emailFactory.CreateForUserReportRequestedAsync(user, reportTemplateRendered);

                    _emailService.SendAsync(email);
                }

                //TODO: return to UI
                File.WriteAllBytes($"{_appSettingsService.ReportsDirectory}\\inventions_{Guid.NewGuid()}.pdf", reportTemplateRendered);

                return new OkEmptyResult();
            }));
        }