From f131300e5db00e8d1526c0467b621849f32ab5ee Mon Sep 17 00:00:00 2001 From: Fabian Eichhorner Date: Sat, 3 Nov 2018 23:29:01 +0100 Subject: [PATCH] Issue #3: - added demo route - updated post route --- PdfService/Controllers/PdfController.cs | 55 +++++++++++++++++++++++-- PdfService/readme.md | 16 +++++-- 2 files changed, 64 insertions(+), 7 deletions(-) diff --git a/PdfService/Controllers/PdfController.cs b/PdfService/Controllers/PdfController.cs index 5638d06..a47cbb1 100644 --- a/PdfService/Controllers/PdfController.cs +++ b/PdfService/Controllers/PdfController.cs @@ -17,11 +17,60 @@ namespace PdfService.Controllers { _converter = converter; } + + [Route("")] + [HttpGet] + public ActionResult GeneratePdfDemo() + { + var globalSettings = new GlobalSettings + { + ColorMode = ColorMode.Color, + Orientation = Orientation.Portrait, + PaperSize = PaperKind.A4, + Margins = new MarginSettings { Top = 0, Left = 0, Right = 0 }, + DocumentTitle = "PDF Demo Report", + }; + + var objectSettings = new ObjectSettings + { + PagesCount = true, + HtmlContent = TemplateGenerator.GetHTMLString(new Tweet[]{ + new Tweet("Test", "Good"), + new Tweet("Test2", "Bad"), + new Tweet("Another Tweet", "Bad"), + new Tweet("Another bad Tweet", "Bad") + }), + WebSettings = { DefaultEncoding = "utf-8", UserStyleSheet = Path.Combine(Directory.GetCurrentDirectory(), "assets", "bulma.min.css") } + }; + + var pdf = new HtmlToPdfDocument() + { + GlobalSettings = globalSettings, + Objects = { objectSettings } + }; + + var file = _converter.Convert(pdf); + return File(file, "application/pdf"); + } + // GET /generatePDF [Route("generatePDF")] - [HttpGet] - public ActionResult Get() + [HttpPost] + public ActionResult GeneratePdf([FromBody] Tweet[] tweets) { + // check parameters + if (!ModelState.IsValid) + { + return BadRequest(ModelState); + } + + // check for emptyString + foreach(Tweet tweet in tweets) { + if(tweet.Name.Length<1 || tweet.Sentiment.Length<1){ + return BadRequest(ModelState); + } + } + var globalSettings = new GlobalSettings { ColorMode = ColorMode.Color, @@ -34,7 +83,7 @@ namespace PdfService.Controllers var objectSettings = new ObjectSettings { PagesCount = true, - HtmlContent = TemplateGenerator.GetHTMLString(new Tweet[]{new Tweet("Test", "Good"), new Tweet("Test2", "Bad")}), + HtmlContent = TemplateGenerator.GetHTMLString(tweets), WebSettings = { DefaultEncoding = "utf-8", UserStyleSheet = Path.Combine(Directory.GetCurrentDirectory(), "assets", "bulma.min.css") } }; diff --git a/PdfService/readme.md b/PdfService/readme.md index b8c16ee..03be834 100644 --- a/PdfService/readme.md +++ b/PdfService/readme.md @@ -1,12 +1,20 @@ # PDF Service -Provides an API interface to generate PDF reports for given tweets and their sentiment analysis result. -`POST`: `/generatePDF/` (TODO) +Provides an API interface to generate PDF reports for given tweets and their sentiment analysis result. +`GET`: `/` Shows a demo page +- param: none +- return: pdf file +`POST`: `/generatePDF/` Generates a pdf report for the given tweets and sentiments +- param: Tweet[] as Content-Type: `application/json` +- return: pdf file + +### Tweet model +Tweet(string Name, string Sentiment) ## run with docker -- (if no local image is available) `docker build -t pdfservice .` -- `docker run -p YOUR_PORT:80 pdfservice:latest` +- `docker build -t service-reporting .` +- `docker run -p YOUR_PORT:80 service-reporting:latest` ## run local ### requirements -- 2.43.0