|
| 1 | +package org.spring.content; |
| 2 | + |
| 3 | +import freemarker.template.TemplateException; |
| 4 | +import org.springframework.http.ResponseEntity; |
| 5 | +import org.springframework.web.bind.annotation.PostMapping; |
| 6 | +import org.springframework.web.bind.annotation.RequestBody; |
| 7 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 8 | +import org.springframework.web.bind.annotation.RestController; |
| 9 | + |
| 10 | +import java.io.IOException; |
| 11 | + |
| 12 | +/** |
| 13 | + * Controller for campaign and notification template rendering. |
| 14 | + */ |
| 15 | +@RestController |
| 16 | +@RequestMapping("/api/campaigns") |
| 17 | +public class CampaignController { |
| 18 | + |
| 19 | + private final MarketingTemplateService marketingService; |
| 20 | + private final NotificationTemplateService notificationService; |
| 21 | + private final TemplateRenderingService templateService; |
| 22 | + |
| 23 | + public CampaignController(MarketingTemplateService marketingService, |
| 24 | + NotificationTemplateService notificationService, |
| 25 | + TemplateRenderingService templateService) { |
| 26 | + this.marketingService = marketingService; |
| 27 | + this.notificationService = notificationService; |
| 28 | + this.templateService = templateService; |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Preview a marketing email template before sending to customers. |
| 33 | + */ |
| 34 | + @PostMapping("/marketing/preview") |
| 35 | + public ResponseEntity<String> previewMarketing(@RequestBody RenderRequest request) { |
| 36 | + try { |
| 37 | + String result = marketingService.render(request.getTemplateName(), request.getTemplateContent()); |
| 38 | + return ResponseEntity.ok(result); |
| 39 | + } catch (IOException | TemplateException e) { |
| 40 | + return ResponseEntity.badRequest().body("Preview failed: " + e.getMessage()); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Render a notification template with hardened configuration. |
| 46 | + */ |
| 47 | + @PostMapping("/notifications/render") |
| 48 | + public ResponseEntity<String> renderNotification(@RequestBody RenderRequest request) { |
| 49 | + try { |
| 50 | + String result = notificationService.render(request.getTemplateName(), request.getTemplateContent()); |
| 51 | + return ResponseEntity.ok(result); |
| 52 | + } catch (IOException | TemplateException e) { |
| 53 | + return ResponseEntity.badRequest().body("Render failed: " + e.getMessage()); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Renders a custom template from user input. |
| 59 | + * Data flows from request body → DTO fields → service → template engine. |
| 60 | + */ |
| 61 | + @PostMapping("/render") |
| 62 | + public ResponseEntity<String> renderTemplate(@RequestBody RenderRequest request) { |
| 63 | + try { |
| 64 | + String result = templateService.renderFromRequest(request); |
| 65 | + return ResponseEntity.ok(result); |
| 66 | + } catch (Exception e) { |
| 67 | + return ResponseEntity.badRequest().body("Template processing failed: " + e.getMessage()); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Alternative endpoint demonstrating data flow through object field assignment. |
| 73 | + */ |
| 74 | + @PostMapping("/render/inline") |
| 75 | + public ResponseEntity<String> renderInline(@RequestBody RenderRequest request) { |
| 76 | + try { |
| 77 | + String templateContent = request.getTemplateContent(); |
| 78 | + String result = templateService.renderFromContent(templateContent); |
| 79 | + return ResponseEntity.ok(result); |
| 80 | + } catch (Exception e) { |
| 81 | + return ResponseEntity.badRequest().body("Processing failed: " + e.getMessage()); |
| 82 | + } |
| 83 | + } |
| 84 | +} |
0 commit comments