package handler import ( "encoding/json" "net/http" "strconv" "github.com/gin-gonic/gin" "github.com/rovina/zsp-backend/internal/service" ) type CompanyHandler struct { service service.CompanyService } func NewCompanyHandler(s service.CompanyService) *CompanyHandler { return &CompanyHandler{service: s} } func (h *CompanyHandler) ListFolders(c *gin.Context) { list, err := h.service.ListFolders() if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"success": false, "message": err.Error()}) return } c.JSON(http.StatusOK, gin.H{"success": true, "data": list}) } func (h *CompanyHandler) CreateFolder(c *gin.Context) { var body struct { Name string `json:"name" binding:"required"` Description string `json:"description"` Category string `json:"category"` } if err := c.ShouldBindJSON(&body); err != nil { c.JSON(http.StatusBadRequest, gin.H{"success": false, "message": err.Error()}) return } f, err := h.service.CreateFolder(body.Name, body.Description, body.Category) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"success": false, "message": err.Error()}) return } c.JSON(http.StatusOK, gin.H{"success": true, "data": f}) } func (h *CompanyHandler) UpdateFolder(c *gin.Context) { id, err := strconv.ParseUint(c.Param("id"), 10, 32) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"success": false, "message": "invalid id"}) return } var body struct { Name string `json:"name" binding:"required"` Description string `json:"description"` Category string `json:"category"` } if err := c.ShouldBindJSON(&body); err != nil { c.JSON(http.StatusBadRequest, gin.H{"success": false, "message": err.Error()}) return } if err := h.service.UpdateFolder(uint(id), body.Name, body.Description, body.Category); err != nil { c.JSON(http.StatusInternalServerError, gin.H{"success": false, "message": err.Error()}) return } c.JSON(http.StatusOK, gin.H{"success": true, "message": "updated"}) } func (h *CompanyHandler) DeleteFolder(c *gin.Context) { id, err := strconv.ParseUint(c.Param("id"), 10, 32) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"success": false, "message": "invalid id"}) return } if err := h.service.DeleteFolder(uint(id)); err != nil { c.JSON(http.StatusInternalServerError, gin.H{"success": false, "message": err.Error()}) return } c.JSON(http.StatusOK, gin.H{"success": true, "message": "deleted"}) } func (h *CompanyHandler) ListFiles(c *gin.Context) { var folderID *uint if idStr := c.Query("folderId"); idStr != "" { if id, err := strconv.ParseUint(idStr, 10, 32); err == nil { uid := uint(id) folderID = &uid } } list, err := h.service.ListFiles(folderID) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"success": false, "message": err.Error()}) return } c.JSON(http.StatusOK, gin.H{"success": true, "data": list}) } func (h *CompanyHandler) UploadFiles(c *gin.Context) { form, err := c.MultipartForm() if err != nil { c.JSON(http.StatusBadRequest, gin.H{"success": false, "message": err.Error()}) return } files := form.File["files"] if len(files) == 0 { c.JSON(http.StatusBadRequest, gin.H{"success": false, "message": "no files"}) return } var dataStr string if v := form.Value["data"]; len(v) > 0 { dataStr = v[0] } var data struct { CompanyName string `json:"companyName"` FileCategory string `json:"fileCategory"` ExpireDate string `json:"expireDate"` Description string `json:"description"` FolderID string `json:"folderId"` } if dataStr != "" { _ = json.Unmarshal([]byte(dataStr), &data) } var folderID *uint if data.FolderID != "" { if id, err := strconv.ParseUint(data.FolderID, 10, 32); err == nil { uid := uint(id) folderID = &uid } } count, err := h.service.UploadFiles(folderID, data.CompanyName, data.FileCategory, data.ExpireDate, data.Description, files) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"success": false, "message": err.Error()}) return } c.JSON(http.StatusOK, gin.H{"success": true, "data": gin.H{"uploadedCount": count}, "message": "uploaded"}) } func (h *CompanyHandler) UpdateFile(c *gin.Context) { id, err := strconv.ParseUint(c.Param("id"), 10, 32) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"success": false, "message": "invalid id"}) return } var body struct { CompanyName string `json:"companyName"` FileCategory string `json:"fileCategory"` ExpireDate string `json:"expireDate"` Description string `json:"description"` FolderID *uint `json:"folderId"` } if err := c.ShouldBindJSON(&body); err != nil { c.JSON(http.StatusBadRequest, gin.H{"success": false, "message": err.Error()}) return } if err := h.service.UpdateFile(uint(id), body.CompanyName, body.FileCategory, body.ExpireDate, body.Description, body.FolderID); err != nil { c.JSON(http.StatusInternalServerError, gin.H{"success": false, "message": err.Error()}) return } c.JSON(http.StatusOK, gin.H{"success": true, "message": "updated"}) } func (h *CompanyHandler) DeleteFile(c *gin.Context) { id, err := strconv.ParseUint(c.Param("id"), 10, 32) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"success": false, "message": "invalid id"}) return } if err := h.service.DeleteFile(uint(id)); err != nil { c.JSON(http.StatusInternalServerError, gin.H{"success": false, "message": err.Error()}) return } c.JSON(http.StatusOK, gin.H{"success": true, "message": "deleted"}) }