86 lines
2.4 KiB
Go
86 lines
2.4 KiB
Go
package handlers
|
|
|
|
import (
|
|
"kontor-api-echo/internal/globals"
|
|
"kontor-api-echo/internal/models"
|
|
"kontor-api-echo/internal/repository"
|
|
"kontor-api-echo/internal/service"
|
|
"net/http"
|
|
|
|
"github.com/labstack/echo/v5"
|
|
"github.com/uptrace/bun"
|
|
)
|
|
|
|
type MediaActorFileHandler struct {
|
|
Service *service.MediaActorFileService
|
|
}
|
|
|
|
func NewMediaActorFileHandler(db *bun.DB) *MediaActorFileHandler {
|
|
mediaActorFileHandler := &MediaActorFileHandler{
|
|
Service: service.NewMediaActorFileService(repository.NewMediaActorFileRepository(db)),
|
|
}
|
|
return mediaActorFileHandler
|
|
}
|
|
|
|
func (handler *MediaActorFileHandler) List(c *echo.Context) {
|
|
media_actor_files, err := handler.Service.List(c.Request().Context())
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()})
|
|
return
|
|
}
|
|
dtos := models.ConvertMediaActorFilesToDtos(media_actor_files)
|
|
c.JSON(http.StatusOK, dtos)
|
|
}
|
|
|
|
func (handler *MediaActorFileHandler) Get(c *echo.Context) {
|
|
id := c.Param("id")
|
|
actor, err := handler.Service.Get(c.Request().Context(), id)
|
|
if err != nil {
|
|
c.JSON(http.StatusNotFound, map[string]string{"error": "sport not found"})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, actor)
|
|
}
|
|
|
|
func (handler *MediaActorFileHandler) Add(c *echo.Context) {
|
|
var input models.MediaActorFileAdd
|
|
|
|
if err := c.Bind(&input); err != nil {
|
|
c.JSON(http.StatusBadRequest, map[string]string{"error": err.Error()})
|
|
return
|
|
}
|
|
actorfile, err := handler.Service.Add(c.Request().Context(), input.MediaActorID, input.MediaFileID)
|
|
if err != nil {
|
|
c.JSON(http.StatusNotFound, map[string]string{"error": "lofi not added"})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, actorfile)
|
|
}
|
|
|
|
func (handler *MediaActorFileHandler) Delete(c *echo.Context) {
|
|
id := c.Param("id")
|
|
err := handler.Service.Delete(c.Request().Context(), id)
|
|
if err != nil {
|
|
c.JSON(http.StatusNotFound, map[string]string{"error": "sport not found"})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, map[string]string{"ok": "true"})
|
|
}
|
|
|
|
func (handler *MediaActorFileHandler) ListPage(c *echo.Context) {
|
|
cookie, err := c.Cookie(globals.UserKey)
|
|
user := cookie.Value
|
|
media_actor_files, err := handler.Service.List(c.Request().Context())
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()})
|
|
return
|
|
}
|
|
c.Render(http.StatusOK, "mediaactorfiles", map[string]string{
|
|
"title": "Kontor MediaActorFiles",
|
|
"version": "0.4.0",
|
|
"message": "MediaActorFiles...",
|
|
"actors": media_actor_files,
|
|
"user": user,
|
|
})
|
|
}
|