Files
kontor/kontor-gin/internal/handlers/publisher.go
T
Thomas Peetz 2889927416
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 6s
Gitea Actions Demo / build-and-test (push) Failing after 2m49s
change templates
2026-10-25 04:05:11 +01:00

47 lines
1.2 KiB
Go

package handlers
import (
"kontor-api-gin/internal/globals"
"kontor-api-gin/internal/models"
"kontor-api-gin/internal/repository"
"kontor-api-gin/internal/service"
"net/http"
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
)
type PublisherHandler struct {
Service *service.PublisherService
}
func NewPublisherHandler(db *gorm.DB) *PublisherHandler {
handler := &PublisherHandler{Service: service.NewPublisherService(repository.NewPublisherRepository(db))}
return handler
}
func (handler *PublisherHandler) List(c *gin.Context) {
comics, err := handler.Service.List(c.Request.Context())
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
dtos := models.ConvertPublishersToDtos(comics)
c.JSON(http.StatusOK, dtos)
}
func (handler *PublisherHandler) ListPage(c *gin.Context) {
session := sessions.Default(c)
user := session.Get(globals.UserKey)
publishers, err := handler.Service.List(c.Request.Context())
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
data := globals.GetDefaultValues(user, "Publishers...")
data["title"] = "Kontor Comics"
data["publishers"] = publishers
c.HTML(http.StatusOK, "publishers", data)
}