The Controllers directory contains the API controllers that handle HTTP requests and responses for the Giddh Template Service.
Purpose: Provides basic health check functionality
Endpoints:
GET /- Returns service status
Implementation:
[ApiController]
public class MainController : ControllerBase
{
[HttpGet("/")]
public IActionResult Get()
{
return Ok("Hello from Giddh template!");
}
}Usage:
- Health monitoring
- Service availability verification
- Load balancer health checks
Purpose: Main API controller for PDF generation functionality
public PdfController(PdfService pdfService, ISlackService slackService, IConfiguration configuration)Dependencies:
PdfService- Core PDF generation logicISlackService- Error notification serviceIConfiguration- Environment configuration access
Purpose: Generate PDF invoice from JSON data
Request:
- Method: POST
- Content-Type:
application/json - Body: JSON object matching
Rootmodel structure
Response:
- Success (200): PDF file with headers:
Content-Type: application/pdfContent-Disposition: attachment; filename="invoice.pdf"
- Error (400): Invalid request format
- Error (500): PDF generation failure
Implementation Flow:
-
Request Deserialization:
var jsonString = JsonSerializer.Serialize(requestObj); Root request = JsonSerializer.Deserialize<Root>(jsonString, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
-
Validation:
if (request == null || string.IsNullOrEmpty(request.Company?.Name)) { return BadRequest("Invalid request data. Ensure payload matches expected format."); }
-
PDF Generation:
byte[] pdfBytes = await _pdfService.GeneratePdfAsync(request);
-
Error Handling:
if (pdfBytes == null || pdfBytes.Length == 0) { await _slackService.SendErrorAlertAsync( url: "api/v1/pdf", environment: _environment, error: "PDF generation returned empty result.", stackTrace: "No stacktrace (service returned empty bytes)." ); return StatusCode(500, new { error = "Failed to generate PDF!" }); }
-
Response:
return File(pdfBytes, "application/pdf", "invoice.pdf");
- Missing required fields in request
- Invalid JSON structure
- Null or empty company name
- PDF generation failures
- Template rendering issues
- Browser automation problems
- Automatic Slack notification for production issues
Environment Detection:
_environment = Environment.GetEnvironmentVariable("ENVIRONMENT");Usage:
- Error alert context
- Environment-specific behavior
- Logging correlation
- Resource-based URLs (
/api/v1/pdf) - HTTP status codes for response indication
- JSON content negotiation
- Constructor injection for services
- Interface-based abstractions
- Scoped service lifetimes
- Non-blocking PDF generation
- Scalable request handling
- Proper resource disposal
- Mock
PdfServicefor controller logic testing - Mock
ISlackServicefor error handling verification - Test request validation scenarios
- End-to-end PDF generation flow
- Error handling with real services
- Performance testing with large payloads
{
"company": {
"name": "Test Company",
"address": "123 Test Street"
},
"voucherNumber": "INV-001",
"voucherDate": "2025-12-23",
"entries": [
{
"accountName": "Test Item",
"amount": {
"amountForAccount": 100.00
}
}
]
}- JSON deserialization with case-insensitive options
- Required field validation
- Payload size limitations (implicit via ASP.NET Core)
- Generic error messages for client responses
- Detailed logging for internal debugging
- Sensitive information excluded from responses
- Non-blocking PDF generation
- Concurrent request handling
- Resource-efficient processing
- Byte array handling for PDF data
- Proper disposal of resources
- Streaming responses for large files
Author/Developer: Divyanshu Shrivastava
Last Updated: December 2025