47 lines
1.2 KiB
Go
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)
|
|
}
|