feat: added new commands
- added follow command - added unfollow command - following command - introduced logged in user middleware
This commit is contained in:
@@ -88,3 +88,22 @@ func (q *Queries) GetAllFeeds(ctx context.Context) ([]Feed, error) {
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const getFeedByUrl = `-- name: GetFeedByUrl :one
|
||||
SELECT id, created_at, updated_at, name, url, user_id FROM feeds
|
||||
WHERE url = $1
|
||||
`
|
||||
|
||||
func (q *Queries) GetFeedByUrl(ctx context.Context, url string) (Feed, error) {
|
||||
row := q.db.QueryRowContext(ctx, getFeedByUrl, url)
|
||||
var i Feed
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.Name,
|
||||
&i.Url,
|
||||
&i.UserID,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
@@ -0,0 +1,135 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.31.1
|
||||
// source: follows.sql
|
||||
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
const createFollow = `-- name: CreateFollow :one
|
||||
WITH inserted_follow as (
|
||||
INSERT INTO follows (id, created_at, updated_at, feed_id, user_id)
|
||||
VALUES (
|
||||
$1,
|
||||
$2,
|
||||
$3,
|
||||
$4,
|
||||
$5
|
||||
)
|
||||
RETURNING id, created_at, updated_at, user_id, feed_id
|
||||
)
|
||||
SELECT inserted_follow.id, inserted_follow.created_at, inserted_follow.updated_at, inserted_follow.user_id, inserted_follow.feed_id,
|
||||
feeds.name as feed_name,
|
||||
users.name as user_name
|
||||
FROM inserted_follow
|
||||
INNER JOIN users
|
||||
ON inserted_follow.user_id = users.id
|
||||
INNER JOIN feeds
|
||||
ON inserted_follow.feed_id = feeds.id
|
||||
`
|
||||
|
||||
type CreateFollowParams struct {
|
||||
ID uuid.UUID
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
FeedID uuid.UUID
|
||||
UserID uuid.UUID
|
||||
}
|
||||
|
||||
type CreateFollowRow struct {
|
||||
ID uuid.UUID
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
UserID uuid.UUID
|
||||
FeedID uuid.UUID
|
||||
FeedName string
|
||||
UserName string
|
||||
}
|
||||
|
||||
func (q *Queries) CreateFollow(ctx context.Context, arg CreateFollowParams) (CreateFollowRow, error) {
|
||||
row := q.db.QueryRowContext(ctx, createFollow,
|
||||
arg.ID,
|
||||
arg.CreatedAt,
|
||||
arg.UpdatedAt,
|
||||
arg.FeedID,
|
||||
arg.UserID,
|
||||
)
|
||||
var i CreateFollowRow
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.UserID,
|
||||
&i.FeedID,
|
||||
&i.FeedName,
|
||||
&i.UserName,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const deleteFollowForUser = `-- name: DeleteFollowForUser :exec
|
||||
DELETE FROM follows
|
||||
WHERE feed_id = $1 AND user_id = $2
|
||||
`
|
||||
|
||||
type DeleteFollowForUserParams struct {
|
||||
FeedID uuid.UUID
|
||||
UserID uuid.UUID
|
||||
}
|
||||
|
||||
func (q *Queries) DeleteFollowForUser(ctx context.Context, arg DeleteFollowForUserParams) error {
|
||||
_, err := q.db.ExecContext(ctx, deleteFollowForUser, arg.FeedID, arg.UserID)
|
||||
return err
|
||||
}
|
||||
|
||||
const getFollowsForUser = `-- name: GetFollowsForUser :many
|
||||
SELECT follows.id, follows.created_at, follows.updated_at, follows.user_id, follows.feed_id, feeds.name as feed_name FROM follows
|
||||
INNER JOIN feeds
|
||||
ON follows.feed_id = feeds.id
|
||||
WHERE follows.user_id = $1
|
||||
`
|
||||
|
||||
type GetFollowsForUserRow struct {
|
||||
ID uuid.UUID
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
UserID uuid.UUID
|
||||
FeedID uuid.UUID
|
||||
FeedName string
|
||||
}
|
||||
|
||||
func (q *Queries) GetFollowsForUser(ctx context.Context, userID uuid.UUID) ([]GetFollowsForUserRow, error) {
|
||||
rows, err := q.db.QueryContext(ctx, getFollowsForUser, userID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []GetFollowsForUserRow
|
||||
for rows.Next() {
|
||||
var i GetFollowsForUserRow
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.UserID,
|
||||
&i.FeedID,
|
||||
&i.FeedName,
|
||||
); 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
|
||||
}
|
||||
@@ -19,6 +19,14 @@ type Feed struct {
|
||||
UserID uuid.UUID
|
||||
}
|
||||
|
||||
type Follow struct {
|
||||
ID uuid.UUID
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
UserID uuid.UUID
|
||||
FeedID uuid.UUID
|
||||
}
|
||||
|
||||
type User struct {
|
||||
ID uuid.UUID
|
||||
CreatedAt time.Time
|
||||
|
||||
Reference in New Issue
Block a user