2022-06-19 22:02:44 -07:00
|
|
|
package routes
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ListArticles
|
|
|
|
// @Summary Lists the top 50 records
|
|
|
|
// @Produce application/json
|
2022-07-12 15:28:31 -07:00
|
|
|
// @Tags Articles
|
2022-06-19 22:02:44 -07:00
|
|
|
// @Router /articles [get]
|
|
|
|
func (s *Server) listArticles(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
2022-07-12 15:28:31 -07:00
|
|
|
res, err := s.Db.ListArticlesByDate(*s.ctx, 50)
|
2022-06-19 22:02:44 -07:00
|
|
|
if err != nil {
|
|
|
|
w.Write([]byte(err.Error()))
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
bres, err := json.Marshal(res)
|
|
|
|
if err != nil {
|
|
|
|
w.Write([]byte(err.Error()))
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
w.Write(bres)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetArticleById
|
|
|
|
// @Summary Returns an article based on defined ID.
|
|
|
|
// @Param id path string true "uuid"
|
|
|
|
// @Produce application/json
|
2022-07-12 15:28:31 -07:00
|
|
|
// @Tags Articles
|
2022-12-04 08:49:17 -08:00
|
|
|
// @Router /articles/{ID} [get]
|
2022-06-19 22:02:44 -07:00
|
|
|
func (s *Server) getArticleById(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
id := chi.URLParam(r, "ID")
|
|
|
|
uuid, err := uuid.Parse(id)
|
|
|
|
if err != nil {
|
|
|
|
w.Write([]byte(err.Error()))
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := s.Db.GetArticleByID(*s.ctx, uuid)
|
|
|
|
if err != nil {
|
|
|
|
w.Write([]byte(err.Error()))
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
bres, err := json.Marshal(res)
|
|
|
|
if err != nil {
|
|
|
|
w.Write([]byte(err.Error()))
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Write(bres)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO add page support
|
|
|
|
// GetArticlesBySourceID
|
|
|
|
// @Summary Finds the articles based on the SourceID provided. Returns the top 50.
|
2022-06-30 14:54:58 -07:00
|
|
|
// @Param id query string true "Source ID UUID"
|
2022-06-19 22:02:44 -07:00
|
|
|
// @Produce application/json
|
2022-07-12 15:28:31 -07:00
|
|
|
// @Tags Articles
|
2022-06-30 14:54:58 -07:00
|
|
|
// @Router /articles/by/sourceid [get]
|
2022-06-19 22:02:44 -07:00
|
|
|
func (s *Server) GetArticlesBySourceId(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
2022-06-30 14:54:58 -07:00
|
|
|
r.URL.Query()
|
|
|
|
query := r.URL.Query()
|
|
|
|
_id := query["id"][0]
|
|
|
|
|
|
|
|
uuid, err := uuid.Parse(_id)
|
2022-06-19 22:02:44 -07:00
|
|
|
if err != nil {
|
|
|
|
w.Write([]byte(err.Error()))
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2022-11-06 19:07:44 -08:00
|
|
|
res, err := s.Db.GetNewArticlesBySourceId(*s.ctx, uuid)
|
|
|
|
//res, err := s.Db.GetArticlesBySourceId(*s.ctx, uuid)
|
2022-06-19 22:02:44 -07:00
|
|
|
if err != nil {
|
|
|
|
w.Write([]byte(err.Error()))
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
bres, err := json.Marshal(res)
|
|
|
|
if err != nil {
|
|
|
|
w.Write([]byte(err.Error()))
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Write(bres)
|
2022-07-12 15:28:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO add page support
|
|
|
|
// GetArticlesByTag
|
|
|
|
// @Summary Finds the articles based on the SourceID provided. Returns the top 50.
|
2022-07-13 21:31:53 -07:00
|
|
|
// @Param tag query string true "Tag name"
|
2022-07-12 15:28:31 -07:00
|
|
|
// @Produce application/json
|
|
|
|
// @Tags Articles
|
2022-07-13 21:31:53 -07:00
|
|
|
// @Router /articles/by/tag [get]
|
2022-07-12 15:28:31 -07:00
|
|
|
func (s *Server) GetArticlesByTag(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
r.URL.Query()
|
|
|
|
query := r.URL.Query()
|
2022-07-13 21:31:53 -07:00
|
|
|
_id := query["tag"][0]
|
2022-07-12 15:28:31 -07:00
|
|
|
|
|
|
|
uuid, err := uuid.Parse(_id)
|
|
|
|
if err != nil {
|
|
|
|
w.Write([]byte(err.Error()))
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := s.Db.GetArticlesBySourceId(*s.ctx, uuid)
|
|
|
|
if err != nil {
|
|
|
|
w.Write([]byte(err.Error()))
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
bres, err := json.Marshal(res)
|
|
|
|
if err != nil {
|
|
|
|
w.Write([]byte(err.Error()))
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Write(bres)
|
|
|
|
}
|