87 lines
2.2 KiB
Go
87 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 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) Add(c *gin.Context) {
|
|
var input models.MediaActorAdd
|
|
|
|
if err := c.ShouldBindJSON(&input); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
actor, err := handler.Service.Add(c.Request.Context(), input.WebLink)
|
|
if err != nil {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "lofi not added"})
|
|
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,
|
|
})
|
|
}
|