Features/get route updates (#37)

* cleaned up get routes

* added a func to query articles by page

* removed unused imports

* merged page with listArticles as a optional
This commit is contained in:
James Tombleson 2023-01-31 08:19:23 -08:00 committed by GitHub
parent ada453e08a
commit 90e739a56e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 202 additions and 214 deletions

View File

@ -948,6 +948,55 @@ func (q *Queries) ListArticlesByDate(ctx context.Context, limit int32) ([]Articl
return items, nil
}
const listArticlesByPage = `-- name: ListArticlesByPage :many
select id, sourceid, tags, title, url, pubdate, video, videoheight, videowidth, thumbnail, description, authorname, authorimage from articles
order by pubdate desc
offset $2
fetch next $1 rows only
`
type ListArticlesByPageParams struct {
Limit int32
Offset int32
}
func (q *Queries) ListArticlesByPage(ctx context.Context, arg ListArticlesByPageParams) ([]Article, error) {
rows, err := q.db.QueryContext(ctx, listArticlesByPage, arg.Limit, arg.Offset)
if err != nil {
return nil, err
}
defer rows.Close()
var items []Article
for rows.Next() {
var i Article
if err := rows.Scan(
&i.ID,
&i.Sourceid,
&i.Tags,
&i.Title,
&i.Url,
&i.Pubdate,
&i.Video,
&i.Videoheight,
&i.Videowidth,
&i.Thumbnail,
&i.Description,
&i.Authorname,
&i.Authorimage,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const listDiscordQueueItems = `-- name: ListDiscordQueueItems :many
Select id, articleid from DiscordQueue LIMIT $1
`

View File

@ -35,6 +35,12 @@ Left Join sources
On articles.sourceid = sources.id
Where name = $1;
-- name: ListArticlesByPage :many
select * from articles
order by pubdate desc
offset $2
fetch next $1 rows only;
-- name: CreateArticle :exec
INSERT INTO Articles
(ID, SourceId, Tags, Title, Url, PubDate, Video, VideoHeight, VideoWidth, Thumbnail, Description, AuthorName, AuthorImage)

View File

@ -25,6 +25,14 @@ const docTemplate = `{
"Articles"
],
"summary": "Lists the top 50 records",
"parameters": [
{
"type": "string",
"description": "page number",
"name": "page",
"in": "query"
}
],
"responses": {
"200": {
"description": "OK",

View File

@ -16,6 +16,14 @@
"Articles"
],
"summary": "Lists the top 50 records",
"parameters": [
{
"type": "string",
"description": "page number",
"name": "page",
"in": "query"
}
],
"responses": {
"200": {
"description": "OK",

View File

@ -241,6 +241,11 @@ info:
paths:
/articles:
get:
parameters:
- description: page number
in: query
name: page
type: string
produces:
- application/json
responses:

View File

@ -35,6 +35,24 @@ func (c DtoClient) ListArticles(ctx context.Context, limit int) ([]models.Articl
return res, nil
}
func (c DtoClient) ListArticlesByPage(ctx context.Context, page, limit int32 ) ([]models.ArticleDto, error) {
var res []models.ArticleDto
a, err := c.db.ListArticlesByPage(ctx, database.ListArticlesByPageParams{
Limit: limit,
Offset: page * limit,
})
if err != nil {
return res, err
}
for _, article := range a {
res = append(res, c.convertArticle(article))
}
return res, nil
}
func (c DtoClient) GetArticle(ctx context.Context, ID uuid.UUID) (models.ArticleDto, error) {
a, err := c.db.GetArticleByID(ctx, ID)
if err != nil {

View File

@ -1,8 +1,9 @@
package routes
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"github.com/go-chi/chi/v5"
"github.com/google/uuid"
@ -27,9 +28,20 @@ type ArticlesListResults struct {
Payload []models.ArticleDto `json:"payload"`
}
type ArticleGetResults struct {
ApiStatusModel
Payload models.ArticleDto `json:"payload"`
}
type ArticleDetailsResult struct {
ApiStatusModel
Payload models.ArticleDetailsDto `json:"payload"`
}
// ListArticles
// @Summary Lists the top 50 records
// @Produce application/json
// @Param page query string false "page number"
// @Tags Articles
// @Router /articles [get]
// @Success 200 {object} ArticlesListResults "OK"
@ -41,27 +53,34 @@ func (s *Server) listArticles(w http.ResponseWriter, r *http.Request) {
},
}
w.Header().Set(HeaderContentType, ApplicationJson)
query := r.URL.Query()
queryPage := query["page"]
fmt.Printf("queryPage: %v\n", queryPage)
// if a page number was sent, process it
if len(queryPage) == 1 {
page, err := strconv.Atoi(query["page"][0])
if err != nil {
s.WriteError(w, err.Error(), http.StatusBadRequest)
return
}
res, err := s.dto.ListArticlesByPage(r.Context(), int32(page), 50)
if err != nil {
s.WriteError(w, err.Error(), http.StatusInternalServerError)
return
}
p.Payload = res
s.WriteJson(w, p)
} else {
res, err := s.dto.ListArticles(r.Context(), 50)
if err != nil {
s.WriteError(w, err.Error(), http.StatusInternalServerError)
return
}
p.Payload = res
bres, err := json.Marshal(p)
if err != nil {
s.WriteError(w, err.Error(), http.StatusInternalServerError)
return
s.WriteJson(w, p)
}
w.Write(bres)
}
type ArticleGetResults struct {
ApiStatusModel
Payload models.ArticleDto `json:"payload"`
}
// GetArticle
@ -72,15 +91,13 @@ type ArticleGetResults struct {
// @Router /articles/{ID} [get]
// @Success 200 {object} ArticleGetResults "OK"
func (s *Server) getArticle(w http.ResponseWriter, r *http.Request) {
p := ArticleGetResults {
p := ArticleGetResults{
ApiStatusModel: ApiStatusModel{
Message: "OK",
StatusCode: http.StatusOK,
},
}
w.Header().Set(HeaderContentType, ApplicationJson)
id := chi.URLParam(r, "ID")
uuid, err := uuid.Parse(id)
if err != nil {
@ -96,18 +113,7 @@ func (s *Server) getArticle(w http.ResponseWriter, r *http.Request) {
p.Payload = res
bres, err := json.Marshal(p)
if err != nil {
s.WriteError(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write(bres)
}
type ArticleDetailsResult struct {
ApiStatusModel
Payload models.ArticleDetailsDto `json:"payload"`
s.WriteJson(w, p)
}
// GetArticleDetails
@ -118,15 +124,13 @@ type ArticleDetailsResult struct {
// @Router /articles/{ID}/details [get]
// @Success 200 {object} ArticleDetailsResult "OK"
func (s *Server) getArticleDetails(w http.ResponseWriter, r *http.Request) {
p := ArticleDetailsResult {
p := ArticleDetailsResult{
ApiStatusModel: ApiStatusModel{
Message: "OK",
StatusCode: http.StatusOK,
},
}
w.Header().Set(HeaderContentType, ApplicationJson)
id := chi.URLParam(r, "ID")
uuid, err := uuid.Parse(id)
if err != nil {
@ -142,18 +146,7 @@ func (s *Server) getArticleDetails(w http.ResponseWriter, r *http.Request) {
p.Payload = res
bres, err := json.Marshal(p)
if err != nil {
s.WriteError(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write(bres)
}
type ArticlesBySourceIDResults struct {
ApiStatusModel
Payload []models.ArticleDto `json:"payload"`
s.WriteJson(w, p)
}
// TODO add page support
@ -165,7 +158,12 @@ type ArticlesBySourceIDResults struct {
// @Router /articles/by/sourceid [get]
// @Success 200 {object} ArticlesListResults "OK"
func (s *Server) GetArticlesBySourceId(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
p := ArticlesListResults{
ApiStatusModel: ApiStatusModel{
Message: "OK",
StatusCode: http.StatusOK,
},
}
r.URL.Query()
query := r.URL.Query()
@ -182,12 +180,7 @@ func (s *Server) GetArticlesBySourceId(w http.ResponseWriter, r *http.Request) {
s.WriteError(w, err.Error(), http.StatusInternalServerError)
return
}
p.Payload = res
bres, err := json.Marshal(res)
if err != nil {
s.WriteError(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write(bres)
s.WriteJson(w, p)
}

View File

@ -12,6 +12,16 @@ import (
"github.com/jtom38/newsbot/collector/domain/models"
)
type ListDiscordWebhooks struct {
ApiStatusModel
Payload []models.DiscordWebHooksDto `json:"payload"`
}
type GetDiscordWebhook struct {
ApiStatusModel
Payload models.DiscordWebHooksDto `json:"payload"`
}
func (s Server) DiscordWebHookRouter() http.Handler {
r := chi.NewRouter()
@ -28,11 +38,6 @@ func (s Server) DiscordWebHookRouter() http.Handler {
return r
}
type ListDiscordWebhooks struct {
ApiStatusModel
Payload []models.DiscordWebHooksDto `json:"payload"`
}
// ListDiscordWebhooks
// @Summary Returns the top 100 entries from the queue to be processed.
// @Produce application/json
@ -46,27 +51,13 @@ func (s *Server) ListDiscordWebHooks(w http.ResponseWriter, r *http.Request) {
},
}
w.Header().Set(HeaderContentType, ApplicationJson)
res, err := s.dto.ListDiscordWebHooks(r.Context(), 50)
if err != nil {
s.WriteError(w, err.Error(), http.StatusInternalServerError)
return
}
p.Payload = res
bres, err := json.Marshal(p)
if err != nil {
s.WriteError(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write(bres)
}
type GetDiscordWebhook struct {
ApiStatusModel
Payload models.DiscordWebHooksDto `json:"payload"`
s.WriteJson(w, p)
}
// GetDiscordWebHook
@ -84,8 +75,6 @@ func (s *Server) GetDiscordWebHooksById(w http.ResponseWriter, r *http.Request)
},
}
w.Header().Set(HeaderContentType, ApplicationJson)
_id := chi.URLParam(r, "ID")
if _id == "" {
s.WriteError(w, "id is missing", http.StatusBadRequest)
@ -104,14 +93,7 @@ func (s *Server) GetDiscordWebHooksById(w http.ResponseWriter, r *http.Request)
return
}
p.Payload = res
bres, err := json.Marshal(p)
if err != nil {
s.WriteError(w, "unable to convert to json", http.StatusBadRequest)
return
}
w.Write(bres)
s.WriteJson(w, p)
}
// GetDiscordWebHookByServerAndChannel
@ -130,8 +112,6 @@ func (s *Server) GetDiscordWebHooksByServerAndChannel(w http.ResponseWriter, r *
},
}
w.Header().Set(HeaderContentType, ApplicationJson)
query := r.URL.Query()
_server := query["server"][0]
if _server == "" {
@ -152,14 +132,7 @@ func (s *Server) GetDiscordWebHooksByServerAndChannel(w http.ResponseWriter, r *
}
p.Payload = res
bres, err := json.Marshal(p)
if err != nil {
s.WriteError(w, "unable to convert to json", http.StatusInternalServerError)
return
}
w.Write(bres)
s.WriteJson(w, p)
}
// NewDiscordWebHook

View File

@ -1,13 +1,17 @@
package routes
import (
"encoding/json"
"net/http"
"github.com/go-chi/chi/v5"
"github.com/jtom38/newsbot/collector/domain/models"
)
type ListDiscordWebHooksQueueResults struct {
ApiStatusModel
Payload []models.DiscordQueueDetailsDto `json:"payload"`
}
func (s *Server) GetQueueRouter() http.Handler {
r := chi.NewRouter()
@ -16,11 +20,6 @@ func (s *Server) GetQueueRouter() http.Handler {
return r
}
type ListDiscordWebHooksQueueResults struct {
ApiStatusModel
Payload []models.DiscordQueueDetailsDto `json:"payload"`
}
// GetDiscordQueue
// @Summary Returns the top 100 entries from the queue to be processed.
// @Produce application/json
@ -28,8 +27,6 @@ type ListDiscordWebHooksQueueResults struct {
// @Router /queue/discord/webhooks [get]
// @Success 200 {object} ListDiscordWebHooksQueueResults "ok"
func (s *Server) ListDiscordWebhookQueue(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
p := ListDiscordWebHooksQueueResults{
ApiStatusModel: ApiStatusModel{
Message: "OK",
@ -45,13 +42,5 @@ func (s *Server) ListDiscordWebhookQueue(w http.ResponseWriter, r *http.Request)
}
p.Payload = res
// convert to json
b, err := json.Marshal(p)
if err != nil {
s.WriteError(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write(b)
s.WriteJson(w, p)
}

View File

@ -100,15 +100,27 @@ type ApiError struct {
func (s *Server) WriteError(w http.ResponseWriter, errMessage string, HttpStatusCode int) {
e := ApiError{
ApiStatusModel: &ApiStatusModel{
StatusCode: http.StatusInternalServerError,
StatusCode: HttpStatusCode,
Message: errMessage,
},
}
b, err := json.Marshal(e)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
http.Error(w, err.Error(), HttpStatusCode)
}
w.Write(b)
}
func (s *Server) WriteJson(w http.ResponseWriter, model interface{}) {
w.Header().Set(HeaderContentType, ApplicationJson)
bres, err := json.Marshal(model)
if err != nil {
s.WriteError(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write(bres)
}

View File

@ -61,8 +61,7 @@ func (s *Server) listSources(w http.ResponseWriter, r *http.Request) {
res, err := s.Db.ListSources(*s.ctx, int32(topInt))
*/
w.Header().Set(HeaderContentType, ApplicationJson)
result := ListSources{
p := ListSources{
ApiStatusModel: ApiStatusModel{
StatusCode: http.StatusOK,
Message: "OK",
@ -76,15 +75,8 @@ func (s *Server) listSources(w http.ResponseWriter, r *http.Request) {
return
}
result.Payload = items
bResult, err := json.Marshal(result)
if err != nil {
s.WriteError(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write(bResult)
p.Payload = items
s.WriteJson(w, p)
}
// ListSourcesBySource
@ -106,9 +98,8 @@ func (s *Server) listSourcesBySource(w http.ResponseWriter, r *http.Request) {
}
res, err := s.Db.ListSources(*s.ctx, int32(topInt))
*/
w.Header().Set(HeaderContentType, ApplicationJson)
result := ListSources{
p := ListSources{
ApiStatusModel: ApiStatusModel{
StatusCode: http.StatusOK,
Message: "OK",
@ -116,24 +107,16 @@ func (s *Server) listSourcesBySource(w http.ResponseWriter, r *http.Request) {
}
query := r.URL.Query()
_source := query["source"][0]
// Shows the list by Sources.source
res, err := s.dto.ListSourcesBySource(r.Context(), _source)
res, err := s.dto.ListSourcesBySource(r.Context(), query["source"][0])
if err != nil {
s.WriteError(w, err.Error(), http.StatusBadRequest)
return
}
result.Payload = res
bResult, err := json.Marshal(result)
if err != nil {
s.WriteError(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write(bResult)
p.Payload = res
s.WriteJson(w, p)
}
// GetSource
@ -154,10 +137,7 @@ func (s *Server) getSources(w http.ResponseWriter, r *http.Request) {
},
}
w.Header().Set(HeaderContentType, ApplicationJson)
id := chi.URLParam(r, "ID")
uuid, err := uuid.Parse(id)
uuid, err := uuid.Parse(chi.URLParam(r, "ID"))
if err != nil {
s.WriteError(w, err.Error(), http.StatusBadRequest)
return
@ -170,14 +150,7 @@ func (s *Server) getSources(w http.ResponseWriter, r *http.Request) {
}
payload.Payload = res
bResult, err := json.Marshal(payload)
if err != nil {
s.WriteError(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write(bResult)
s.WriteJson(w, payload)
}
// GetSourceByNameAndSource
@ -219,15 +192,7 @@ func (s *Server) GetSourceBySourceAndName(w http.ResponseWriter, r *http.Request
}
p.Payload = item
bResult, err := json.Marshal(item)
if err != nil {
s.WriteError(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set(HeaderContentType, ApplicationJson)
w.Write(bResult)
s.WriteJson(w, p)
}
// NewRedditSource
@ -242,8 +207,6 @@ func (s *Server) newRedditSource(w http.ResponseWriter, r *http.Request) {
_url := query["url"][0]
//_tags := query["tags"][0]
w.Header().Set("Content-Type", "application/json")
if _url == "" {
s.WriteError(w, "url is missing a value", http.StatusBadRequest)
return
@ -277,6 +240,7 @@ func (s *Server) newRedditSource(w http.ResponseWriter, r *http.Request) {
s.WriteError(w, err.Error(), http.StatusInternalServerError)
return
}
//s.WriteJson(w, &params)
bJson, err := json.Marshal(&params)
if err != nil {

View File

@ -34,6 +34,11 @@ type GetSubscription struct {
Payload models.SubscriptionDto `json:"payload"`
}
type ListSubscriptionDetails struct {
ApiStatusModel
Payload []models.SubscriptionDetailsDto `json:"payload"`
}
// GetSubscriptions
// @Summary Returns the top 100 entries from the queue to be processed.
// @Produce application/json
@ -43,8 +48,6 @@ type GetSubscription struct {
// @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")
payload := ListSubscriptions{
ApiStatusModel: ApiStatusModel{
StatusCode: http.StatusOK,
@ -59,19 +62,7 @@ func (s *Server) ListSubscriptions(w http.ResponseWriter, r *http.Request) {
}
payload.Payload = res
bres, err := json.Marshal(payload)
if err != nil {
s.WriteError(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write(bres)
}
type ListSubscriptionDetails struct {
ApiStatusModel
Payload []models.SubscriptionDetailsDto `json:"payload"`
s.WriteJson(w, payload)
}
// ListSubscriptionDetails
@ -80,8 +71,7 @@ type ListSubscriptionDetails struct {
// @Tags Subscription
// @Router /subscriptions/details [get]
// @Success 200 {object} ListSubscriptionDetails "ok"
func (s Server) ListSubscriptionDetails(w http.ResponseWriter, r *http.Request) {
w.Header().Set(HeaderContentType, ApplicationJson)
func (s *Server) ListSubscriptionDetails(w http.ResponseWriter, r *http.Request) {
payload := ListSubscriptionDetails{
ApiStatusModel: ApiStatusModel{
StatusCode: http.StatusOK,
@ -96,13 +86,7 @@ func (s Server) ListSubscriptionDetails(w http.ResponseWriter, r *http.Request)
}
payload.Payload = res
b, err := json.Marshal(payload)
if err != nil {
s.WriteError(w, err.Error(), http.StatusInternalServerError)
}
w.Write(b)
s.WriteJson(w, payload)
}
// GetSubscriptionsByDiscordId
@ -115,8 +99,6 @@ func (s Server) ListSubscriptionDetails(w http.ResponseWriter, r *http.Request)
// @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) {
w.Header().Set(HeaderContentType, ApplicationJson)
p := ListSubscriptions{
ApiStatusModel: ApiStatusModel{
StatusCode: http.StatusOK,
@ -125,13 +107,12 @@ func (s *Server) GetSubscriptionsByDiscordId(w http.ResponseWriter, r *http.Requ
}
query := r.URL.Query()
_id := query["id"][0]
if _id == "" {
if query["id"][0] == "" {
s.WriteError(w, ErrIdValueMissing, http.StatusBadRequest)
return
}
uuid, err := uuid.Parse(_id)
uuid, err := uuid.Parse(query["id"][0])
if err != nil {
s.WriteError(w, ErrValueNotUuid, http.StatusBadRequest)
return
@ -144,14 +125,7 @@ func (s *Server) GetSubscriptionsByDiscordId(w http.ResponseWriter, r *http.Requ
}
p.Payload = res
bres, err := json.Marshal(p)
if err != nil {
s.WriteError(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write(bres)
s.WriteJson(w, p)
}
// GetSubscriptionsBySourceId
@ -162,8 +136,6 @@ func (s *Server) GetSubscriptionsByDiscordId(w http.ResponseWriter, r *http.Requ
// @Router /subscriptions/by/SourceId [get]
// @Success 200 {object} ListSubscriptions "ok"
func (s *Server) GetSubscriptionsBySourceId(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
p := ListSubscriptions{
ApiStatusModel: ApiStatusModel{
StatusCode: http.StatusOK,
@ -191,14 +163,7 @@ func (s *Server) GetSubscriptionsBySourceId(w http.ResponseWriter, r *http.Reque
}
p.Payload = res
bres, err := json.Marshal(p)
if err != nil {
s.WriteError(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write(bres)
s.WriteJson(w, p)
}
// NewDiscordWebHookSubscription
@ -208,8 +173,6 @@ func (s *Server) GetSubscriptionsBySourceId(w http.ResponseWriter, r *http.Reque
// @Tags Subscription
// @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]