Files
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

76 lines
2.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 ComicsHandler struct {
ComicHandler *ComicHandler
PublisherHandler *PublisherHandler
ArtistHandler *ArtistHandler
IssueHandler *IssueHandler
WorkTypeHandler *WorkTypeHandler
VolumeHandler *VolumeHandler
StoryArcHandler *StoryArcHandler
ComicworkHandler *ComicworkHandler
IssueworkHandler *IssueworkHandler
TradePaperbackHandler *TradePaperbackHandler
}
func NewComicsHandler(db *gorm.DB) *ComicsHandler {
comicsHandler := &ComicsHandler{
ComicHandler: NewComicHandler(db),
PublisherHandler: NewPublisherHandler(db),
ArtistHandler: NewArtistHandler(db),
IssueHandler: NewIssueHandler(db),
WorkTypeHandler: NewWorkTypeHandler(db),
VolumeHandler: NewVolumeHandler(db),
StoryArcHandler: NewStoryArcHandler(db),
ComicworkHandler: NewComicworkHandler(db),
IssueworkHandler: NewIssueworkHandler(db),
TradePaperbackHandler: NewTradePaperbackHandler(db),
}
return comicsHandler
}
type ComicHandler struct {
Service *service.ComicService
}
func NewComicHandler(db *gorm.DB) *ComicHandler {
handler := &ComicHandler{Service: service.NewComicService(repository.NewComicRepository(db))}
return handler
}
func (handler *ComicHandler) 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.ConvertComicsToDtos(comics)
c.JSON(http.StatusOK, dtos)
}
func (handler *ComicHandler) ListPage(c *gin.Context) {
session := sessions.Default(c)
user := session.Get(globals.UserKey)
comics, err := handler.Service.List(c.Request.Context())
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
data := globals.GetDefaultValues(user, "Comics...")
data["title"] = "Kontor Comics"
data["comics"] = comics
c.HTML(http.StatusOK, "comics", data)
}