Compare commits

...

3 Commits

4 changed files with 216 additions and 0 deletions

73
handlers/certificate.go Normal file
View File

@ -0,0 +1,73 @@
package handlers
import (
"log"
"slices"
"git.odit.services/lfk/document-server/models"
"git.odit.services/lfk/document-server/services"
"git.odit.services/lfk/document-server/templates"
"github.com/gofiber/fiber/v2"
)
// GenerateCertificate godoc
// @Summary Generate runner certificates
// @Description Generate certificates based on the provided data
// @Tags certificates
// @Accept json
// @Param data body models.CertificateRequest true "Certificate data"
// @Produce application/pdf
// @Router /certificates [post]
func GenerateCertificate(c *fiber.Ctx) error {
certificateRequest := new(models.CertificateRequest)
if err := c.BodyParser(certificateRequest); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"error": err.Error(),
})
}
if !slices.Contains([]string{"en", "de"}, certificateRequest.Locale) {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"error": "Invalid locale",
})
}
generator := services.DefaultTemplater{}
templateString, err := templates.GetTemplate(certificateRequest.Locale, "certificate")
if err != nil {
log.Println(err)
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"error": "Template not found",
})
}
template, err := generator.StringToTemplate(templateString)
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"error": err.Error(),
})
}
genConfig := &models.CertificateTemplateOptions{
Runners: certificateRequest.Runners,
EventName: "Event name",
Footer: "Footer",
CurrencySymbol: "€",
}
result, err := generator.Execute(template, genConfig)
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"error": err.Error(),
})
}
c.Set(fiber.HeaderContentType, "text/html")
converter := services.GotenbergConverter{BaseUrl: "http://localhost:3001"}
pdf, err := converter.ToPdf(result, "a4", false)
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"error": err.Error(),
})
}
c.Set(fiber.HeaderContentType, "application/pdf")
return c.Send(pdf)
}

View File

@ -36,6 +36,7 @@ func main() {
})
v1.Post("/contracts", handlers.GenerateContract)
v1.Post("/cards", handlers.GenerateCard)
v1.Post("/certificates", handlers.GenerateCertificate)
app.Use(handlers.NotFoundHandler)
docs.SwaggerInfo.BasePath = "/"

40
models/certificate.go Normal file
View File

@ -0,0 +1,40 @@
package models
type CertificateRequest struct {
Runners []RunnerWithDonations `json:"runners"`
Locale string `json:"locale" enums:"en,de"`
}
type RunnerWithDonations struct {
ID int `json:"id"`
FirstName string `json:"first_name"`
MiddleName string `json:"middle_name"`
LastName string `json:"last_name"`
Group Group `json:"group"`
Distance int `json:"distance"`
DistanceDonations []DistanceDonation `json:"distance_donations"`
TotalPerDistance int `json:"total_per_distance"`
TotalDonations int `json:"total_donations"`
}
type DistanceDonation struct {
ID int `json:"id"`
Amount int `json:"amount"`
PaidAmount int `json:"paid_amount"`
AmountPerDistance int `json:"amount_per_distance"`
Donor Donor `json:"donor"`
}
type Donor struct {
ID int `json:"id"`
FirstName string `json:"first_name"`
MiddleName string `json:"middle_name"`
LastName string `json:"last_name"`
}
type CertificateTemplateOptions struct {
Runners []RunnerWithDonations `json:"runners"`
EventName string `json:"event_name"`
Footer string `json:"footer"`
CurrencySymbol string `json:"currency_symbol"`
}

File diff suppressed because one or more lines are too long