mirror of
https://github.com/zoriya/vex.git
synced 2025-12-06 07:06:09 +00:00
Create AddFeed dto, add validation and sqlx
This commit is contained in:
@@ -3,4 +3,4 @@ RUN go install github.com/bokwoon95/wgo@latest
|
||||
WORKDIR /app
|
||||
|
||||
EXPOSE 1597
|
||||
CMD wgo run .
|
||||
CMD wgo run ./cmd
|
||||
|
||||
@@ -1,21 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/zoriya/vex"
|
||||
)
|
||||
|
||||
func (h *Handler) AddFeed(c echo.Context) error {
|
||||
var feed vex.Feed
|
||||
err := c.Bind(&feed)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ret := make([]interface{}, 0)
|
||||
return c.JSON(201, ret)
|
||||
}
|
||||
|
||||
func (h *Handler) RegisterFeedsRoutes(e *echo.Echo) {
|
||||
e.POST("/feed", h.AddFeed)
|
||||
}
|
||||
40
api/cmd/feeds.go
Normal file
40
api/cmd/feeds.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
|
||||
type AddFeedDto struct {
|
||||
Link string `json:"link" validate:"required,url"`
|
||||
Tags []string `json:"tags" validate:"required"`
|
||||
}
|
||||
|
||||
func (h *Handler) AddFeed(c echo.Context) error {
|
||||
user := uuid.New()
|
||||
|
||||
var req AddFeedDto
|
||||
err := c.Bind(&req)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
|
||||
}
|
||||
if err = c.Validate(&req); err != nil {
|
||||
return err
|
||||
}
|
||||
log.Printf("%v", req)
|
||||
|
||||
feed, err := h.feeds.AddFeed(req.Link, req.Tags, user)
|
||||
if err != nil {
|
||||
log.Printf("Add feed error: %v", err)
|
||||
return echo.NewHTTPError(500, "internal server error")
|
||||
}
|
||||
return c.JSON(201, feed)
|
||||
}
|
||||
|
||||
func (h *Handler) RegisterFeedsRoutes(e *echo.Echo) {
|
||||
e.POST("/feeds", h.AddFeed)
|
||||
}
|
||||
@@ -1,18 +1,21 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/jmoiron/sqlx"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/labstack/echo/v4/middleware"
|
||||
_ "github.com/lib/pq"
|
||||
"github.com/zoriya/vex"
|
||||
)
|
||||
|
||||
type Handler struct {
|
||||
database *sql.DB
|
||||
feeds vex.FeedService
|
||||
}
|
||||
|
||||
func (h *Handler) GetEntries(c echo.Context) error {
|
||||
@@ -20,6 +23,18 @@ func (h *Handler) GetEntries(c echo.Context) error {
|
||||
return c.JSON(200, ret)
|
||||
}
|
||||
|
||||
type Validator struct {
|
||||
validator *validator.Validate
|
||||
}
|
||||
|
||||
func (cv *Validator) Validate(i interface{}) error {
|
||||
if err := cv.validator.Struct(i); err != nil {
|
||||
// Optionally, you could return the error to give each route more control over the status code
|
||||
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
con := fmt.Sprintf(
|
||||
"postgresql://%v:%v@%v:%v/%v?sslmode=disable",
|
||||
@@ -29,15 +44,16 @@ func main() {
|
||||
os.Getenv("POSTGRES_PORT"),
|
||||
os.Getenv("POSTGRES_DB"),
|
||||
)
|
||||
db, err := sql.Open("postgres", con)
|
||||
db, err := sqlx.Open("postgres", con)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
h := Handler{
|
||||
database: db,
|
||||
feeds: vex.NewFeedService(db),
|
||||
}
|
||||
|
||||
e := echo.New()
|
||||
e.Validator = &Validator{validator: validator.New()}
|
||||
e.Use(middleware.Logger())
|
||||
e.GET("/entries", h.GetEntries)
|
||||
h.RegisterFeedsRoutes(e)
|
||||
|
||||
12
api/feed.go
12
api/feed.go
@@ -1,12 +0,0 @@
|
||||
package vex
|
||||
|
||||
import "github.com/google/uuid"
|
||||
|
||||
type Feed struct {
|
||||
Id uuid.UUID `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Link string `json:"link"`
|
||||
FaviconUrl string `json:"faviconUrl"`
|
||||
Tags []string `json:"tags"`
|
||||
SubmitterId uuid.UUID `json:"submitterId"`
|
||||
}
|
||||
45
api/feeds.go
Normal file
45
api/feeds.go
Normal 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
|
||||
}
|
||||
@@ -3,14 +3,20 @@ module github.com/zoriya/vex
|
||||
go 1.22.1
|
||||
|
||||
require (
|
||||
github.com/go-playground/validator/v10 v10.20.0
|
||||
github.com/google/uuid v1.6.0
|
||||
github.com/jmoiron/sqlx v1.4.0
|
||||
github.com/labstack/echo/v4 v4.12.0
|
||||
github.com/lib/pq v1.10.9
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
|
||||
github.com/go-playground/locales v0.14.1 // indirect
|
||||
github.com/go-playground/universal-translator v0.18.1 // indirect
|
||||
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
|
||||
github.com/labstack/gommon v0.4.2 // indirect
|
||||
github.com/leodido/go-urn v1.4.0 // indirect
|
||||
github.com/mattn/go-colorable v0.1.13 // indirect
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
github.com/valyala/bytebufferpool v1.0.0 // indirect
|
||||
|
||||
20
api/go.sum
20
api/go.sum
@@ -1,13 +1,31 @@
|
||||
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
|
||||
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0=
|
||||
github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk=
|
||||
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
|
||||
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
|
||||
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
|
||||
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
|
||||
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
|
||||
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
|
||||
github.com/go-playground/validator/v10 v10.20.0 h1:K9ISHbSaI0lyB2eWMPJo+kOS/FBExVwjEviJTixqxL8=
|
||||
github.com/go-playground/validator/v10 v10.20.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
|
||||
github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y=
|
||||
github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg=
|
||||
github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY=
|
||||
github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I=
|
||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/jmoiron/sqlx v1.4.0 h1:1PLqN7S1UYp5t4SrVVnt4nUVNemrDAtxlulVe+Qgm3o=
|
||||
github.com/jmoiron/sqlx v1.4.0/go.mod h1:ZrZ7UsYB/weZdl2Bxg6jCRO9c3YHl8r3ahlKmRT4JLY=
|
||||
github.com/labstack/echo/v4 v4.12.0 h1:IKpw49IMryVB2p1a4dzwlhP1O2Tf2E0Ir/450lH+kI0=
|
||||
github.com/labstack/echo/v4 v4.12.0/go.mod h1:UP9Cr2DJXbOK3Kr9ONYzNowSh7HP0aG0ShAyycHSJvM=
|
||||
github.com/labstack/gommon v0.4.2 h1:F8qTUNXgG1+6WQmqoUWnz8WiEU60mXVVw0P4ht1WRA0=
|
||||
github.com/labstack/gommon v0.4.2/go.mod h1:QlUFxVM+SNXhDL/Z7YhocGIBYOiwB0mXm1+1bAPHPyU=
|
||||
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
|
||||
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
|
||||
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
|
||||
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
|
||||
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
|
||||
@@ -15,6 +33,8 @@ github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovk
|
||||
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
|
||||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||
github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
|
||||
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
|
||||
|
||||
@@ -2,13 +2,13 @@ create table if not exists users(
|
||||
id uuid not null primary key,
|
||||
name text not null,
|
||||
password varchar(100) not null,
|
||||
email text not null
|
||||
email text not null unique
|
||||
);
|
||||
|
||||
create table if not exists feeds(
|
||||
id uuid not null primary key,
|
||||
name text not null,
|
||||
link text not null,
|
||||
link text not null unique,
|
||||
favicon_url text not null,
|
||||
tags text[] not null,
|
||||
submitter_id uuid not null references users(id)
|
||||
@@ -16,7 +16,7 @@ create table if not exists feeds(
|
||||
|
||||
create table if not exists entries(
|
||||
id uuid not null primary key,
|
||||
feed_id uuid not null references feed(id),
|
||||
feed_id uuid not null references feeds(id),
|
||||
title text not null,
|
||||
link text not null,
|
||||
date timestamp with time zone not null,
|
||||
@@ -31,6 +31,6 @@ create table if not exists entries_users(
|
||||
is_bookmarked bool not null,
|
||||
is_read_later bool not null,
|
||||
is_ignored bool not null,
|
||||
constraint entries_users_pk primary keys(user_id, feed_id)
|
||||
constraint entries_users_pk primary key(user_id, feed_id)
|
||||
);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user