2025-08-07 13:47:42 +00:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
|
|
|
|
|
|
"neomovies-api/pkg/middleware"
|
|
|
|
|
"neomovies-api/pkg/models"
|
|
|
|
|
"neomovies-api/pkg/services"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type ReactionsHandler struct {
|
|
|
|
|
reactionsService *services.ReactionsService
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewReactionsHandler(reactionsService *services.ReactionsService) *ReactionsHandler {
|
2025-08-08 16:47:02 +00:00
|
|
|
return &ReactionsHandler{reactionsService: reactionsService}
|
2025-08-07 13:47:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *ReactionsHandler) GetReactionCounts(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
|
mediaType := vars["mediaType"]
|
|
|
|
|
mediaID := vars["mediaId"]
|
|
|
|
|
|
|
|
|
|
if mediaType == "" || mediaID == "" {
|
|
|
|
|
http.Error(w, "Media type and ID are required", http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
counts, err := h.reactionsService.GetReactionCounts(mediaType, mediaID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
json.NewEncoder(w).Encode(counts)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *ReactionsHandler) GetMyReaction(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
userID, ok := middleware.GetUserIDFromContext(r.Context())
|
|
|
|
|
if !ok {
|
|
|
|
|
http.Error(w, "User ID not found in context", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
|
mediaType := vars["mediaType"]
|
|
|
|
|
mediaID := vars["mediaId"]
|
|
|
|
|
|
|
|
|
|
if mediaType == "" || mediaID == "" {
|
|
|
|
|
http.Error(w, "Media type and ID are required", http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-08 16:47:02 +00:00
|
|
|
reactionType, err := h.reactionsService.GetMyReaction(userID, mediaType, mediaID)
|
2025-08-07 13:47:42 +00:00
|
|
|
if err != nil {
|
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2025-08-08 16:47:02 +00:00
|
|
|
if reactionType == "" {
|
2025-08-07 13:47:42 +00:00
|
|
|
json.NewEncoder(w).Encode(map[string]interface{}{})
|
|
|
|
|
} else {
|
2025-08-08 16:47:02 +00:00
|
|
|
json.NewEncoder(w).Encode(map[string]string{"type": reactionType})
|
2025-08-07 13:47:42 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *ReactionsHandler) SetReaction(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
userID, ok := middleware.GetUserIDFromContext(r.Context())
|
|
|
|
|
if !ok {
|
|
|
|
|
http.Error(w, "User ID not found in context", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
|
mediaType := vars["mediaType"]
|
|
|
|
|
mediaID := vars["mediaId"]
|
|
|
|
|
|
|
|
|
|
if mediaType == "" || mediaID == "" {
|
|
|
|
|
http.Error(w, "Media type and ID are required", http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-28 11:46:20 +00:00
|
|
|
var request struct {
|
|
|
|
|
Type string `json:"type"`
|
|
|
|
|
}
|
2025-08-07 13:47:42 +00:00
|
|
|
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
|
|
|
|
|
http.Error(w, "Invalid request body", http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if request.Type == "" {
|
|
|
|
|
http.Error(w, "Reaction type is required", http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-08 16:47:02 +00:00
|
|
|
if err := h.reactionsService.SetReaction(userID, mediaType, mediaID, request.Type); err != nil {
|
2025-08-07 13:47:42 +00:00
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2025-08-08 16:47:02 +00:00
|
|
|
json.NewEncoder(w).Encode(models.APIResponse{Success: true, Message: "Reaction set successfully"})
|
2025-08-07 13:47:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *ReactionsHandler) RemoveReaction(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
userID, ok := middleware.GetUserIDFromContext(r.Context())
|
|
|
|
|
if !ok {
|
|
|
|
|
http.Error(w, "User ID not found in context", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
|
mediaType := vars["mediaType"]
|
|
|
|
|
mediaID := vars["mediaId"]
|
|
|
|
|
|
|
|
|
|
if mediaType == "" || mediaID == "" {
|
|
|
|
|
http.Error(w, "Media type and ID are required", http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-08 16:47:02 +00:00
|
|
|
if err := h.reactionsService.RemoveReaction(userID, mediaType, mediaID); err != nil {
|
2025-08-07 13:47:42 +00:00
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2025-08-08 16:47:02 +00:00
|
|
|
json.NewEncoder(w).Encode(models.APIResponse{Success: true, Message: "Reaction removed successfully"})
|
2025-08-07 13:47:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *ReactionsHandler) GetMyReactions(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
userID, ok := middleware.GetUserIDFromContext(r.Context())
|
|
|
|
|
if !ok {
|
|
|
|
|
http.Error(w, "User ID not found in context", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
limit := getIntQuery(r, "limit", 50)
|
|
|
|
|
|
|
|
|
|
reactions, err := h.reactionsService.GetUserReactions(userID, limit)
|
|
|
|
|
if err != nil {
|
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2025-08-08 16:47:02 +00:00
|
|
|
json.NewEncoder(w).Encode(models.APIResponse{Success: true, Data: reactions})
|
2025-09-28 11:46:20 +00:00
|
|
|
}
|