Exemplo n.º 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()));
        }
Exemplo n.º 2
0
        private void PublishMessage(string groupId, string pdfId, string routingKey)
        {
            var pdfUri = _uris.PdfUri(groupId, pdfId);

            using var connection = new ConnectionFactory { HostName = _mqConfig.Host }.CreateConnection();
            using var channel    = connection.CreateModel();

            var data = JObject.FromObject(new
            {
                id        = Guid.NewGuid(),
                timeStamp = DateTime.UtcNow,
                data      = new
                {
                    groupId   = groupId,
                    pdfId     = pdfId,
                    pdfUri    = pdfUri,
                    timeStamp = DateTime.UtcNow,
                }
            }).ToString();

            channel.BasicPublish(exchange: Exhcange,
                                 routingKey: routingKey,
                                 body: Encoding.UTF8.GetBytes(data));

            _logger.LogInformation($"Message '{routingKey}': {data}");
        }