newsbot-api/routes/subscriptions.go

298 lines
7.7 KiB
Go
Raw Normal View History

package routes
import (
"context"
"encoding/json"
"net/http"
"github.com/go-chi/chi/v5"
"github.com/google/uuid"
"github.com/jtom38/newsbot/collector/database"
2022-12-11 09:48:30 -08:00
"github.com/jtom38/newsbot/collector/domain/models"
)
func (s *Server) GetSubscriptionsRouter() http.Handler {
r := chi.NewRouter()
r.Get("/", s.ListSubscriptions)
2023-01-21 12:20:33 -08:00
r.Get("/details", s.ListSubscriptionDetails)
r.Get("/by/discordId", s.GetSubscriptionsByDiscordId)
r.Get("/by/sourceId", s.GetSubscriptionsBySourceId)
r.Post("/discord/webhook/new", s.newDiscordWebHookSubscription)
r.Delete("/discord/webhook/delete", s.DeleteDiscordWebHookSubscription)
return r
}
2023-01-21 18:49:16 -08:00
type ListSubscriptions struct {
2022-12-11 09:48:30 -08:00
ApiStatusModel
2023-01-21 12:20:33 -08:00
Payload []models.SubscriptionDto `json:"payload"`
}
2023-01-21 18:49:16 -08:00
type GetSubscription struct {
ApiStatusModel
Payload models.SubscriptionDto `json:"payload"`
}
// GetSubscriptions
// @Summary Returns the top 100 entries from the queue to be processed.
// @Produce application/json
// @Tags Subscription
// @Router /subscriptions [get]
2023-01-21 18:49:16 -08:00
// @Success 200 {object} ListSubscriptions "ok"
// @Failure 400 {object} ApiError "Unable to reach SQL."
// @Failure 500 {object} ApiError "Failed to process data from SQL."
func (s *Server) ListSubscriptions(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
2023-01-21 18:49:16 -08:00
payload := ListSubscriptions{
2022-12-11 09:48:30 -08:00
ApiStatusModel: ApiStatusModel{
StatusCode: http.StatusOK,
Message: "OK",
},
}
2023-01-21 18:49:16 -08:00
res, err := s.dto.ListSubscriptions(r.Context(), 50)
if err != nil {
2022-12-11 09:48:30 -08:00
s.WriteError(w, err.Error(), http.StatusBadRequest)
return
}
2023-01-21 18:49:16 -08:00
payload.Payload = res
bres, err := json.Marshal(payload)
if err != nil {
2022-12-11 09:48:30 -08:00
s.WriteError(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write(bres)
}
2023-01-21 12:20:33 -08:00
type ListSubscriptionDetails struct {
ApiStatusModel
2023-01-21 18:49:16 -08:00
Payload []models.SubscriptionDetailsDto `json:"payload"`
2023-01-21 12:20:33 -08:00
}
// ListSubscriptionDetails
// @Summary Returns the top 50 entries with full deatils on the source and output.
// @Produce application/json
// @Tags Subscription
// @Router /subscriptions/details [get]
2023-01-21 18:49:16 -08:00
// @Success 200 {object} ListSubscriptionDetails "ok"
func (s Server) ListSubscriptionDetails(w http.ResponseWriter, r *http.Request) {
2023-01-21 12:20:33 -08:00
w.Header().Set(HeaderContentType, ApplicationJson)
payload := ListSubscriptionDetails{
ApiStatusModel: ApiStatusModel{
StatusCode: http.StatusOK,
Message: "OK",
},
}
2023-01-21 18:49:16 -08:00
res, err := s.dto.ListSubscriptionDetails(r.Context(), 50)
2023-01-21 12:20:33 -08:00
if err != nil {
s.WriteError(w, err.Error(), http.StatusInternalServerError)
return
}
2023-01-21 18:49:16 -08:00
payload.Payload = res
2023-01-21 12:20:33 -08:00
b, err := json.Marshal(payload)
if err != nil {
s.WriteError(w, err.Error(), http.StatusInternalServerError)
}
w.Write(b)
}
// GetSubscriptionsByDiscordId
// @Summary Returns the top 100 entries from the queue to be processed.
// @Produce application/json
// @Param id query string true "id"
// @Tags Subscription
// @Router /subscriptions/by/discordId [get]
2023-01-21 18:49:16 -08:00
// @Success 200 {object} ListSubscriptions "ok"
// @Failure 400 {object} ApiError "Unable to reach SQL or Data problems"
// @Failure 500 {object} ApiError "Data problems"
func (s *Server) GetSubscriptionsByDiscordId(w http.ResponseWriter, r *http.Request) {
2023-01-21 18:49:16 -08:00
w.Header().Set(HeaderContentType, ApplicationJson)
2023-01-21 18:49:16 -08:00
p := ListSubscriptions{
2022-12-11 09:48:30 -08:00
ApiStatusModel: ApiStatusModel{
StatusCode: http.StatusOK,
2023-01-21 12:20:33 -08:00
Message: "OK",
2022-12-11 09:48:30 -08:00
},
}
query := r.URL.Query()
_id := query["id"][0]
if _id == "" {
2022-12-11 09:48:30 -08:00
s.WriteError(w, ErrIdValueMissing, http.StatusBadRequest)
return
}
uuid, err := uuid.Parse(_id)
if err != nil {
2022-12-11 09:48:30 -08:00
s.WriteError(w, ErrValueNotUuid, http.StatusBadRequest)
return
}
2023-01-21 18:49:16 -08:00
res, err := s.dto.ListSubscriptionsByDiscordWebhookId(r.Context(), uuid)
if err != nil {
2022-12-11 09:48:30 -08:00
s.WriteError(w, err.Error(), http.StatusNoContent)
return
}
2023-01-21 18:49:16 -08:00
p.Payload = res
bres, err := json.Marshal(p)
if err != nil {
2022-12-11 09:48:30 -08:00
s.WriteError(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write(bres)
}
// GetSubscriptionsBySourceId
// @Summary Returns the top 100 entries from the queue to be processed.
// @Produce application/json
// @Param id query string true "id"
// @Tags Subscription
// @Router /subscriptions/by/SourceId [get]
2023-01-21 18:49:16 -08:00
// @Success 200 {object} ListSubscriptions "ok"
func (s *Server) GetSubscriptionsBySourceId(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
2023-01-21 18:49:16 -08:00
p := ListSubscriptions{
2022-12-11 09:48:30 -08:00
ApiStatusModel: ApiStatusModel{
StatusCode: http.StatusOK,
2023-01-21 12:20:33 -08:00
Message: "OK",
2022-12-11 09:48:30 -08:00
},
}
query := r.URL.Query()
_id := query["id"][0]
if _id == "" {
2022-12-11 09:48:30 -08:00
s.WriteError(w, ErrIdValueMissing, http.StatusBadRequest)
return
}
uuid, err := uuid.Parse(_id)
if err != nil {
2022-12-11 09:48:30 -08:00
s.WriteError(w, err.Error(), http.StatusBadRequest)
return
}
2023-01-21 18:49:16 -08:00
res, err := s.dto.ListSubscriptionsBySourceId(r.Context(), uuid)
if err != nil {
2022-12-11 09:48:30 -08:00
s.WriteError(w, err.Error(), http.StatusNoContent)
return
}
2023-01-21 18:49:16 -08:00
p.Payload = res
bres, err := json.Marshal(p)
if err != nil {
2022-12-11 09:48:30 -08:00
s.WriteError(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write(bres)
}
// NewDiscordWebHookSubscription
// @Summary Creates a new subscription to link a post from a Source to a DiscordWebHook.
// @Param discordWebHookId query string true "discordWebHookId"
// @Param sourceId query string true "sourceId"
// @Tags Subscription
2023-01-21 12:20:33 -08:00
// @Router /subscriptions/discord/webhook/new [post]
func (s *Server) newDiscordWebHookSubscription(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
// Extract the values given
query := r.URL.Query()
discordWebHookId := query["discordWebHookId"][0]
sourceId := query["sourceId"][0]
// Check to make we didn't get a null
if discordWebHookId == "" {
2022-12-11 09:48:30 -08:00
s.WriteError(w, "invalid discordWebHooksId given", http.StatusBadRequest)
return
}
if sourceId == "" {
2022-12-11 09:48:30 -08:00
s.WriteError(w, "invalid sourceID given", http.StatusBadRequest)
return
}
// Validate they are UUID values
uHook, err := uuid.Parse(discordWebHookId)
if err != nil {
2022-12-11 09:48:30 -08:00
s.WriteError(w, err.Error(), http.StatusBadRequest)
return
}
uSource, err := uuid.Parse(sourceId)
if err != nil {
2022-12-11 09:48:30 -08:00
s.WriteError(w, err.Error(), http.StatusBadRequest)
return
}
// Check if the sub already exists
_, err = s.Db.QuerySubscriptions(*s.ctx, database.QuerySubscriptionsParams{
Discordwebhookid: uHook,
Sourceid: uSource,
})
if err == nil {
2022-12-11 09:48:30 -08:00
s.WriteError(w, "a subscription already exists between these two entities", http.StatusBadRequest)
return
}
// Does not exist, so make it.
params := database.CreateSubscriptionParams{
ID: uuid.New(),
Discordwebhookid: uHook,
Sourceid: uSource,
}
err = s.Db.CreateSubscription(*s.ctx, params)
if err != nil {
2022-12-11 09:48:30 -08:00
s.WriteError(w, err.Error(), http.StatusInternalServerError)
return
}
bJson, err := json.Marshal(&params)
if err != nil {
2022-12-11 09:48:30 -08:00
s.WriteError(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write(bJson)
}
// DeleteDiscordWebHookSubscription
// @Summary Removes a Discord WebHook Subscription based on the Subscription ID.
2023-01-21 12:20:33 -08:00
// @Param id query string true "id"
// @Tags Subscription
// @Router /subscriptions/discord/webhook/delete [delete]
func (s *Server) DeleteDiscordWebHookSubscription(w http.ResponseWriter, r *http.Request) {
var ErrMissingSubscriptionID string = "the request was missing a 'Id'"
query := r.URL.Query()
2023-01-21 12:20:33 -08:00
id := query["id"][0]
if id == "" {
2022-12-11 09:48:30 -08:00
s.WriteError(w, ErrMissingSubscriptionID, http.StatusBadRequest)
return
}
2023-01-21 12:20:33 -08:00
uid, err := uuid.Parse(query["id"][0])
if err != nil {
2022-12-11 09:48:30 -08:00
s.WriteError(w, err.Error(), http.StatusBadRequest)
return
}
err = s.Db.DeleteSubscription(context.Background(), uid)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
}