225 lines
6.3 KiB
Go
225 lines
6.3 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)
|
|
logger := slog.Get(c)
|
|
var (
|
|
review bool
|
|
download bool
|
|
)
|
|
paramReview, existsReview := c.GetQuery("review")
|
|
if existsReview {
|
|
if paramReview == "on" {
|
|
review = true
|
|
} else {
|
|
review = false
|
|
}
|
|
}
|
|
paramDownload, existsDownload := c.GetQuery("should_download")
|
|
if existsDownload {
|
|
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, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
logger.Info("MediaLoFiHandler#ListPage", "lofis count", len(lofis))
|
|
data := globals.GetDefaultValues(user, "MediaFiles...")
|
|
data["lofis"] = lofis
|
|
data["isReview"] = review
|
|
data["isDownload"] = download
|
|
c.HTML(http.StatusOK, "lofi", data)
|
|
}
|
|
|
|
func (handler *MediaLoFiHandler) EditPage(c *gin.Context) {
|
|
session := sessions.Default(c)
|
|
user := session.Get(globals.UserKey)
|
|
id := c.Param("id")
|
|
logger := slog.Get(c)
|
|
lofi, err := handler.Service.Get(c.Request.Context(), id)
|
|
if err != nil {
|
|
logger.Info("MediaLoFiHandler#EditPage", "error", err.Error())
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
logger.Info("MediaLoFiHandler#EditPage", "lofi", lofi)
|
|
c.HTML(http.StatusOK, "lofi_detail", gin.H{
|
|
"title": "Kontor MediaFLofi",
|
|
"version": "0.4.0",
|
|
"message": "MediaFiles...",
|
|
"lofi": lofi,
|
|
"user": user,
|
|
})
|
|
}
|
|
|
|
func (handler *MediaLoFiHandler) ChangeReviewPage(c *gin.Context) {
|
|
id := c.Param("id")
|
|
logger := slog.Get(c)
|
|
lofi, err := handler.Service.Get(c.Request.Context(), id)
|
|
if err != nil {
|
|
logger.Info("MediaLoFiHandler#ChangeReviewPage", "error", err.Error())
|
|
c.Redirect(http.StatusNotFound, "/web/media-lofis")
|
|
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, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
logger.Info("MediaLoFiHandler#ListPage", "lofis count", len(lofis))
|
|
c.HTML(http.StatusOK, "lofi", gin.H{
|
|
"title": "Kontor MediaFiles",
|
|
"version": "0.4.0",
|
|
"message": "MediaFiles...",
|
|
"lofis": lofis,
|
|
"user": user,
|
|
})
|
|
c.Redirect(http.StatusCreated, "/web/media-lofis")
|
|
}
|
|
|
|
func (handler *MediaLoFiHandler) UpdatePage(c *gin.Context) {
|
|
session := sessions.Default(c)
|
|
user := session.Get(globals.UserKey)
|
|
id := c.Param("id")
|
|
logger := slog.Get(c)
|
|
lofi, err := handler.Service.Get(c.Request.Context(), id)
|
|
if err != nil {
|
|
logger.Info("MediaLoFiHandler#UpdatePage", "error", err.Error())
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
var form models.MediaLoFiForm
|
|
if err := c.ShouldBind(&form); err != nil {
|
|
logger.Info("MediaLoFiHandler#UpdatePage", "error", err.Error())
|
|
c.HTML(http.StatusBadRequest, "lofi_detail", gin.H{
|
|
"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, "web/media-lofis")
|
|
}
|
|
|
|
func (handler *MediaLoFiHandler) DeletePage(c *gin.Context) {
|
|
session := sessions.Default(c)
|
|
user := session.Get(globals.UserKey)
|
|
id := c.Param("id")
|
|
logger := slog.Get(c)
|
|
lofi, err := handler.Service.Get(c.Request.Context(), id)
|
|
if err != nil {
|
|
logger.Info("MediaLoFiHandler#DeletePage", "error", err.Error())
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
logger.Info("MediaLoFiHandler#DeletePage", "lofi", id)
|
|
c.HTML(http.StatusOK, "lofi_delete", gin.H{
|
|
"title": "Kontor MediaFiles",
|
|
"version": "0.4.0",
|
|
"message": "MediaFiles...",
|
|
"lofi": lofi,
|
|
"user": user,
|
|
})
|
|
}
|