Example #1
0
        public ActionResult <IEnumerable <NewPdfResponse> > AddNewPdf([Required][FromRoute] string groupId, [FromBody] NewPdfRequest request)
        {
            if (!request.RowData.Any())
            {
                return(BadRequest("Expected to get atleast one 'rowData' element, but got none."));
            }

            var responses = request.RowData.Select(row =>
            {
                var entity = _context.PdfFiles.Add(new PdfEntity(groupId, PdfType.Pdf)
                {
                    Options = request.Options
                }).Entity;
                var templatedRow = TemplateUtils.MergeBaseTemplatingWithRows(request.BaseData, row);
                PersistParsedHtmlTemplateOfPdfDocument(entity, request.Html, templatedRow);

                _context.SaveChanges();

                entity.HangfireJobId =
                    _backgroundJobs.Enqueue <IPdfQueue>(que => que.CreatePdf(entity.Id));

                _context.SaveChanges();

                var pdfUri  = _uris.PdfUri(groupId, entity.FileId);
                var htmlUri = _uris.HtmlUri(groupId, entity.FileId);

                return(new NewPdfResponse(entity.FileId, entity.GroupId, pdfUri, htmlUri, row));
            });

            return(Accepted(responses.ToList()));
        }