Files
kontor/kontor-gin/internal/handlers/media_lofi.go
T
Thomas Peetz 4c603a8eff
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 4s
implement command update-title
2026-10-12 17:37:06 +02:00

104 lines
2.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-contrib/slog"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
)
type MediaLoFiHandler struct {
Service *service.MediaLoFiService
}
func NewMediaLoFiHandler(db *gorm.DB) *MediaLoFiHandler {
mediaLoFiHandler := &MediaLoFiHandler{
Service: service.NewMediaLoFiService(repository.NewMediaLoFiRepository(db)),
}
return mediaLoFiHandler
}
func (handler *MediaLoFiHandler) 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.ConvertMediaLoFisToDtos(lofis)
c.JSON(http.StatusOK, dtos)
}
func (handler *MediaLoFiHandler) Get(c *gin.Context) {
id := c.Param("id")
lofi, err := handler.Service.Get(c.Request.Context(), id)
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "Lofi not found"})
return
}
c.JSON(http.StatusOK, lofi)
}
func (handler *MediaLoFiHandler) Add(c *gin.Context) {
var input models.MediaLofiAdd
if err := c.ShouldBindJSON(&input); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
lofi, err := handler.Service.Add(c.Request.Context(), input.Url)
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "lofi not added"})
return
}
c.JSON(http.StatusOK, lofi)
}
func (handler *MediaLoFiHandler) Update(c *gin.Context) {
var input models.MediaLoFiDTO
if err := c.ShouldBindJSON(&input); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
lofi, err := handler.Service.Update(c.Request.Context(), input)
if err != nil {
logger := slog.Get(c)
logger.Info("MediaLoFiHandler#Update", "error", err.Error())
c.JSON(http.StatusNotFound, gin.H{"error": "lofi not updated"})
return
}
c.JSON(http.StatusOK, lofi)
}
func (handler *MediaLoFiHandler) 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": "lofi not found"})
return
}
c.JSON(http.StatusOK, gin.H{"ok": "true"})
}
func (handler *MediaLoFiHandler) ListPage(c *gin.Context) {
session := sessions.Default(c)
user := session.Get(globals.UserKey)
lofis, err := handler.Service.List(c.Request.Context())
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.HTML(http.StatusOK, "lofi", gin.H{
"title": "Kontor MediaFiles",
"version": "0.4.0",
"message": "MediaFiles...",
"lofis": lofis,
"user": user,
})
}