mirror of
https://github.com/serty2005/rmser.git
synced 2026-02-05 03:12:34 -06:00
69 lines
1.6 KiB
Go
69 lines
1.6 KiB
Go
package handlers
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/google/uuid"
|
|
|
|
photosSvc "rmser/internal/services/photos"
|
|
)
|
|
|
|
type PhotosHandler struct {
|
|
service *photosSvc.Service
|
|
}
|
|
|
|
func NewPhotosHandler(service *photosSvc.Service) *PhotosHandler {
|
|
return &PhotosHandler{service: service}
|
|
}
|
|
|
|
// GetPhotos GET /api/photos
|
|
func (h *PhotosHandler) GetPhotos(c *gin.Context) {
|
|
userID := c.MustGet("userID").(uuid.UUID)
|
|
|
|
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
|
limit, _ := strconv.Atoi(c.DefaultQuery("limit", "20"))
|
|
|
|
photos, total, err := h.service.GetPhotosForServer(userID, page, limit)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"photos": photos,
|
|
"total": total,
|
|
"page": page,
|
|
"limit": limit,
|
|
})
|
|
}
|
|
|
|
// DeletePhoto DELETE /api/photos/:id
|
|
func (h *PhotosHandler) DeletePhoto(c *gin.Context) {
|
|
userID := c.MustGet("userID").(uuid.UUID)
|
|
photoID, err := uuid.Parse(c.Param("id"))
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid id"})
|
|
return
|
|
}
|
|
|
|
forceDeleteDraft := c.Query("force") == "true"
|
|
|
|
err = h.service.DeletePhoto(userID, photoID, forceDeleteDraft)
|
|
if err != nil {
|
|
if err == photosSvc.ErrPhotoHasDraft {
|
|
// Специальный статус, чтобы фронт показал Confirm
|
|
c.JSON(http.StatusConflict, gin.H{
|
|
"error": err.Error(),
|
|
"requires_confirm": true,
|
|
})
|
|
return
|
|
}
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{"status": "deleted"})
|
|
}
|