Activity Pub Implementation #58
|
@ -51,3 +51,9 @@ type InteractionRepository interface {
|
|||
// ListAllInteractions lists all interactions, sorted by creation date (descending)
|
||||
ListAllInteractions() ([]model.Interaction, error)
|
||||
}
|
||||
|
||||
type FollowerRepository interface {
|
||||
Add(follower string) error
|
||||
Remove(follower string) error
|
||||
All() ([]string, error)
|
||||
}
|
||||
|
|
|
@ -0,0 +1,57 @@
|
|||
package infra
|
||||
|
||||
import (
|
||||
"owl-blogs/app/repository"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
)
|
||||
|
||||
type sqlFollower struct {
|
||||
Follwer string `db:"follower"`
|
||||
}
|
||||
|
||||
type DefaultFollowerRepo struct {
|
||||
db *sqlx.DB
|
||||
}
|
||||
|
||||
func NewFollowerRepository(db Database) repository.FollowerRepository {
|
||||
sqlxdb := db.Get()
|
||||
|
||||
// Create tables if not exists
|
||||
sqlxdb.MustExec(`
|
||||
CREATE TABLE IF NOT EXISTS followers (
|
||||
follower TEXT PRIMARY KEY
|
||||
);
|
||||
`)
|
||||
|
||||
return &DefaultFollowerRepo{
|
||||
db: sqlxdb,
|
||||
}
|
||||
}
|
||||
|
||||
// Add implements repository.FollowerRepository.
|
||||
func (d *DefaultFollowerRepo) Add(follower string) error {
|
||||
_, err := d.db.Exec("INSERT INTO followers (follower) VALUES (?) ON CONFLICT DO NOTHING", follower)
|
||||
return err
|
||||
}
|
||||
|
||||
// Remove implements repository.FollowerRepository.
|
||||
func (d *DefaultFollowerRepo) Remove(follower string) error {
|
||||
_, err := d.db.Exec("DELETE FROM followers WHERE follower = ?", follower)
|
||||
return err
|
||||
}
|
||||
|
||||
// All implements repository.FollowerRepository.
|
||||
func (d *DefaultFollowerRepo) All() ([]string, error) {
|
||||
var followers []sqlFollower
|
||||
err := d.db.Select(&followers, "SELECT * FROM followers")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result := []string{}
|
||||
for _, follower := range followers {
|
||||
result = append(result, follower.Follwer)
|
||||
}
|
||||
return result, nil
|
||||
}
|
|
@ -0,0 +1,91 @@
|
|||
package infra_test
|
||||
|
||||
import (
|
||||
"owl-blogs/app/repository"
|
||||
"owl-blogs/infra"
|
||||
"owl-blogs/test"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func setupFollowerRepo() repository.FollowerRepository {
|
||||
db := test.NewMockDb()
|
||||
repo := infra.NewFollowerRepository(db)
|
||||
return repo
|
||||
}
|
||||
|
||||
func TestAddFollower(t *testing.T) {
|
||||
repo := setupFollowerRepo()
|
||||
|
||||
err := repo.Add("foo@example.com")
|
||||
require.NoError(t, err)
|
||||
|
||||
followers, err := repo.All()
|
||||
require.NoError(t, err)
|
||||
require.Len(t, followers, 1)
|
||||
require.Equal(t, followers[0], "foo@example.com")
|
||||
}
|
||||
|
||||
func TestDoubleAddFollower(t *testing.T) {
|
||||
repo := setupFollowerRepo()
|
||||
|
||||
err := repo.Add("foo@example.com")
|
||||
require.NoError(t, err)
|
||||
|
||||
err = repo.Add("foo@example.com")
|
||||
require.NoError(t, err)
|
||||
|
||||
followers, err := repo.All()
|
||||
require.NoError(t, err)
|
||||
require.Len(t, followers, 1)
|
||||
require.Equal(t, followers[0], "foo@example.com")
|
||||
}
|
||||
|
||||
func TestMultipleAddFollower(t *testing.T) {
|
||||
repo := setupFollowerRepo()
|
||||
|
||||
err := repo.Add("foo@example.com")
|
||||
require.NoError(t, err)
|
||||
|
||||
err = repo.Add("bar@example.com")
|
||||
require.NoError(t, err)
|
||||
|
||||
err = repo.Add("baz@example.com")
|
||||
require.NoError(t, err)
|
||||
|
||||
followers, err := repo.All()
|
||||
require.NoError(t, err)
|
||||
require.Len(t, followers, 3)
|
||||
}
|
||||
|
||||
func TestRemoveFollower(t *testing.T) {
|
||||
repo := setupFollowerRepo()
|
||||
|
||||
err := repo.Add("foo@example.com")
|
||||
require.NoError(t, err)
|
||||
|
||||
followers, err := repo.All()
|
||||
require.NoError(t, err)
|
||||
require.Len(t, followers, 1)
|
||||
|
||||
err = repo.Remove("foo@example.com")
|
||||
require.NoError(t, err)
|
||||
|
||||
followers, err = repo.All()
|
||||
require.NoError(t, err)
|
||||
require.Len(t, followers, 0)
|
||||
|
||||
}
|
||||
|
||||
func TestRemoveNonExistingFollower(t *testing.T) {
|
||||
repo := setupFollowerRepo()
|
||||
|
||||
err := repo.Remove("foo@example.com")
|
||||
require.NoError(t, err)
|
||||
|
||||
followers, err := repo.All()
|
||||
require.NoError(t, err)
|
||||
require.Len(t, followers, 0)
|
||||
|
||||
}
|
Loading…
Reference in New Issue