Files
kontor/kontor-gin/internal/handlers/comic.go
T

65 lines
1.7 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
}
func NewComicsHandler(db *gorm.DB) *ComicsHandler {
comicsHandler := &ComicsHandler{
ComicHandler: &ComicHandler{Service: service.NewComicService(repository.NewComicRepository(db))},
PublisherHandler: &PublisherHandler{Service: service.NewPublisherService(repository.NewPublisherRepository(db))},
}
return comicsHandler
}
type ComicHandler struct {
Service *service.ComicService
}
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
}
c.HTML(http.StatusOK, "comics", gin.H{
"title": "Kontor Comics",
"version": "0.4.0",
"message": "Comics...",
"comics": comics,
"user": user,
})
}