update kontor-echo
This commit is contained in:
@@ -0,0 +1,233 @@
|
||||
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"
|
||||
"time"
|
||||
|
||||
"github.com/gin-contrib/sessions"
|
||||
"github.com/google/uuid"
|
||||
"github.com/labstack/echo/v5"
|
||||
"github.com/uptrace/bun"
|
||||
)
|
||||
|
||||
type MediaLoFiHandler struct {
|
||||
Service *service.MediaLoFiService
|
||||
}
|
||||
|
||||
func NewMediaLoFiHandler(db *bun.DB) *MediaLoFiHandler {
|
||||
mediaLoFiHandler := &MediaLoFiHandler{
|
||||
Service: service.NewMediaLoFiService(repository.NewMediaLoFiRepository(db)),
|
||||
}
|
||||
return mediaLoFiHandler
|
||||
}
|
||||
|
||||
func (handler *MediaLoFiHandler) 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.ConvertMediaLoFisToDtos(lofis)
|
||||
c.JSON(http.StatusOK, dtos)
|
||||
}
|
||||
|
||||
func (handler *MediaLoFiHandler) Get(c *echo.Context) {
|
||||
id := c.Param("id")
|
||||
lofi, err := handler.Service.Get(c.Request().Context(), id)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, map[string]string{"error": "Lofi not found"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, lofi)
|
||||
}
|
||||
|
||||
func (handler *MediaLoFiHandler) Add(c *echo.Context) {
|
||||
var input models.MediaLofiAdd
|
||||
|
||||
if err := c.Bind(&input); err != nil {
|
||||
c.JSON(http.StatusBadRequest, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
lofi, err := handler.Service.Add(c.Request().Context(), input.Url)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, map[string]string{"error": "lofi not added"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, lofi)
|
||||
}
|
||||
|
||||
func (handler *MediaLoFiHandler) Update(c *echo.Context) {
|
||||
var input models.MediaLoFiDTO
|
||||
|
||||
if err := c.Bind(&input); err != nil {
|
||||
c.JSON(http.StatusBadRequest, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
lofi, err := handler.Service.Update(c.Request().Context(), input)
|
||||
if err != nil {
|
||||
logger := c.Logger()
|
||||
logger.Info("MediaLoFiHandler#Update", "error", err.Error())
|
||||
c.JSON(http.StatusNotFound, map[string]string{"error": "lofi not updated"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, lofi)
|
||||
}
|
||||
func (handler *MediaLoFiHandler) 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": "lofi not found"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, map[string]string{"ok": "true"})
|
||||
}
|
||||
|
||||
func (handler *MediaLoFiHandler) ListPage(c *echo.Context) {
|
||||
cookie, err := c.Cookie(globals.UserKey)
|
||||
user := cookie.Value
|
||||
logger := c.Logger()
|
||||
var (
|
||||
review bool
|
||||
download bool
|
||||
)
|
||||
paramReview, err := echo.QueryParam[string](c, "review")
|
||||
if err == nil {
|
||||
if paramReview == "on" {
|
||||
review = true
|
||||
} else {
|
||||
review = false
|
||||
}
|
||||
}
|
||||
paramDownload, err := echo.QueryParam[string](c, "should_download")
|
||||
if err == nil {
|
||||
if paramDownload == "on" {
|
||||
download = true
|
||||
} else {
|
||||
download = false
|
||||
}
|
||||
}
|
||||
|
||||
lofis, err := handler.Service.List(c.Request().Context())
|
||||
if err != nil {
|
||||
logger.Info("MediaLoFiHandler#ListPage", "error", err.Error())
|
||||
c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
logger.Info("MediaLoFiHandler#ListPage", "lofis count", len(lofis))
|
||||
data := globals.GetDefaultValues(user, "MediaFiles...")
|
||||
data["media_links"] = lofis
|
||||
data["isReview"] = review
|
||||
data["isDownload"] = download
|
||||
data["MediaLinksUrl"] = globals.MediaLofiLink
|
||||
c.Render(http.StatusOK, "media_links", data)
|
||||
}
|
||||
|
||||
func (handler *MediaLoFiHandler) EditPage(c *echo.Context) {
|
||||
cookie, err := c.Cookie(globals.UserKey)
|
||||
user := cookie.Value
|
||||
id := c.Param("id")
|
||||
logger := c.Logger()
|
||||
var lofi *models.MediaLoFi
|
||||
if id == "" {
|
||||
// id is empty, adding is assumed
|
||||
lofi = &models.MediaLoFi{
|
||||
ID: uuid.NewString(),
|
||||
CreatedAt: time.Now(),
|
||||
UpdatedAt: time.Now(),
|
||||
Version: 0,
|
||||
Review: true,
|
||||
ShouldDownload: true,
|
||||
}
|
||||
} else {
|
||||
// id is set, fetch MediaLofi by ID
|
||||
lofi, err = handler.Service.Get(c.Request().Context(), id)
|
||||
if err != nil {
|
||||
logger.Info("MediaLoFiHandler#EditPage", "error", err.Error())
|
||||
c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
}
|
||||
logger.Info("MediaLoFiHandler#EditPage", "lofi", lofi)
|
||||
data := globals.GetDefaultValues(user, "MediaLofis...")
|
||||
data["media"] = lofi
|
||||
data["MediaLinksUrl"] = globals.MediaLofiLink
|
||||
c.Render(http.StatusOK, "media_detail", data)
|
||||
}
|
||||
|
||||
func (handler *MediaLoFiHandler) ChangeReviewPage(c *echo.Context) {
|
||||
id := c.Param("id")
|
||||
logger := c.Logger()
|
||||
lofi, err := handler.Service.Get(c.Request().Context(), id)
|
||||
if err != nil {
|
||||
logger.Info("MediaLoFiHandler#ChangeReviewPage", "error", err.Error())
|
||||
c.Redirect(http.StatusNotFound, globals.MediaLofiLink)
|
||||
return
|
||||
}
|
||||
lofi.Review = !lofi.Review
|
||||
handler.Service.Update(c.Request().Context(), lofi.ConvertMediaLoFiToDTO())
|
||||
logger.Info("MediaLoFiHandler#ChangeReviewPage", "lofi", lofi)
|
||||
session := sessions.Default(c)
|
||||
user := session.Get(globals.UserKey)
|
||||
lofis, err := handler.Service.List(c.Request().Context())
|
||||
if err != nil {
|
||||
logger.Info("MediaLoFiHandler#ChangeReviewPage", "error", err.Error())
|
||||
c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
logger.Info("MediaLoFiHandler#ListPage", "lofis count", len(lofis))
|
||||
data := globals.GetDefaultValues(user, "MediaLofis...")
|
||||
data["lofis"] = lofis
|
||||
//c.HTML(http.StatusOK, "lofi", data)
|
||||
c.Redirect(http.StatusCreated, globals.MediaLofiLink)
|
||||
}
|
||||
|
||||
func (handler *MediaLoFiHandler) UpdatePage(c *echo.Context) {
|
||||
cookie, err := c.Cookie(globals.UserKey)
|
||||
user := cookie.Value
|
||||
id := c.Param("id")
|
||||
logger := c.Logger()
|
||||
lofi, err := handler.Service.Get(c.Request().Context(), id)
|
||||
if err != nil {
|
||||
logger.Info("MediaLoFiHandler#UpdatePage", "error", err.Error())
|
||||
c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
var form models.MediaLoFiForm
|
||||
if err := c.Bind(&form); err != nil {
|
||||
logger.Info("MediaLoFiHandler#UpdatePage", "error", err.Error())
|
||||
c.Render(http.StatusBadRequest, "lofi_detail", map[string]string{
|
||||
"Form": form,
|
||||
"Errors": err.Error(),
|
||||
"user": user,
|
||||
})
|
||||
return
|
||||
}
|
||||
logger.Info("MediaLoFiHandler#UpdatePage", "lofi", lofi)
|
||||
//handler.Service.Update(c.Request.Context(), form.ConvertFormToMediaLofiDTO())
|
||||
c.Redirect(http.StatusFound, globals.MediaLofiLink)
|
||||
}
|
||||
|
||||
func (handler *MediaLoFiHandler) DeletePage(c *echo.Context) {
|
||||
cookie, err := c.Cookie(globals.UserKey)
|
||||
user := cookie.Value
|
||||
id := c.Param("id")
|
||||
logger := c.Logger()
|
||||
lofi, err := handler.Service.Get(c.Request().Context(), id)
|
||||
if err != nil {
|
||||
logger.Info("MediaLoFiHandler#DeletePage", "error", err.Error())
|
||||
c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
logger.Info("MediaLoFiHandler#DeletePage", "lofi", id)
|
||||
c.Render(http.StatusOK, "lofi_delete", map[string]string{
|
||||
"title": "Kontor MediaFiles",
|
||||
"version": "0.4.0",
|
||||
"message": "MediaFiles...",
|
||||
"lofi": lofi,
|
||||
"user": user,
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user