diff --git a/docs/docs.go b/docs/docs.go index ddfecb0..331c299 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -104,6 +104,28 @@ const docTemplate = `{ "responses": {} } }, + "/config/sources/by/source": { + "get": { + "produces": [ + "application/json" + ], + "tags": [ + "Config", + "Source" + ], + "summary": "Lists the top 50 records based on the name given. Example: reddit", + "parameters": [ + { + "type": "string", + "description": "Source Name", + "name": "source", + "in": "query", + "required": true + } + ], + "responses": {} + } + }, "/config/sources/new/reddit": { "post": { "tags": [ diff --git a/docs/swagger.json b/docs/swagger.json index 4362b33..6e515b3 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -95,6 +95,28 @@ "responses": {} } }, + "/config/sources/by/source": { + "get": { + "produces": [ + "application/json" + ], + "tags": [ + "Config", + "Source" + ], + "summary": "Lists the top 50 records based on the name given. Example: reddit", + "parameters": [ + { + "type": "string", + "description": "Source Name", + "name": "source", + "in": "query", + "required": true + } + ], + "responses": {} + } + }, "/config/sources/new/reddit": { "post": { "tags": [ diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 6ab85ac..6eac5c4 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -118,6 +118,21 @@ paths: tags: - Config - Source + /config/sources/by/source: + get: + parameters: + - description: Source Name + in: query + name: source + required: true + type: string + produces: + - application/json + responses: {} + summary: 'Lists the top 50 records based on the name given. Example: reddit' + tags: + - Config + - Source /config/sources/new/reddit: post: parameters: diff --git a/routes/server.go b/routes/server.go index f90b46b..5e9198f 100644 --- a/routes/server.go +++ b/routes/server.go @@ -93,6 +93,7 @@ func (s *Server) MountRoutes() { /* Source Routes */ s.Router.Get("/api/config/sources", s.listSources) + s.Router.Get("/api/config/sources/by/source", s.listSourcesBySource) /* Reddit Source Routes */ diff --git a/routes/sources.go b/routes/sources.go index d1ea1c2..6949db9 100644 --- a/routes/sources.go +++ b/routes/sources.go @@ -28,12 +28,49 @@ func (s *Server) listSources(w http.ResponseWriter, r *http.Request) { res, err := s.Db.ListSources(*s.ctx, int32(topInt)) */ + // Default way of showing all sources res, err := s.Db.ListSources(*s.ctx, 50) if err != nil { http.Error(w, "url is missing a value", http.StatusBadRequest) return } + bResult, err := json.Marshal(res) + if err != nil { + http.Error(w, "unable to convert to json", http.StatusBadRequest) + return + } + w.Header().Set("Content-Type", "application/json") + w.Write(bResult) + +} + +// ListSourcesBySource +// @Summary Lists the top 50 records based on the name given. Example: reddit +// @Param source query string true "Source Name" +// @Produce application/json +// @Tags Config, Source +// @Router /config/sources/by/source [get] +func (s *Server) listSourcesBySource(w http.ResponseWriter, r *http.Request) { + //TODO Add top? + /* + top := chi.URLParam(r, "top") + topInt, err := strconv.ParseInt(top, 0, 32) + if err != nil { + panic(err) + } + res, err := s.Db.ListSources(*s.ctx, int32(topInt)) + */ + + query := r.URL.Query() + _source := query["source"][0] + + // Shows the list by Sources.source + res, err := s.Db.ListSourcesBySource(*s.ctx, strings.ToLower(_source)) + if err != nil { + http.Error(w, "invalid source is missing a value", http.StatusBadRequest) + return + } bResult, err := json.Marshal(res) if err != nil { http.Error(w, "unable to convert to json", http.StatusBadRequest) @@ -116,10 +153,6 @@ func (s *Server) newRedditSource(w http.ResponseWriter, r *http.Request) { w.Write(bJson) } -func (s *Server) getSourceByType(w http.ResponseWriter, r *http.Request) { - -} - // NewYoutubeSource // @Summary Creates a new youtube source to monitor. // @Param name query string true "name"