mirror of
https://github.com/truewhile/MeBox.git
synced 2026-09-28 03:06:38 +08:00
103 lines
3.0 KiB
Go
103 lines
3.0 KiB
Go
// Package handler — RSS subscription endpoints.
|
|
package handler
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/ShukeBta/MediaStationGo/internal/middleware"
|
|
"github.com/ShukeBta/MediaStationGo/internal/model"
|
|
"github.com/ShukeBta/MediaStationGo/internal/service"
|
|
)
|
|
|
|
type subscriptionReq struct {
|
|
Name string `json:"name" binding:"required"`
|
|
FeedURL string `json:"feed_url" binding:"required"`
|
|
Filter string `json:"filter"`
|
|
MediaType string `json:"media_type"`
|
|
MediaCategory string `json:"media_category"`
|
|
SavePath string `json:"save_path"`
|
|
SearchMode string `json:"search_mode"`
|
|
IMDBID string `json:"imdb_id"`
|
|
Resolution string `json:"resolution"`
|
|
Quality string `json:"quality"`
|
|
Effects string `json:"effects"`
|
|
ReleaseGroups string `json:"release_groups"`
|
|
ExcludeWords string `json:"exclude_words"`
|
|
WashPriority string `json:"wash_priority"`
|
|
Priority int `json:"priority"`
|
|
Enabled *bool `json:"enabled"`
|
|
}
|
|
|
|
func createSubscriptionHandler(svc *service.Container) gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
var req subscriptionReq
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
uid, _ := c.Get(middleware.CtxUserID)
|
|
enabled := true
|
|
if req.Enabled != nil {
|
|
enabled = *req.Enabled
|
|
}
|
|
s := &model.Subscription{
|
|
UserID: uid.(string),
|
|
Name: req.Name,
|
|
FeedURL: req.FeedURL,
|
|
Filter: req.Filter,
|
|
MediaType: req.MediaType,
|
|
MediaCategory: req.MediaCategory,
|
|
SavePath: req.SavePath,
|
|
SearchMode: req.SearchMode,
|
|
IMDBID: req.IMDBID,
|
|
Resolution: req.Resolution,
|
|
Quality: req.Quality,
|
|
Effects: req.Effects,
|
|
ReleaseGroups: req.ReleaseGroups,
|
|
ExcludeWords: req.ExcludeWords,
|
|
WashPriority: req.WashPriority,
|
|
Priority: req.Priority,
|
|
Enabled: enabled,
|
|
}
|
|
if err := svc.Subscription.Create(c.Request.Context(), s); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, s)
|
|
}
|
|
}
|
|
|
|
func listSubscriptionsHandler(svc *service.Container) gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
items, err := svc.Subscription.List(c.Request.Context())
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"items": items})
|
|
}
|
|
}
|
|
|
|
func deleteSubscriptionHandler(svc *service.Container) gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
if err := svc.Subscription.Delete(c.Request.Context(), c.Param("id")); err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.Status(http.StatusNoContent)
|
|
}
|
|
}
|
|
|
|
func runSubscriptionHandler(svc *service.Container) gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
n, err := svc.Subscription.RunNow(c.Request.Context(), c.Param("id"))
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"queued": n})
|
|
}
|
|
}
|