2022-06-19 22:02:44 -07:00
|
|
|
package routes
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2023-01-31 08:19:23 -08:00
|
|
|
"strconv"
|
2022-06-19 22:02:44 -07:00
|
|
|
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
"github.com/google/uuid"
|
2023-01-22 10:12:55 -08:00
|
|
|
"github.com/jtom38/newsbot/collector/domain/models"
|
2022-06-19 22:02:44 -07:00
|
|
|
)
|
|
|
|
|
2023-01-22 10:12:55 -08:00
|
|
|
func (s *Server) GetArticleRouter() http.Handler {
|
|
|
|
r := chi.NewRouter()
|
|
|
|
|
|
|
|
r.Get("/", s.listArticles)
|
|
|
|
r.Route("/{ID}", func(r chi.Router) {
|
|
|
|
r.Get("/", s.getArticle)
|
|
|
|
r.Get("/details", s.getArticleDetails)
|
|
|
|
})
|
|
|
|
r.Get("/by/sourceid", s.GetArticlesBySourceId)
|
|
|
|
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
type ArticlesListResults struct {
|
|
|
|
ApiStatusModel
|
|
|
|
Payload []models.ArticleDto `json:"payload"`
|
|
|
|
}
|
|
|
|
|
2023-01-31 08:19:23 -08:00
|
|
|
type ArticleGetResults struct {
|
|
|
|
ApiStatusModel
|
|
|
|
Payload models.ArticleDto `json:"payload"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type ArticleDetailsResult struct {
|
|
|
|
ApiStatusModel
|
|
|
|
Payload models.ArticleDetailsDto `json:"payload"`
|
|
|
|
}
|
|
|
|
|
2022-06-19 22:02:44 -07:00
|
|
|
// ListArticles
|
2023-02-04 20:15:10 -08:00
|
|
|
// @Summary Lists the top 25 records ordering from newest to oldest.
|
2022-06-19 22:02:44 -07:00
|
|
|
// @Produce application/json
|
2023-01-31 08:19:23 -08:00
|
|
|
// @Param page query string false "page number"
|
2022-07-12 15:28:31 -07:00
|
|
|
// @Tags Articles
|
2022-06-19 22:02:44 -07:00
|
|
|
// @Router /articles [get]
|
2023-01-22 10:12:55 -08:00
|
|
|
// @Success 200 {object} ArticlesListResults "OK"
|
2022-06-19 22:02:44 -07:00
|
|
|
func (s *Server) listArticles(w http.ResponseWriter, r *http.Request) {
|
2023-01-22 10:12:55 -08:00
|
|
|
p := ArticlesListResults{
|
|
|
|
ApiStatusModel: ApiStatusModel{
|
|
|
|
Message: "OK",
|
|
|
|
StatusCode: http.StatusOK,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-01-31 08:19:23 -08:00
|
|
|
query := r.URL.Query()
|
|
|
|
queryPage := query["page"]
|
2023-01-31 12:39:54 -08:00
|
|
|
|
2023-01-31 08:19:23 -08:00
|
|
|
// if a page number was sent, process it
|
2023-02-04 20:15:10 -08:00
|
|
|
if len(queryPage) >= 1 {
|
2023-01-31 08:19:23 -08:00
|
|
|
page, err := strconv.Atoi(query["page"][0])
|
|
|
|
if err != nil {
|
|
|
|
s.WriteError(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-02-04 20:15:10 -08:00
|
|
|
res, err := s.dto.ListArticles(r.Context(), 25, page)
|
2023-01-31 08:19:23 -08:00
|
|
|
if err != nil {
|
|
|
|
s.WriteError(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
p.Payload = res
|
|
|
|
s.WriteJson(w, p)
|
|
|
|
} else {
|
2023-02-04 20:15:10 -08:00
|
|
|
res, err := s.dto.ListArticles(r.Context(), 25, 0)
|
2023-01-31 08:19:23 -08:00
|
|
|
if err != nil {
|
|
|
|
s.WriteError(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
p.Payload = res
|
|
|
|
s.WriteJson(w, p)
|
2022-06-19 22:02:44 -07:00
|
|
|
}
|
2023-01-22 10:12:55 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetArticle
|
2022-06-19 22:02:44 -07:00
|
|
|
// @Summary Returns an article based on defined ID.
|
2023-01-22 10:12:55 -08:00
|
|
|
// @Param ID path string true "uuid"
|
2022-06-19 22:02:44 -07:00
|
|
|
// @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]
|
2023-01-22 10:12:55 -08:00
|
|
|
// @Success 200 {object} ArticleGetResults "OK"
|
|
|
|
func (s *Server) getArticle(w http.ResponseWriter, r *http.Request) {
|
2023-01-31 08:19:23 -08:00
|
|
|
p := ArticleGetResults{
|
2023-01-22 10:12:55 -08:00
|
|
|
ApiStatusModel: ApiStatusModel{
|
2023-01-31 08:19:23 -08:00
|
|
|
Message: "OK",
|
2023-01-22 10:12:55 -08:00
|
|
|
StatusCode: http.StatusOK,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-06-19 22:02:44 -07:00
|
|
|
id := chi.URLParam(r, "ID")
|
|
|
|
uuid, err := uuid.Parse(id)
|
|
|
|
if err != nil {
|
2023-01-22 10:12:55 -08:00
|
|
|
s.WriteError(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
2022-06-19 22:02:44 -07:00
|
|
|
}
|
|
|
|
|
2023-01-22 10:12:55 -08:00
|
|
|
res, err := s.dto.GetArticle(r.Context(), uuid)
|
2022-06-19 22:02:44 -07:00
|
|
|
if err != nil {
|
2023-01-22 10:12:55 -08:00
|
|
|
s.WriteError(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
2022-06-19 22:02:44 -07:00
|
|
|
}
|
|
|
|
|
2023-01-22 10:12:55 -08:00
|
|
|
p.Payload = res
|
|
|
|
|
2023-01-31 08:19:23 -08:00
|
|
|
s.WriteJson(w, p)
|
2023-01-22 10:12:55 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetArticleDetails
|
|
|
|
// @Summary Returns an article and source based on defined ID.
|
|
|
|
// @Param ID path string true "uuid"
|
2022-06-19 22:02:44 -07:00
|
|
|
// @Produce application/json
|
2022-07-12 15:28:31 -07:00
|
|
|
// @Tags Articles
|
2023-01-22 10:12:55 -08:00
|
|
|
// @Router /articles/{ID}/details [get]
|
|
|
|
// @Success 200 {object} ArticleDetailsResult "OK"
|
|
|
|
func (s *Server) getArticleDetails(w http.ResponseWriter, r *http.Request) {
|
2023-01-31 08:19:23 -08:00
|
|
|
p := ArticleDetailsResult{
|
2023-01-22 10:12:55 -08:00
|
|
|
ApiStatusModel: ApiStatusModel{
|
|
|
|
Message: "OK",
|
|
|
|
StatusCode: http.StatusOK,
|
|
|
|
},
|
|
|
|
}
|
2022-06-19 22:02:44 -07:00
|
|
|
|
2023-01-22 10:12:55 -08:00
|
|
|
id := chi.URLParam(r, "ID")
|
|
|
|
uuid, err := uuid.Parse(id)
|
2022-06-19 22:02:44 -07:00
|
|
|
if err != nil {
|
2023-01-22 10:12:55 -08:00
|
|
|
s.WriteError(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
2022-06-19 22:02:44 -07:00
|
|
|
}
|
|
|
|
|
2023-01-22 10:12:55 -08:00
|
|
|
res, err := s.dto.GetArticleDetails(r.Context(), uuid)
|
2022-06-19 22:02:44 -07:00
|
|
|
if err != nil {
|
2023-01-22 10:12:55 -08:00
|
|
|
s.WriteError(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
2022-06-19 22:02:44 -07:00
|
|
|
}
|
|
|
|
|
2023-01-22 10:12:55 -08:00
|
|
|
p.Payload = res
|
|
|
|
|
2023-01-31 08:19:23 -08:00
|
|
|
s.WriteJson(w, p)
|
2023-01-22 10:12:55 -08:00
|
|
|
}
|
|
|
|
|
2022-07-12 15:28:31 -07:00
|
|
|
// TODO add page support
|
2023-01-22 10:12:55 -08:00
|
|
|
// GetArticlesBySourceID
|
2022-07-12 15:28:31 -07:00
|
|
|
// @Summary Finds the articles based on the SourceID provided. Returns the top 50.
|
2023-01-22 10:12:55 -08:00
|
|
|
// @Param id query string true "Source ID UUID"
|
2022-07-12 15:28:31 -07:00
|
|
|
// @Produce application/json
|
|
|
|
// @Tags Articles
|
2023-01-22 10:12:55 -08:00
|
|
|
// @Router /articles/by/sourceid [get]
|
|
|
|
// @Success 200 {object} ArticlesListResults "OK"
|
|
|
|
func (s *Server) GetArticlesBySourceId(w http.ResponseWriter, r *http.Request) {
|
2023-01-31 08:19:23 -08:00
|
|
|
p := ArticlesListResults{
|
|
|
|
ApiStatusModel: ApiStatusModel{
|
|
|
|
Message: "OK",
|
|
|
|
StatusCode: http.StatusOK,
|
|
|
|
},
|
|
|
|
}
|
2022-07-12 15:28:31 -07:00
|
|
|
|
|
|
|
r.URL.Query()
|
|
|
|
query := r.URL.Query()
|
2023-01-22 10:12:55 -08:00
|
|
|
_id := query["id"][0]
|
2022-07-12 15:28:31 -07:00
|
|
|
|
|
|
|
uuid, err := uuid.Parse(_id)
|
|
|
|
if err != nil {
|
2023-01-22 10:12:55 -08:00
|
|
|
s.WriteError(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
2022-07-12 15:28:31 -07:00
|
|
|
}
|
|
|
|
|
2023-01-22 10:12:55 -08:00
|
|
|
res, err := s.dto.GetArticlesBySourceId(r.Context(), uuid)
|
2022-07-12 15:28:31 -07:00
|
|
|
if err != nil {
|
2023-01-22 10:12:55 -08:00
|
|
|
s.WriteError(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
2022-07-12 15:28:31 -07:00
|
|
|
}
|
2023-01-31 08:19:23 -08:00
|
|
|
p.Payload = res
|
2022-07-12 15:28:31 -07:00
|
|
|
|
2023-01-31 08:19:23 -08:00
|
|
|
s.WriteJson(w, p)
|
2022-07-12 15:28:31 -07:00
|
|
|
}
|