#7 - saving feed, but without content
This commit is contained in:
56
internal/database/createFeed.sql.go
Normal file
56
internal/database/createFeed.sql.go
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
// Code generated by sqlc. DO NOT EDIT.
|
||||||
|
// versions:
|
||||||
|
// sqlc v1.28.0
|
||||||
|
// source: createFeed.sql
|
||||||
|
|
||||||
|
package database
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
|
)
|
||||||
|
|
||||||
|
const createFeed = `-- name: CreateFeed :one
|
||||||
|
INSERT INTO feeds (id, created_at, updated_at, user_id, url, name)
|
||||||
|
VALUES (
|
||||||
|
$1,
|
||||||
|
$2,
|
||||||
|
$3,
|
||||||
|
$4,
|
||||||
|
$5,
|
||||||
|
$6
|
||||||
|
)
|
||||||
|
RETURNING id, created_at, updated_at, name, url, user_id
|
||||||
|
`
|
||||||
|
|
||||||
|
type CreateFeedParams struct {
|
||||||
|
ID uuid.UUID
|
||||||
|
CreatedAt time.Time
|
||||||
|
UpdatedAt time.Time
|
||||||
|
UserID uuid.UUID
|
||||||
|
Url string
|
||||||
|
Name string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queries) CreateFeed(ctx context.Context, arg CreateFeedParams) (Feed, error) {
|
||||||
|
row := q.db.QueryRowContext(ctx, createFeed,
|
||||||
|
arg.ID,
|
||||||
|
arg.CreatedAt,
|
||||||
|
arg.UpdatedAt,
|
||||||
|
arg.UserID,
|
||||||
|
arg.Url,
|
||||||
|
arg.Name,
|
||||||
|
)
|
||||||
|
var i Feed
|
||||||
|
err := row.Scan(
|
||||||
|
&i.ID,
|
||||||
|
&i.CreatedAt,
|
||||||
|
&i.UpdatedAt,
|
||||||
|
&i.Name,
|
||||||
|
&i.Url,
|
||||||
|
&i.UserID,
|
||||||
|
)
|
||||||
|
return i, err
|
||||||
|
}
|
||||||
@@ -10,6 +10,15 @@ import (
|
|||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type Feed struct {
|
||||||
|
ID uuid.UUID
|
||||||
|
CreatedAt time.Time
|
||||||
|
UpdatedAt time.Time
|
||||||
|
Name string
|
||||||
|
Url string
|
||||||
|
UserID uuid.UUID
|
||||||
|
}
|
||||||
|
|
||||||
type User struct {
|
type User struct {
|
||||||
ID uuid.UUID
|
ID uuid.UUID
|
||||||
CreatedAt time.Time
|
CreatedAt time.Time
|
||||||
|
|||||||
51
internal/handlers/addFeedHandler.go
Normal file
51
internal/handlers/addFeedHandler.go
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
package handlers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"github.com/google/uuid"
|
||||||
|
"github.com/rdarius/boot-dev-blog-aggregator/internal/config"
|
||||||
|
"github.com/rdarius/boot-dev-blog-aggregator/internal/database"
|
||||||
|
"github.com/rdarius/boot-dev-blog-aggregator/internal/rss"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func AddFeedHandler(s *config.State, cmd config.Command) error {
|
||||||
|
|
||||||
|
if len(cmd.Args) < 2 {
|
||||||
|
log.Fatal("usage: boot-dev-blog-aggregator addfeed NAME URL")
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
user, err := s.DB.GetUser(ctx, s.Config.CurrentUserName)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
name := cmd.Args[0]
|
||||||
|
url := cmd.Args[1]
|
||||||
|
|
||||||
|
_, err = rss.FetchFeed(ctx, url)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Failed to fetch feed: %v\n", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
f, err := s.DB.CreateFeed(ctx, database.CreateFeedParams{
|
||||||
|
ID: uuid.UUID{},
|
||||||
|
CreatedAt: time.Time{},
|
||||||
|
UpdatedAt: time.Time{},
|
||||||
|
UserID: user.ID,
|
||||||
|
Url: url,
|
||||||
|
Name: name,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("%v", f)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
1
main.go
1
main.go
@@ -36,6 +36,7 @@ func main() {
|
|||||||
commands.Register("reset", handlers.ResetUsersHandler)
|
commands.Register("reset", handlers.ResetUsersHandler)
|
||||||
commands.Register("users", handlers.GetUsersHandler)
|
commands.Register("users", handlers.GetUsersHandler)
|
||||||
commands.Register("agg", handlers.FetchFeedHandler)
|
commands.Register("agg", handlers.FetchFeedHandler)
|
||||||
|
commands.Register("addfeed", handlers.AddFeedHandler)
|
||||||
|
|
||||||
if len(os.Args) < 2 {
|
if len(os.Args) < 2 {
|
||||||
log.Fatal("usage: boot-dev-blog-aggregator <command> [args...]")
|
log.Fatal("usage: boot-dev-blog-aggregator <command> [args...]")
|
||||||
|
|||||||
11
sql/queries/createFeed.sql
Normal file
11
sql/queries/createFeed.sql
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
-- name: CreateFeed :one
|
||||||
|
INSERT INTO feeds (id, created_at, updated_at, user_id, url, name)
|
||||||
|
VALUES (
|
||||||
|
$1,
|
||||||
|
$2,
|
||||||
|
$3,
|
||||||
|
$4,
|
||||||
|
$5,
|
||||||
|
$6
|
||||||
|
)
|
||||||
|
RETURNING *;
|
||||||
12
sql/schema/0002_feed.sql
Normal file
12
sql/schema/0002_feed.sql
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
-- +goose Up
|
||||||
|
CREATE TABLE feeds(
|
||||||
|
id UUID PRIMARY KEY,
|
||||||
|
created_at TIMESTAMP not null DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
updated_at TIMESTAMP not null,
|
||||||
|
name VARCHAR(128) not null,
|
||||||
|
url VARCHAR(512) not null UNIQUE,
|
||||||
|
user_id UUID not null REFERENCES users(id) ON DELETE CASCADE
|
||||||
|
);
|
||||||
|
|
||||||
|
-- +goose Down
|
||||||
|
DROP TABLE feeds;
|
||||||
Reference in New Issue
Block a user