cleaned up swag and updated models

This commit is contained in:
James Tombleson 2022-12-11 09:48:30 -08:00
parent 3b72a4b329
commit 5637d718cf

View File

@ -8,6 +8,7 @@ import (
"github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/jtom38/newsbot/collector/database" "github.com/jtom38/newsbot/collector/database"
"github.com/jtom38/newsbot/collector/domain/models"
) )
func (s *Server) GetSubscriptionsRouter() http.Handler { func (s *Server) GetSubscriptionsRouter() http.Handler {
@ -23,9 +24,8 @@ func (s *Server) GetSubscriptionsRouter() http.Handler {
} }
type ListSubscriptionResults struct { type ListSubscriptionResults struct {
StatusCode int `json:"status"` ApiStatusModel
Message string `json:"message"` Payload []models.SubscriptionDto `json:"payload"`
Payload []database.SubscriptionDto `json:"payload"`
} }
// GetSubscriptions // GetSubscriptions
@ -34,29 +34,31 @@ type ListSubscriptionResults struct {
// @Tags Subscription // @Tags Subscription
// @Router /subscriptions [get] // @Router /subscriptions [get]
// @Success 200 {object} ListSubscriptionResults "ok" // @Success 200 {object} ListSubscriptionResults "ok"
// @Failure 400 {object} models.ApiError "Unable to reach SQL." // @Failure 400 {object} ApiError "Unable to reach SQL."
// @Failure 500 {object} models.ApiError "Failed to process data from SQL." // @Failure 500 {object} ApiError "Failed to process data from SQL."
func (s *Server) ListSubscriptions(w http.ResponseWriter, r *http.Request) { func (s *Server) ListSubscriptions(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
payload := ListSubscriptionResults{ payload := ListSubscriptionResults{
StatusCode: http.StatusOK, ApiStatusModel: ApiStatusModel{
Message: "OK", StatusCode: http.StatusOK,
Message: "OK",
},
} }
res, err := s.Db.ListSubscriptions(*s.ctx, 100) res, err := s.Db.ListSubscriptions(*s.ctx, 100)
if err != nil { if err != nil {
s.WriteError(w, err.Error(), http.StatusBadRequest, nil) s.WriteError(w, err.Error(), http.StatusBadRequest)
return return
} }
for _, item := range res { for _, item := range res {
payload.Payload = append(payload.Payload, database.ConvertToSubscriptionDto(item)) payload.Payload = append(payload.Payload, models.ConvertToSubscriptionDto(item))
} }
bres, err := json.Marshal(payload) bres, err := json.Marshal(payload)
if err != nil { if err != nil {
s.WriteError(w, err.Error(), http.StatusInternalServerError, nil) s.WriteError(w, err.Error(), http.StatusInternalServerError)
return return
} }
@ -64,9 +66,8 @@ func (s *Server) ListSubscriptions(w http.ResponseWriter, r *http.Request) {
} }
type GetSubscriptionResults struct { type GetSubscriptionResults struct {
StatusCode int `json:"status"` ApiStatusModel
Message string `json:"message"` Payload models.SubscriptionDto `json:"payload"`
Payload database.SubscriptionDto `json:"payload"`
} }
// GetSubscriptionsByDiscordId // GetSubscriptionsByDiscordId
@ -76,42 +77,44 @@ type GetSubscriptionResults struct {
// @Tags Subscription // @Tags Subscription
// @Router /subscriptions/by/discordId [get] // @Router /subscriptions/by/discordId [get]
// @Success 200 {object} ListSubscriptionResults "ok" // @Success 200 {object} ListSubscriptionResults "ok"
// @Failure 400 {object} models.ApiError "Unable to reach SQL or Data problems" // @Failure 400 {object} ApiError "Unable to reach SQL or Data problems"
// @Failure 500 {object} models.ApiError "Data problems" // @Failure 500 {object} ApiError "Data problems"
func (s *Server) GetSubscriptionsByDiscordId(w http.ResponseWriter, r *http.Request) { func (s *Server) GetSubscriptionsByDiscordId(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
p := ListSubscriptionResults { p := ListSubscriptionResults {
StatusCode: http.StatusOK, ApiStatusModel: ApiStatusModel{
Message: "OK", StatusCode: http.StatusOK,
Message: "OK",
},
} }
query := r.URL.Query() query := r.URL.Query()
_id := query["id"][0] _id := query["id"][0]
if _id == "" { if _id == "" {
s.WriteError(w, ErrIdValueMissing, http.StatusBadRequest, nil) s.WriteError(w, ErrIdValueMissing, http.StatusBadRequest)
return return
} }
uuid, err := uuid.Parse(_id) uuid, err := uuid.Parse(_id)
if err != nil { if err != nil {
s.WriteError(w, ErrValueNotUuid, http.StatusBadRequest, nil) s.WriteError(w, ErrValueNotUuid, http.StatusBadRequest)
return return
} }
res, err := s.Db.GetSubscriptionsByDiscordWebHookId(*s.ctx, uuid) res, err := s.Db.GetSubscriptionsByDiscordWebHookId(*s.ctx, uuid)
if err != nil { if err != nil {
s.WriteError(w, err.Error(), http.StatusNoContent, nil) s.WriteError(w, err.Error(), http.StatusNoContent)
return return
} }
for _, item := range res { for _, item := range res {
p.Payload = append(p.Payload, database.ConvertToSubscriptionDto(item)) p.Payload = append(p.Payload, models.ConvertToSubscriptionDto(item))
} }
bres, err := json.Marshal(p) bres, err := json.Marshal(p)
if err != nil { if err != nil {
s.WriteError(w, err.Error(), http.StatusInternalServerError, nil) s.WriteError(w, err.Error(), http.StatusInternalServerError)
return return
} }
@ -129,36 +132,38 @@ func (s *Server) GetSubscriptionsBySourceId(w http.ResponseWriter, r *http.Reque
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
p := ListSubscriptionResults { p := ListSubscriptionResults {
StatusCode: http.StatusOK, ApiStatusModel: ApiStatusModel{
Message: "OK", StatusCode: http.StatusOK,
Message: "OK",
},
} }
query := r.URL.Query() query := r.URL.Query()
_id := query["id"][0] _id := query["id"][0]
if _id == "" { if _id == "" {
s.WriteError(w, ErrIdValueMissing, http.StatusBadRequest, nil) s.WriteError(w, ErrIdValueMissing, http.StatusBadRequest)
return return
} }
uuid, err := uuid.Parse(_id) uuid, err := uuid.Parse(_id)
if err != nil { if err != nil {
s.WriteError(w, err.Error(), http.StatusBadRequest, nil) s.WriteError(w, err.Error(), http.StatusBadRequest)
return return
} }
res, err := s.Db.GetSubscriptionsByDiscordWebHookId(*s.ctx, uuid) res, err := s.Db.GetSubscriptionsByDiscordWebHookId(*s.ctx, uuid)
if err != nil { if err != nil {
s.WriteError(w, err.Error(), http.StatusNoContent, nil) s.WriteError(w, err.Error(), http.StatusNoContent)
return return
} }
for _, item := range res { for _, item := range res {
p.Payload = append(p.Payload, database.ConvertToSubscriptionDto(item)) p.Payload = append(p.Payload, models.ConvertToSubscriptionDto(item))
} }
bres, err := json.Marshal(p) bres, err := json.Marshal(p)
if err != nil { if err != nil {
s.WriteError(w, err.Error(), http.StatusInternalServerError, nil) s.WriteError(w, err.Error(), http.StatusInternalServerError)
return return
} }
@ -181,23 +186,23 @@ func (s *Server) newDiscordWebHookSubscription(w http.ResponseWriter, r *http.Re
// Check to make we didn't get a null // Check to make we didn't get a null
if discordWebHookId == "" { if discordWebHookId == "" {
s.WriteError(w, "invalid discordWebHooksId given", http.StatusBadRequest, nil) s.WriteError(w, "invalid discordWebHooksId given", http.StatusBadRequest)
return return
} }
if sourceId == "" { if sourceId == "" {
s.WriteError(w, "invalid sourceID given", http.StatusBadRequest, nil) s.WriteError(w, "invalid sourceID given", http.StatusBadRequest)
return return
} }
// Validate they are UUID values // Validate they are UUID values
uHook, err := uuid.Parse(discordWebHookId) uHook, err := uuid.Parse(discordWebHookId)
if err != nil { if err != nil {
s.WriteError(w, err.Error(), http.StatusBadRequest, nil) s.WriteError(w, err.Error(), http.StatusBadRequest)
return return
} }
uSource, err := uuid.Parse(sourceId) uSource, err := uuid.Parse(sourceId)
if err != nil { if err != nil {
s.WriteError(w, err.Error(), http.StatusBadRequest, nil) s.WriteError(w, err.Error(), http.StatusBadRequest)
return return
} }
@ -207,7 +212,7 @@ func (s *Server) newDiscordWebHookSubscription(w http.ResponseWriter, r *http.Re
Sourceid: uSource, Sourceid: uSource,
}) })
if err == nil { if err == nil {
s.WriteError(w, "a subscription already exists between these two entities", http.StatusBadRequest, nil) s.WriteError(w, "a subscription already exists between these two entities", http.StatusBadRequest)
return return
} }
@ -219,13 +224,13 @@ func (s *Server) newDiscordWebHookSubscription(w http.ResponseWriter, r *http.Re
} }
err = s.Db.CreateSubscription(*s.ctx, params) err = s.Db.CreateSubscription(*s.ctx, params)
if err != nil { if err != nil {
s.WriteError(w, err.Error(), http.StatusInternalServerError, nil) s.WriteError(w, err.Error(), http.StatusInternalServerError)
return return
} }
bJson, err := json.Marshal(&params) bJson, err := json.Marshal(&params)
if err != nil { if err != nil {
s.WriteError(w, err.Error(), http.StatusInternalServerError, nil) s.WriteError(w, err.Error(), http.StatusInternalServerError)
return return
} }
@ -243,13 +248,13 @@ func (s *Server) DeleteDiscordWebHookSubscription(w http.ResponseWriter, r *http
id := query["Id"][0] id := query["Id"][0]
if id == "" { if id == "" {
s.WriteError(w, ErrMissingSubscriptionID, http.StatusBadRequest, nil) s.WriteError(w, ErrMissingSubscriptionID, http.StatusBadRequest)
return return
} }
uid, err := uuid.Parse(query["Id"][0]) uid, err := uuid.Parse(query["Id"][0])
if err != nil { if err != nil {
s.WriteError(w, err.Error(), http.StatusBadRequest, nil) s.WriteError(w, err.Error(), http.StatusBadRequest)
return return
} }