Files
kontor/kontor-gin/internal/handlers/media_actor.go
T
tpeetz 32f3f034eb
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 3s
add tests for repository
2026-10-06 00:16:03 +02:00

72 lines
1.8 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 MediaActorHandler struct {
Service *service.MediaActorService
}
func NewMediaActorHandler(db *gorm.DB) *MediaActorHandler {
mediaActorHandler := &MediaActorHandler{
Service: service.NewMediaActorService(repository.NewMediaActorRepository(db)),
}
return mediaActorHandler
}
func (handler *MediaActorHandler) List(c *gin.Context) {
lofis, err := handler.Service.List(c.Request.Context())
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
dtos := models.ConvertMediaActorsToDtos(lofis)
c.JSON(http.StatusOK, dtos)
}
func (handler *MediaActorHandler) Get(c *gin.Context) {
id := c.Param("id")
actor, err := handler.Service.Get(c.Request.Context(), id)
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "sport not found"})
return
}
c.JSON(http.StatusOK, actor)
}
func (handler *MediaActorHandler) Delete(c *gin.Context) {
id := c.Param("id")
err := handler.Service.Delete(c.Request.Context(), id)
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "sport not found"})
return
}
c.JSON(http.StatusOK, gin.H{"ok": "true"})
}
func (handler *MediaActorHandler) ListPage(c *gin.Context) {
session := sessions.Default(c)
user := session.Get(globals.UserKey)
actors, err := handler.Service.List(c.Request.Context())
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.HTML(http.StatusOK, "mediaactors", gin.H{
"title": "Kontor MediaActors",
"version": "0.4.0",
"message": "MediaActors...",
"actors": actors,
"user": user,
})
}