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:
parent
ada453e08a
commit
90e739a56e
@ -948,6 +948,55 @@ func (q *Queries) ListArticlesByDate(ctx context.Context, limit int32) ([]Articl
|
|||||||
return items, nil
|
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
|
const listDiscordQueueItems = `-- name: ListDiscordQueueItems :many
|
||||||
Select id, articleid from DiscordQueue LIMIT $1
|
Select id, articleid from DiscordQueue LIMIT $1
|
||||||
`
|
`
|
||||||
|
@ -35,6 +35,12 @@ Left Join sources
|
|||||||
On articles.sourceid = sources.id
|
On articles.sourceid = sources.id
|
||||||
Where name = $1;
|
Where name = $1;
|
||||||
|
|
||||||
|
-- name: ListArticlesByPage :many
|
||||||
|
select * from articles
|
||||||
|
order by pubdate desc
|
||||||
|
offset $2
|
||||||
|
fetch next $1 rows only;
|
||||||
|
|
||||||
-- name: CreateArticle :exec
|
-- name: CreateArticle :exec
|
||||||
INSERT INTO Articles
|
INSERT INTO Articles
|
||||||
(ID, SourceId, Tags, Title, Url, PubDate, Video, VideoHeight, VideoWidth, Thumbnail, Description, AuthorName, AuthorImage)
|
(ID, SourceId, Tags, Title, Url, PubDate, Video, VideoHeight, VideoWidth, Thumbnail, Description, AuthorName, AuthorImage)
|
||||||
|
@ -25,6 +25,14 @@ const docTemplate = `{
|
|||||||
"Articles"
|
"Articles"
|
||||||
],
|
],
|
||||||
"summary": "Lists the top 50 records",
|
"summary": "Lists the top 50 records",
|
||||||
|
"parameters": [
|
||||||
|
{
|
||||||
|
"type": "string",
|
||||||
|
"description": "page number",
|
||||||
|
"name": "page",
|
||||||
|
"in": "query"
|
||||||
|
}
|
||||||
|
],
|
||||||
"responses": {
|
"responses": {
|
||||||
"200": {
|
"200": {
|
||||||
"description": "OK",
|
"description": "OK",
|
||||||
|
@ -16,6 +16,14 @@
|
|||||||
"Articles"
|
"Articles"
|
||||||
],
|
],
|
||||||
"summary": "Lists the top 50 records",
|
"summary": "Lists the top 50 records",
|
||||||
|
"parameters": [
|
||||||
|
{
|
||||||
|
"type": "string",
|
||||||
|
"description": "page number",
|
||||||
|
"name": "page",
|
||||||
|
"in": "query"
|
||||||
|
}
|
||||||
|
],
|
||||||
"responses": {
|
"responses": {
|
||||||
"200": {
|
"200": {
|
||||||
"description": "OK",
|
"description": "OK",
|
||||||
|
@ -241,6 +241,11 @@ info:
|
|||||||
paths:
|
paths:
|
||||||
/articles:
|
/articles:
|
||||||
get:
|
get:
|
||||||
|
parameters:
|
||||||
|
- description: page number
|
||||||
|
in: query
|
||||||
|
name: page
|
||||||
|
type: string
|
||||||
produces:
|
produces:
|
||||||
- application/json
|
- application/json
|
||||||
responses:
|
responses:
|
||||||
|
@ -35,6 +35,24 @@ func (c DtoClient) ListArticles(ctx context.Context, limit int) ([]models.Articl
|
|||||||
return res, nil
|
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) {
|
func (c DtoClient) GetArticle(ctx context.Context, ID uuid.UUID) (models.ArticleDto, error) {
|
||||||
a, err := c.db.GetArticleByID(ctx, ID)
|
a, err := c.db.GetArticleByID(ctx, ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -1,8 +1,9 @@
|
|||||||
package routes
|
package routes
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
"github.com/go-chi/chi/v5"
|
"github.com/go-chi/chi/v5"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
@ -27,9 +28,20 @@ type ArticlesListResults struct {
|
|||||||
Payload []models.ArticleDto `json:"payload"`
|
Payload []models.ArticleDto `json:"payload"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ArticleGetResults struct {
|
||||||
|
ApiStatusModel
|
||||||
|
Payload models.ArticleDto `json:"payload"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type ArticleDetailsResult struct {
|
||||||
|
ApiStatusModel
|
||||||
|
Payload models.ArticleDetailsDto `json:"payload"`
|
||||||
|
}
|
||||||
|
|
||||||
// ListArticles
|
// ListArticles
|
||||||
// @Summary Lists the top 50 records
|
// @Summary Lists the top 50 records
|
||||||
// @Produce application/json
|
// @Produce application/json
|
||||||
|
// @Param page query string false "page number"
|
||||||
// @Tags Articles
|
// @Tags Articles
|
||||||
// @Router /articles [get]
|
// @Router /articles [get]
|
||||||
// @Success 200 {object} ArticlesListResults "OK"
|
// @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.ListArticles(r.Context(), 50)
|
res, err := s.dto.ListArticlesByPage(r.Context(), int32(page), 50)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.WriteError(w, err.Error(), http.StatusInternalServerError)
|
s.WriteError(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
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
|
||||||
|
s.WriteJson(w, p)
|
||||||
}
|
}
|
||||||
|
|
||||||
p.Payload = res
|
|
||||||
|
|
||||||
bres, err := json.Marshal(p)
|
|
||||||
if err != nil {
|
|
||||||
s.WriteError(w, err.Error(), http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
w.Write(bres)
|
|
||||||
}
|
|
||||||
|
|
||||||
type ArticleGetResults struct {
|
|
||||||
ApiStatusModel
|
|
||||||
Payload models.ArticleDto `json:"payload"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetArticle
|
// GetArticle
|
||||||
@ -72,15 +91,13 @@ type ArticleGetResults struct {
|
|||||||
// @Router /articles/{ID} [get]
|
// @Router /articles/{ID} [get]
|
||||||
// @Success 200 {object} ArticleGetResults "OK"
|
// @Success 200 {object} ArticleGetResults "OK"
|
||||||
func (s *Server) getArticle(w http.ResponseWriter, r *http.Request) {
|
func (s *Server) getArticle(w http.ResponseWriter, r *http.Request) {
|
||||||
p := ArticleGetResults {
|
p := ArticleGetResults{
|
||||||
ApiStatusModel: ApiStatusModel{
|
ApiStatusModel: ApiStatusModel{
|
||||||
Message: "OK",
|
Message: "OK",
|
||||||
StatusCode: http.StatusOK,
|
StatusCode: http.StatusOK,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
w.Header().Set(HeaderContentType, ApplicationJson)
|
|
||||||
|
|
||||||
id := chi.URLParam(r, "ID")
|
id := chi.URLParam(r, "ID")
|
||||||
uuid, err := uuid.Parse(id)
|
uuid, err := uuid.Parse(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -96,18 +113,7 @@ func (s *Server) getArticle(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
p.Payload = res
|
p.Payload = res
|
||||||
|
|
||||||
bres, err := json.Marshal(p)
|
s.WriteJson(w, p)
|
||||||
if err != nil {
|
|
||||||
s.WriteError(w, err.Error(), http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
w.Write(bres)
|
|
||||||
}
|
|
||||||
|
|
||||||
type ArticleDetailsResult struct {
|
|
||||||
ApiStatusModel
|
|
||||||
Payload models.ArticleDetailsDto `json:"payload"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetArticleDetails
|
// GetArticleDetails
|
||||||
@ -118,15 +124,13 @@ type ArticleDetailsResult struct {
|
|||||||
// @Router /articles/{ID}/details [get]
|
// @Router /articles/{ID}/details [get]
|
||||||
// @Success 200 {object} ArticleDetailsResult "OK"
|
// @Success 200 {object} ArticleDetailsResult "OK"
|
||||||
func (s *Server) getArticleDetails(w http.ResponseWriter, r *http.Request) {
|
func (s *Server) getArticleDetails(w http.ResponseWriter, r *http.Request) {
|
||||||
p := ArticleDetailsResult {
|
p := ArticleDetailsResult{
|
||||||
ApiStatusModel: ApiStatusModel{
|
ApiStatusModel: ApiStatusModel{
|
||||||
Message: "OK",
|
Message: "OK",
|
||||||
StatusCode: http.StatusOK,
|
StatusCode: http.StatusOK,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
w.Header().Set(HeaderContentType, ApplicationJson)
|
|
||||||
|
|
||||||
id := chi.URLParam(r, "ID")
|
id := chi.URLParam(r, "ID")
|
||||||
uuid, err := uuid.Parse(id)
|
uuid, err := uuid.Parse(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -142,18 +146,7 @@ func (s *Server) getArticleDetails(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
p.Payload = res
|
p.Payload = res
|
||||||
|
|
||||||
bres, err := json.Marshal(p)
|
s.WriteJson(w, p)
|
||||||
if err != nil {
|
|
||||||
s.WriteError(w, err.Error(), http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
w.Write(bres)
|
|
||||||
}
|
|
||||||
|
|
||||||
type ArticlesBySourceIDResults struct {
|
|
||||||
ApiStatusModel
|
|
||||||
Payload []models.ArticleDto `json:"payload"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO add page support
|
// TODO add page support
|
||||||
@ -165,7 +158,12 @@ type ArticlesBySourceIDResults struct {
|
|||||||
// @Router /articles/by/sourceid [get]
|
// @Router /articles/by/sourceid [get]
|
||||||
// @Success 200 {object} ArticlesListResults "OK"
|
// @Success 200 {object} ArticlesListResults "OK"
|
||||||
func (s *Server) GetArticlesBySourceId(w http.ResponseWriter, r *http.Request) {
|
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()
|
r.URL.Query()
|
||||||
query := 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)
|
s.WriteError(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
p.Payload = res
|
||||||
|
|
||||||
bres, err := json.Marshal(res)
|
s.WriteJson(w, p)
|
||||||
if err != nil {
|
|
||||||
s.WriteError(w, err.Error(), http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
w.Write(bres)
|
|
||||||
}
|
}
|
||||||
|
@ -12,6 +12,16 @@ import (
|
|||||||
"github.com/jtom38/newsbot/collector/domain/models"
|
"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 {
|
func (s Server) DiscordWebHookRouter() http.Handler {
|
||||||
r := chi.NewRouter()
|
r := chi.NewRouter()
|
||||||
|
|
||||||
@ -28,11 +38,6 @@ func (s Server) DiscordWebHookRouter() http.Handler {
|
|||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
type ListDiscordWebhooks struct {
|
|
||||||
ApiStatusModel
|
|
||||||
Payload []models.DiscordWebHooksDto `json:"payload"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// ListDiscordWebhooks
|
// ListDiscordWebhooks
|
||||||
// @Summary Returns the top 100 entries from the queue to be processed.
|
// @Summary Returns the top 100 entries from the queue to be processed.
|
||||||
// @Produce application/json
|
// @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)
|
res, err := s.dto.ListDiscordWebHooks(r.Context(), 50)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.WriteError(w, err.Error(), http.StatusInternalServerError)
|
s.WriteError(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
p.Payload = res
|
p.Payload = res
|
||||||
|
s.WriteJson(w, p)
|
||||||
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"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetDiscordWebHook
|
// 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")
|
_id := chi.URLParam(r, "ID")
|
||||||
if _id == "" {
|
if _id == "" {
|
||||||
s.WriteError(w, "id is missing", http.StatusBadRequest)
|
s.WriteError(w, "id is missing", http.StatusBadRequest)
|
||||||
@ -104,14 +93,7 @@ func (s *Server) GetDiscordWebHooksById(w http.ResponseWriter, r *http.Request)
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
p.Payload = res
|
p.Payload = res
|
||||||
|
s.WriteJson(w, p)
|
||||||
bres, err := json.Marshal(p)
|
|
||||||
if err != nil {
|
|
||||||
s.WriteError(w, "unable to convert to json", http.StatusBadRequest)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
w.Write(bres)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetDiscordWebHookByServerAndChannel
|
// GetDiscordWebHookByServerAndChannel
|
||||||
@ -130,8 +112,6 @@ func (s *Server) GetDiscordWebHooksByServerAndChannel(w http.ResponseWriter, r *
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
w.Header().Set(HeaderContentType, ApplicationJson)
|
|
||||||
|
|
||||||
query := r.URL.Query()
|
query := r.URL.Query()
|
||||||
_server := query["server"][0]
|
_server := query["server"][0]
|
||||||
if _server == "" {
|
if _server == "" {
|
||||||
@ -152,14 +132,7 @@ func (s *Server) GetDiscordWebHooksByServerAndChannel(w http.ResponseWriter, r *
|
|||||||
}
|
}
|
||||||
|
|
||||||
p.Payload = res
|
p.Payload = res
|
||||||
|
s.WriteJson(w, p)
|
||||||
bres, err := json.Marshal(p)
|
|
||||||
if err != nil {
|
|
||||||
s.WriteError(w, "unable to convert to json", http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
w.Write(bres)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewDiscordWebHook
|
// NewDiscordWebHook
|
||||||
|
@ -1,13 +1,17 @@
|
|||||||
package routes
|
package routes
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/go-chi/chi/v5"
|
"github.com/go-chi/chi/v5"
|
||||||
"github.com/jtom38/newsbot/collector/domain/models"
|
"github.com/jtom38/newsbot/collector/domain/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type ListDiscordWebHooksQueueResults struct {
|
||||||
|
ApiStatusModel
|
||||||
|
Payload []models.DiscordQueueDetailsDto `json:"payload"`
|
||||||
|
}
|
||||||
|
|
||||||
func (s *Server) GetQueueRouter() http.Handler {
|
func (s *Server) GetQueueRouter() http.Handler {
|
||||||
r := chi.NewRouter()
|
r := chi.NewRouter()
|
||||||
|
|
||||||
@ -16,11 +20,6 @@ func (s *Server) GetQueueRouter() http.Handler {
|
|||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
type ListDiscordWebHooksQueueResults struct {
|
|
||||||
ApiStatusModel
|
|
||||||
Payload []models.DiscordQueueDetailsDto `json:"payload"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetDiscordQueue
|
// GetDiscordQueue
|
||||||
// @Summary Returns the top 100 entries from the queue to be processed.
|
// @Summary Returns the top 100 entries from the queue to be processed.
|
||||||
// @Produce application/json
|
// @Produce application/json
|
||||||
@ -28,8 +27,6 @@ type ListDiscordWebHooksQueueResults struct {
|
|||||||
// @Router /queue/discord/webhooks [get]
|
// @Router /queue/discord/webhooks [get]
|
||||||
// @Success 200 {object} ListDiscordWebHooksQueueResults "ok"
|
// @Success 200 {object} ListDiscordWebHooksQueueResults "ok"
|
||||||
func (s *Server) ListDiscordWebhookQueue(w http.ResponseWriter, r *http.Request) {
|
func (s *Server) ListDiscordWebhookQueue(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Header().Set("Content-Type", "application/json")
|
|
||||||
|
|
||||||
p := ListDiscordWebHooksQueueResults{
|
p := ListDiscordWebHooksQueueResults{
|
||||||
ApiStatusModel: ApiStatusModel{
|
ApiStatusModel: ApiStatusModel{
|
||||||
Message: "OK",
|
Message: "OK",
|
||||||
@ -45,13 +42,5 @@ func (s *Server) ListDiscordWebhookQueue(w http.ResponseWriter, r *http.Request)
|
|||||||
}
|
}
|
||||||
|
|
||||||
p.Payload = res
|
p.Payload = res
|
||||||
|
s.WriteJson(w, p)
|
||||||
// convert to json
|
|
||||||
b, err := json.Marshal(p)
|
|
||||||
if err != nil {
|
|
||||||
s.WriteError(w, err.Error(), http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
w.Write(b)
|
|
||||||
}
|
}
|
||||||
|
@ -100,15 +100,27 @@ type ApiError struct {
|
|||||||
func (s *Server) WriteError(w http.ResponseWriter, errMessage string, HttpStatusCode int) {
|
func (s *Server) WriteError(w http.ResponseWriter, errMessage string, HttpStatusCode int) {
|
||||||
e := ApiError{
|
e := ApiError{
|
||||||
ApiStatusModel: &ApiStatusModel{
|
ApiStatusModel: &ApiStatusModel{
|
||||||
StatusCode: http.StatusInternalServerError,
|
StatusCode: HttpStatusCode,
|
||||||
Message: errMessage,
|
Message: errMessage,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
b, err := json.Marshal(e)
|
b, err := json.Marshal(e)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), HttpStatusCode)
|
||||||
}
|
}
|
||||||
|
|
||||||
w.Write(b)
|
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)
|
||||||
|
}
|
@ -61,8 +61,7 @@ func (s *Server) listSources(w http.ResponseWriter, r *http.Request) {
|
|||||||
res, err := s.Db.ListSources(*s.ctx, int32(topInt))
|
res, err := s.Db.ListSources(*s.ctx, int32(topInt))
|
||||||
*/
|
*/
|
||||||
|
|
||||||
w.Header().Set(HeaderContentType, ApplicationJson)
|
p := ListSources{
|
||||||
result := ListSources{
|
|
||||||
ApiStatusModel: ApiStatusModel{
|
ApiStatusModel: ApiStatusModel{
|
||||||
StatusCode: http.StatusOK,
|
StatusCode: http.StatusOK,
|
||||||
Message: "OK",
|
Message: "OK",
|
||||||
@ -76,15 +75,8 @@ func (s *Server) listSources(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
result.Payload = items
|
p.Payload = items
|
||||||
|
s.WriteJson(w, p)
|
||||||
bResult, err := json.Marshal(result)
|
|
||||||
if err != nil {
|
|
||||||
s.WriteError(w, err.Error(), http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
w.Write(bResult)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ListSourcesBySource
|
// ListSourcesBySource
|
||||||
@ -106,9 +98,8 @@ func (s *Server) listSourcesBySource(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
res, err := s.Db.ListSources(*s.ctx, int32(topInt))
|
res, err := s.Db.ListSources(*s.ctx, int32(topInt))
|
||||||
*/
|
*/
|
||||||
w.Header().Set(HeaderContentType, ApplicationJson)
|
|
||||||
|
|
||||||
result := ListSources{
|
p := ListSources{
|
||||||
ApiStatusModel: ApiStatusModel{
|
ApiStatusModel: ApiStatusModel{
|
||||||
StatusCode: http.StatusOK,
|
StatusCode: http.StatusOK,
|
||||||
Message: "OK",
|
Message: "OK",
|
||||||
@ -116,24 +107,16 @@ func (s *Server) listSourcesBySource(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
query := r.URL.Query()
|
query := r.URL.Query()
|
||||||
_source := query["source"][0]
|
|
||||||
|
|
||||||
// Shows the list by Sources.source
|
// 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 {
|
if err != nil {
|
||||||
s.WriteError(w, err.Error(), http.StatusBadRequest)
|
s.WriteError(w, err.Error(), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
result.Payload = res
|
p.Payload = res
|
||||||
|
s.WriteJson(w, p)
|
||||||
bResult, err := json.Marshal(result)
|
|
||||||
if err != nil {
|
|
||||||
s.WriteError(w, err.Error(), http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
w.Write(bResult)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetSource
|
// GetSource
|
||||||
@ -154,10 +137,7 @@ func (s *Server) getSources(w http.ResponseWriter, r *http.Request) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
w.Header().Set(HeaderContentType, ApplicationJson)
|
uuid, err := uuid.Parse(chi.URLParam(r, "ID"))
|
||||||
|
|
||||||
id := chi.URLParam(r, "ID")
|
|
||||||
uuid, err := uuid.Parse(id)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.WriteError(w, err.Error(), http.StatusBadRequest)
|
s.WriteError(w, err.Error(), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
@ -170,14 +150,7 @@ func (s *Server) getSources(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
payload.Payload = res
|
payload.Payload = res
|
||||||
|
s.WriteJson(w, payload)
|
||||||
bResult, err := json.Marshal(payload)
|
|
||||||
if err != nil {
|
|
||||||
s.WriteError(w, err.Error(), http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
w.Write(bResult)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetSourceByNameAndSource
|
// GetSourceByNameAndSource
|
||||||
@ -219,15 +192,7 @@ func (s *Server) GetSourceBySourceAndName(w http.ResponseWriter, r *http.Request
|
|||||||
}
|
}
|
||||||
|
|
||||||
p.Payload = item
|
p.Payload = item
|
||||||
|
s.WriteJson(w, p)
|
||||||
bResult, err := json.Marshal(item)
|
|
||||||
if err != nil {
|
|
||||||
s.WriteError(w, err.Error(), http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
w.Header().Set(HeaderContentType, ApplicationJson)
|
|
||||||
w.Write(bResult)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewRedditSource
|
// NewRedditSource
|
||||||
@ -242,8 +207,6 @@ func (s *Server) newRedditSource(w http.ResponseWriter, r *http.Request) {
|
|||||||
_url := query["url"][0]
|
_url := query["url"][0]
|
||||||
//_tags := query["tags"][0]
|
//_tags := query["tags"][0]
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
|
||||||
|
|
||||||
if _url == "" {
|
if _url == "" {
|
||||||
s.WriteError(w, "url is missing a value", http.StatusBadRequest)
|
s.WriteError(w, "url is missing a value", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
@ -277,6 +240,7 @@ func (s *Server) newRedditSource(w http.ResponseWriter, r *http.Request) {
|
|||||||
s.WriteError(w, err.Error(), http.StatusInternalServerError)
|
s.WriteError(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
//s.WriteJson(w, ¶ms)
|
||||||
|
|
||||||
bJson, err := json.Marshal(¶ms)
|
bJson, err := json.Marshal(¶ms)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -34,6 +34,11 @@ type GetSubscription struct {
|
|||||||
Payload models.SubscriptionDto `json:"payload"`
|
Payload models.SubscriptionDto `json:"payload"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ListSubscriptionDetails struct {
|
||||||
|
ApiStatusModel
|
||||||
|
Payload []models.SubscriptionDetailsDto `json:"payload"`
|
||||||
|
}
|
||||||
|
|
||||||
// GetSubscriptions
|
// GetSubscriptions
|
||||||
// @Summary Returns the top 100 entries from the queue to be processed.
|
// @Summary Returns the top 100 entries from the queue to be processed.
|
||||||
// @Produce application/json
|
// @Produce application/json
|
||||||
@ -43,8 +48,6 @@ type GetSubscription struct {
|
|||||||
// @Failure 400 {object} ApiError "Unable to reach SQL."
|
// @Failure 400 {object} ApiError "Unable to reach SQL."
|
||||||
// @Failure 500 {object} 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")
|
|
||||||
|
|
||||||
payload := ListSubscriptions{
|
payload := ListSubscriptions{
|
||||||
ApiStatusModel: ApiStatusModel{
|
ApiStatusModel: ApiStatusModel{
|
||||||
StatusCode: http.StatusOK,
|
StatusCode: http.StatusOK,
|
||||||
@ -59,19 +62,7 @@ func (s *Server) ListSubscriptions(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
payload.Payload = res
|
payload.Payload = res
|
||||||
|
s.WriteJson(w, payload)
|
||||||
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"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ListSubscriptionDetails
|
// ListSubscriptionDetails
|
||||||
@ -80,8 +71,7 @@ type ListSubscriptionDetails struct {
|
|||||||
// @Tags Subscription
|
// @Tags Subscription
|
||||||
// @Router /subscriptions/details [get]
|
// @Router /subscriptions/details [get]
|
||||||
// @Success 200 {object} ListSubscriptionDetails "ok"
|
// @Success 200 {object} ListSubscriptionDetails "ok"
|
||||||
func (s Server) ListSubscriptionDetails(w http.ResponseWriter, r *http.Request) {
|
func (s *Server) ListSubscriptionDetails(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Header().Set(HeaderContentType, ApplicationJson)
|
|
||||||
payload := ListSubscriptionDetails{
|
payload := ListSubscriptionDetails{
|
||||||
ApiStatusModel: ApiStatusModel{
|
ApiStatusModel: ApiStatusModel{
|
||||||
StatusCode: http.StatusOK,
|
StatusCode: http.StatusOK,
|
||||||
@ -96,13 +86,7 @@ func (s Server) ListSubscriptionDetails(w http.ResponseWriter, r *http.Request)
|
|||||||
}
|
}
|
||||||
|
|
||||||
payload.Payload = res
|
payload.Payload = res
|
||||||
|
s.WriteJson(w, payload)
|
||||||
b, err := json.Marshal(payload)
|
|
||||||
if err != nil {
|
|
||||||
s.WriteError(w, err.Error(), http.StatusInternalServerError)
|
|
||||||
}
|
|
||||||
|
|
||||||
w.Write(b)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetSubscriptionsByDiscordId
|
// 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 400 {object} ApiError "Unable to reach SQL or Data problems"
|
||||||
// @Failure 500 {object} 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(HeaderContentType, ApplicationJson)
|
|
||||||
|
|
||||||
p := ListSubscriptions{
|
p := ListSubscriptions{
|
||||||
ApiStatusModel: ApiStatusModel{
|
ApiStatusModel: ApiStatusModel{
|
||||||
StatusCode: http.StatusOK,
|
StatusCode: http.StatusOK,
|
||||||
@ -125,13 +107,12 @@ func (s *Server) GetSubscriptionsByDiscordId(w http.ResponseWriter, r *http.Requ
|
|||||||
}
|
}
|
||||||
|
|
||||||
query := r.URL.Query()
|
query := r.URL.Query()
|
||||||
_id := query["id"][0]
|
if query["id"][0] == "" {
|
||||||
if _id == "" {
|
|
||||||
s.WriteError(w, ErrIdValueMissing, http.StatusBadRequest)
|
s.WriteError(w, ErrIdValueMissing, http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
uuid, err := uuid.Parse(_id)
|
uuid, err := uuid.Parse(query["id"][0])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.WriteError(w, ErrValueNotUuid, http.StatusBadRequest)
|
s.WriteError(w, ErrValueNotUuid, http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
@ -144,14 +125,7 @@ func (s *Server) GetSubscriptionsByDiscordId(w http.ResponseWriter, r *http.Requ
|
|||||||
}
|
}
|
||||||
|
|
||||||
p.Payload = res
|
p.Payload = res
|
||||||
|
s.WriteJson(w, p)
|
||||||
bres, err := json.Marshal(p)
|
|
||||||
if err != nil {
|
|
||||||
s.WriteError(w, err.Error(), http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
w.Write(bres)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetSubscriptionsBySourceId
|
// GetSubscriptionsBySourceId
|
||||||
@ -162,8 +136,6 @@ func (s *Server) GetSubscriptionsByDiscordId(w http.ResponseWriter, r *http.Requ
|
|||||||
// @Router /subscriptions/by/SourceId [get]
|
// @Router /subscriptions/by/SourceId [get]
|
||||||
// @Success 200 {object} ListSubscriptions "ok"
|
// @Success 200 {object} ListSubscriptions "ok"
|
||||||
func (s *Server) GetSubscriptionsBySourceId(w http.ResponseWriter, r *http.Request) {
|
func (s *Server) GetSubscriptionsBySourceId(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Header().Set("Content-Type", "application/json")
|
|
||||||
|
|
||||||
p := ListSubscriptions{
|
p := ListSubscriptions{
|
||||||
ApiStatusModel: ApiStatusModel{
|
ApiStatusModel: ApiStatusModel{
|
||||||
StatusCode: http.StatusOK,
|
StatusCode: http.StatusOK,
|
||||||
@ -191,14 +163,7 @@ func (s *Server) GetSubscriptionsBySourceId(w http.ResponseWriter, r *http.Reque
|
|||||||
}
|
}
|
||||||
|
|
||||||
p.Payload = res
|
p.Payload = res
|
||||||
|
s.WriteJson(w, p)
|
||||||
bres, err := json.Marshal(p)
|
|
||||||
if err != nil {
|
|
||||||
s.WriteError(w, err.Error(), http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
w.Write(bres)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewDiscordWebHookSubscription
|
// NewDiscordWebHookSubscription
|
||||||
@ -208,8 +173,6 @@ func (s *Server) GetSubscriptionsBySourceId(w http.ResponseWriter, r *http.Reque
|
|||||||
// @Tags Subscription
|
// @Tags Subscription
|
||||||
// @Router /subscriptions/discord/webhook/new [post]
|
// @Router /subscriptions/discord/webhook/new [post]
|
||||||
func (s *Server) newDiscordWebHookSubscription(w http.ResponseWriter, r *http.Request) {
|
func (s *Server) newDiscordWebHookSubscription(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Header().Set("Content-Type", "application/json")
|
|
||||||
|
|
||||||
// Extract the values given
|
// Extract the values given
|
||||||
query := r.URL.Query()
|
query := r.URL.Query()
|
||||||
discordWebHookId := query["discordWebHookId"][0]
|
discordWebHookId := query["discordWebHookId"][0]
|
||||||
|
Loading…
Reference in New Issue
Block a user