This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
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-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type VolumeHandler struct {
|
||||
Service *service.VolumeService
|
||||
}
|
||||
|
||||
func NewVolumeHandler(db *gorm.DB) *VolumeHandler {
|
||||
handler := &VolumeHandler{Service: service.NewVolumeService(repository.NewVolumeRepository(db))}
|
||||
return handler
|
||||
}
|
||||
|
||||
func (handler *VolumeHandler) List(c *gin.Context) {
|
||||
volumes, err := handler.Service.List(c.Request.Context())
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
dtos := models.ConvertVolumesToDtos(volumes)
|
||||
c.JSON(http.StatusOK, dtos)
|
||||
}
|
||||
|
||||
func (handler *VolumeHandler) ListPage(c *gin.Context) {
|
||||
session := sessions.Default(c)
|
||||
user := session.Get(globals.UserKey)
|
||||
volumes, err := handler.Service.List(c.Request.Context())
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.HTML(http.StatusOK, "Volumes", gin.H{
|
||||
"title": "Kontor Volumes",
|
||||
"version": "0.4.0",
|
||||
"message": "Volumes...",
|
||||
"volumes": volumes,
|
||||
"user": user,
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user