Create AddFeed dto, add validation and sqlx

This commit is contained in:
2024-05-04 13:24:08 +02:00
parent 32acf07d9a
commit e127360ecf
9 changed files with 136 additions and 42 deletions
+45
View File
@@ -0,0 +1,45 @@
package vex
import (
"github.com/google/uuid"
"github.com/jmoiron/sqlx"
"github.com/lib/pq"
)
type Feed struct {
Id uuid.UUID
Name string
Link string
FaviconUrl string `db:"favicon_url"`
Tags pq.StringArray
SubmitterId uuid.UUID `db:"submitter_id"`
}
type FeedService struct {
database *sqlx.DB
}
func NewFeedService(db *sqlx.DB) FeedService {
return FeedService{database: db}
}
func (s FeedService) AddFeed(link string, tags []string, submitter uuid.UUID) (Feed, error) {
feed := Feed{
Id: uuid.New(),
Name: link,
Link: link,
FaviconUrl: link,
Tags: tags,
SubmitterId: submitter,
}
_, err := s.database.NamedExec(
`insert into feeds (id, name, link, favicon_url, tags, submitter_id)
values (:id, :name, :link, :favicon_url, :tags, :submitter_id)`,
feed,
)
if err != nil {
return Feed{}, err
}
return feed, nil
}