Add /entries

This commit is contained in:
2024-05-04 19:19:25 +02:00
parent 31077f9fb5
commit b1b56cfd5a
4 changed files with 88 additions and 6 deletions
+17
View File
@@ -0,0 +1,17 @@
package main
import (
"github.com/labstack/echo/v4"
)
func (h *Handler) GetEntries(c echo.Context) error {
ret, err := h.entries.ListEntries()
if err != nil {
return err
}
return c.JSON(200, ret)
}
func (h *Handler) RegisterEntriesRoutes(e *echo.Echo, r *echo.Group) {
e.GET("/entries", h.GetEntries)
}
+3 -5
View File
@@ -17,15 +17,11 @@ import (
type Handler struct {
feeds vex.FeedService
entries vex.EntryService
users vex.UserService
jwtSecret []byte
}
func (h *Handler) GetEntries(c echo.Context) error {
ret := make([]interface{}, 0)
return c.JSON(200, ret)
}
type Validator struct {
validator *validator.Validate
}
@@ -53,6 +49,7 @@ func main() {
}
h := Handler{
feeds: vex.NewFeedService(db),
entries: vex.NewEntryService(db),
users: vex.NewUserService(db),
jwtSecret: []byte(os.Getenv("JWT_SECRET")),
}
@@ -68,6 +65,7 @@ func main() {
e.GET("/entries", h.GetEntries)
h.RegisterLoginRoutes(e, r)
h.RegisterEntriesRoutes(e, r)
h.RegisterFeedsRoutes(e, r)
e.Start(":1597")
+67
View File
@@ -0,0 +1,67 @@
package vex
import (
"time"
"github.com/google/uuid"
"github.com/jmoiron/sqlx"
)
type Entry struct {
Id uuid.UUID `json:"id"`
Title string `json:"title"`
Link string `json:"link"`
Date time.Time `json:"date"`
Content string `json:"content"`
Author *string `json:"author"`
FeedId uuid.UUID `json:"feedId"`
Feed Feed `json:"feed,omitempty"`
}
type EntryDao struct {
Id uuid.UUID
Title string
Link string
Date time.Time
Content string
Author *string
FeedId uuid.UUID `db:"feed_id"`
Feed FeedDao `db:"feed"`
}
func (e *EntryDao) ToEntry() Entry {
return Entry{
Id: e.Id,
Title: e.Title,
Link: e.Link,
Date: e.Date,
Content: e.Content,
Author: e.Author,
FeedId: e.FeedId,
Feed: e.Feed.ToFeed(),
}
}
type EntryService struct {
database *sqlx.DB
}
func NewEntryService(db *sqlx.DB) EntryService {
return EntryService{database: db}
}
func (s EntryService) ListEntries() ([]Entry, error) {
ret := []EntryDao{}
err := s.database.Select(
&ret,
`select e.*, f.id as "feed.id", f.name as "feed.name", f.link as "feed.link", f.favicon_url as "feed.favicon_url",
f.tags as "feed.tags", f.submitter_id as "feed.submitter_id", f.added_date as "feed.added_date"
from entries as e
left join feeds as f on f.id = e.feed_id
order by e.date`,
)
if err != nil {
return nil, err
}
return Map(ret, func(e EntryDao, _ int) Entry { return e.ToEntry() }), nil
}
+1 -1
View File
@@ -12,7 +12,7 @@ create table if not exists feeds(
favicon_url text not null,
tags text[] not null,
submitter_id uuid not null references users(id),
added_date timestamp with time zone not null
added_date timestamp with time zone not null,
);
create table if not exists entries(