feat(handlers): Implement barcode generation

This commit is contained in:
Nicolai Ort 2024-12-12 16:49:33 +01:00
parent b6cc98a165
commit 0f2452eca0
Signed by: niggl
GPG Key ID: 13AFA55AF62F269F
5 changed files with 40 additions and 20 deletions

View File

@ -5,6 +5,7 @@ import (
)
// GenerateBarcode godoc
//
// @Summary Generate barcodes
// @Description Generate barcodes based on the provided data
// @Tags barcodes
@ -13,6 +14,19 @@ import (
// @Produce image/png
// @Router /v1/barcodes/{type}/{content} [get]
func (h *DefaultHandler) GenerateBarcode(c *fiber.Ctx) error {
c.Set(fiber.HeaderContentType, "text/plain")
return c.Send([]byte("Hello, World!"))
// Get the type and content from the URL
barcodeType := c.Params("type")
barcodeContent := c.Params("content")
// Generate the barcode
barcode, err := h.BarcodeService.GenerateBarcode(barcodeType, barcodeContent, 1000, 1000)
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"error": err.Error(),
})
}
c.Set(fiber.HeaderContentType, "image/png")
return c.Send(barcode.Bytes())
}

View File

@ -2,6 +2,7 @@ package handlers
import (
"git.odit.services/lfk/document-server/models"
"git.odit.services/lfk/document-server/services"
"github.com/gofiber/fiber/v2"
)
@ -9,8 +10,10 @@ type Handler interface {
GenerateCard(*fiber.Ctx) error
GenerateContract(*fiber.Ctx) error
GenerateCertificate(*fiber.Ctx) error
GenerateBarcode(*fiber.Ctx) error
}
type DefaultHandler struct {
Config *models.Config
BarcodeService services.BarcodeService
}

View File

@ -8,6 +8,7 @@ import (
"git.odit.services/lfk/document-server/docs" // Correct import path for docs
"git.odit.services/lfk/document-server/handlers"
"git.odit.services/lfk/document-server/models"
"git.odit.services/lfk/document-server/services"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/keyauth"
"github.com/gofiber/swagger"
@ -72,8 +73,10 @@ func main() {
log.Fatal(err)
}
barcodeGenerator := &services.DefaultBarcodeService{}
handler := handlers.DefaultHandler{
Config: config,
BarcodeService: barcodeGenerator,
}
// Create a new Fiber instance

View File

@ -12,15 +12,15 @@ import (
"github.com/boombuler/barcode/qr"
)
type BarcodeGenerator interface {
GenerateBarcode(format string, content string) (bytes.Buffer, error)
type BarcodeService interface {
GenerateBarcode(format string, content string, width int, height int) (bytes.Buffer, error)
IsTypeSupported(format string) bool
}
type DefaultBarcodeGenerator struct {
type DefaultBarcodeService struct {
}
func (b *DefaultBarcodeGenerator) GenerateBarcode(format string, content string, width int, height int) (bytes.Buffer, error) {
func (b *DefaultBarcodeService) GenerateBarcode(format string, content string, width int, height int) (bytes.Buffer, error) {
if !b.IsTypeSupported(format) {
return bytes.Buffer{}, fmt.Errorf("unsupported barcode type: %s", format)
}
@ -63,7 +63,7 @@ func (b *DefaultBarcodeGenerator) GenerateBarcode(format string, content string,
return buf, nil
}
func (b *DefaultBarcodeGenerator) IsTypeSupported(format string) bool {
func (b *DefaultBarcodeService) IsTypeSupported(format string) bool {
supportedTypes := []string{"ean13", "code128", "qr"}
return slices.Contains(supportedTypes, format)
}

View File

@ -31,7 +31,7 @@ func idToEan13(id string, prefix string) (string, error) {
}
func (t *DefaultTemplater) GenerateBarcode(code string, format string, prefix string) (string, error) {
gen := &DefaultBarcodeGenerator{}
gen := &DefaultBarcodeService{}
var err error
if format == "ean13" {