update kontor-echo
This commit is contained in:
@@ -0,0 +1,137 @@
|
||||
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 MediaActorHandler struct {
|
||||
Service *service.MediaActorService
|
||||
}
|
||||
|
||||
func NewMediaActorHandler(db *bun.DB) *MediaActorHandler {
|
||||
mediaActorHandler := &MediaActorHandler{
|
||||
Service: service.NewMediaActorService(repository.NewMediaActorRepository(db)),
|
||||
}
|
||||
return mediaActorHandler
|
||||
}
|
||||
|
||||
func (handler *MediaActorHandler) List(c *echo.Context) {
|
||||
lofis, err := handler.Service.List(c.Request().Context())
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
dtos := models.ConvertMediaActorsToDtos(lofis)
|
||||
c.JSON(http.StatusOK, dtos)
|
||||
}
|
||||
|
||||
func (handler *MediaActorHandler) 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 *MediaActorHandler) Add(c *echo.Context) {
|
||||
var input models.MediaActorAdd
|
||||
|
||||
if err := c.Bind(&input); err != nil {
|
||||
c.JSON(http.StatusBadRequest, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
actor, err := handler.Service.Add(c.Request().Context(), input.WebLink)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, map[string]string{"error": "lofi not added"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, actor)
|
||||
}
|
||||
|
||||
func (handler *MediaActorHandler) 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 *MediaActorHandler) ListPage(c *echo.Context) {
|
||||
cookie, err := c.Cookie(globals.UserKey)
|
||||
user := cookie.Value
|
||||
actors, err := handler.Service.List(c.Request().Context())
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
data := globals.GetDefaultValues(user, "MediaActors...")
|
||||
data["actors"] = actors
|
||||
c.Render(http.StatusOK, "media_actors", data)
|
||||
}
|
||||
|
||||
func (handler *MediaActorHandler) EditPage(c *echo.Context) {
|
||||
cookie, err := c.Cookie(globals.UserKey)
|
||||
user := cookie.Value
|
||||
id := c.Param("id")
|
||||
//logger := slog.Get(c)
|
||||
actor, err := handler.Service.Get(c.Request().Context(), id)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
data := globals.GetDefaultValues(user, "MediaActors...")
|
||||
data["actor"] = actor
|
||||
c.Render(http.StatusOK, "actor_detail", data)
|
||||
}
|
||||
|
||||
func (handler *MediaActorHandler) UpdatePage(c *echo.Context) {
|
||||
cookie, err := c.Cookie(globals.UserKey)
|
||||
user := cookie.Value
|
||||
id := c.Param("id")
|
||||
logger := c.Logger()
|
||||
actor, err := handler.Service.Get(c.Request().Context(), id)
|
||||
if err != nil {
|
||||
logger.Info("MediaActorHandler#UpdatePage", "error", err.Error())
|
||||
c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
var form models.MediaActorForm
|
||||
if err := c.Bind(&form); err != nil {
|
||||
logger.Info("MediaActorHandler#UpdatePage", "error", err.Error())
|
||||
data := globals.GetDefaultValues(user, "MediaActors...")
|
||||
data["actor"] = actor
|
||||
data["Form"] = form
|
||||
data["Errors"] = err.Error()
|
||||
c.Render(http.StatusBadRequest, "actor_detail", data)
|
||||
return
|
||||
}
|
||||
logger.Info("MediaActorHandler#UpdatePage", "actor", actor)
|
||||
//handler.Service.Update(c.Request.Context(), form.ConvertFormToMediaLofiDTO())
|
||||
c.Redirect(http.StatusFound, globals.MediaActorLink)
|
||||
}
|
||||
|
||||
func (handler *MediaActorHandler) DeletePage(c *echo.Context) {
|
||||
//session := sessions.Default(c)
|
||||
//user := session.Get(globals.UserKey)
|
||||
id := c.Param("id")
|
||||
logger := c.Logger()
|
||||
//actor, err := handler.Service.Get(c.Request.Context(), id)
|
||||
// if err != nil {
|
||||
// logger.Info("MediaActorHandler#DeletePage", "error", err.Error())
|
||||
// c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
// return
|
||||
// }
|
||||
logger.Info("MediaActorHandler#DeletePage", "actor", id)
|
||||
c.Redirect(http.StatusFound, globals.MediaActorLink)
|
||||
}
|
||||
Reference in New Issue
Block a user