-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from abjrcode/favorites-and-dashboard
Add navigation sidebar and mechanism for marking provider instance as favorite
- Loading branch information
Showing
23 changed files
with
724 additions
and
174 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package favorites | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"errors" | ||
|
||
"github.com/rs/zerolog" | ||
) | ||
|
||
type Favorite struct { | ||
ProviderCode string | ||
InstanceId string | ||
} | ||
|
||
type FavoritesRepo interface { | ||
ListAll(ctx context.Context) ([]*Favorite, error) | ||
IsFavorite(ctx context.Context, favorite *Favorite) (bool, error) | ||
Add(ctx context.Context, favorite *Favorite) error | ||
Remove(ctx context.Context, favorite *Favorite) error | ||
} | ||
|
||
type favoritesImpl struct { | ||
logger *zerolog.Logger | ||
db *sql.DB | ||
} | ||
|
||
func NewFavorites(db *sql.DB, logger *zerolog.Logger) FavoritesRepo { | ||
enrichedLogger := logger.With().Str("component", "favorites_repo").Logger() | ||
|
||
return &favoritesImpl{ | ||
db: db, | ||
logger: &enrichedLogger, | ||
} | ||
} | ||
|
||
func (f *favoritesImpl) ListAll(ctx context.Context) ([]*Favorite, error) { | ||
rows, err := f.db.QueryContext(ctx, `SELECT * FROM favorite_instances`) | ||
|
||
if err != nil { | ||
if err == sql.ErrNoRows { | ||
return []*Favorite{}, nil | ||
} | ||
|
||
return nil, err | ||
} | ||
|
||
favorites := make([]*Favorite, 0, 10) | ||
|
||
for rows.Next() { | ||
var favorite Favorite | ||
err := rows.Scan(&favorite.ProviderCode, &favorite.InstanceId) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
favorites = append(favorites, &favorite) | ||
} | ||
|
||
return favorites, nil | ||
} | ||
|
||
func (f *favoritesImpl) IsFavorite(ctx context.Context, favorite *Favorite) (bool, error) { | ||
row := f.db.QueryRowContext(ctx, `SELECT COUNT(*) FROM favorite_instances WHERE provider_code = ? AND instance_id = ? `, favorite.ProviderCode, favorite.InstanceId) | ||
|
||
var count int | ||
err := row.Scan(&count) | ||
|
||
if err != nil { | ||
if errors.Is(err, sql.ErrNoRows) { | ||
return false, nil | ||
} | ||
|
||
return false, err | ||
} | ||
|
||
return count > 0, nil | ||
} | ||
|
||
func (f *favoritesImpl) Add(ctx context.Context, favorite *Favorite) error { | ||
_, err := f.db.ExecContext(ctx, `INSERT INTO favorite_instances (provider_code, instance_id) VALUES (?, ?) `, favorite.ProviderCode, favorite.InstanceId) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (f *favoritesImpl) Remove(ctx context.Context, favorite *Favorite) error { | ||
res, err := f.db.ExecContext(ctx, `DELETE FROM favorite_instances WHERE provider_code = ? AND instance_id = ? `, favorite.ProviderCode, favorite.InstanceId) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
rowsAffected, err := res.RowsAffected() | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
if rowsAffected == 0 { | ||
return sql.ErrNoRows | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package favorites | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/abjrcode/swervo/internal/migrations" | ||
"github.com/abjrcode/swervo/providers" | ||
"github.com/rs/zerolog" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestAddFavorite(t *testing.T) { | ||
db, err := migrations.NewInMemoryMigratedDatabase(t, "favorites-repo-tests.db") | ||
require.NoError(t, err) | ||
|
||
logger := zerolog.Nop() | ||
repo := NewFavorites(db, &logger) | ||
|
||
favorite := &Favorite{ | ||
ProviderCode: providers.AwsIamIdc, | ||
InstanceId: "some-nice-id", | ||
} | ||
|
||
ctx := context.Background() | ||
err = repo.Add(ctx, favorite) | ||
require.NoError(t, err) | ||
|
||
favorites, err := repo.ListAll(ctx) | ||
require.NoError(t, err) | ||
|
||
require.Len(t, favorites, 1) | ||
require.Equal(t, favorite, favorites[0]) | ||
} | ||
|
||
func TestRemoveFavorite(t *testing.T) { | ||
db, err := migrations.NewInMemoryMigratedDatabase(t, "favorites-repo-tests.db") | ||
require.NoError(t, err) | ||
|
||
logger := zerolog.Nop() | ||
|
||
repo := NewFavorites(db, &logger) | ||
ctx := context.Background() | ||
|
||
favorite := &Favorite{ | ||
ProviderCode: providers.AwsIamIdc, | ||
InstanceId: "some-nice-id", | ||
} | ||
|
||
err = repo.Add(ctx, favorite) | ||
require.NoError(t, err) | ||
|
||
favorites, err := repo.ListAll(ctx) | ||
require.NoError(t, err) | ||
|
||
require.Len(t, favorites, 1) | ||
require.Equal(t, favorite, favorites[0]) | ||
|
||
err = repo.Remove(ctx, favorite) | ||
require.NoError(t, err) | ||
|
||
favorites, err = repo.ListAll(ctx) | ||
require.NoError(t, err) | ||
|
||
require.Len(t, favorites, 0) | ||
} | ||
|
||
func TestIsFavorite(t *testing.T) { | ||
db, err := migrations.NewInMemoryMigratedDatabase(t, "favorites-repo-tests.db") | ||
require.NoError(t, err) | ||
|
||
logger := zerolog.Nop() | ||
|
||
repo := NewFavorites(db, &logger) | ||
ctx := context.Background() | ||
|
||
favorite := &Favorite{ | ||
ProviderCode: providers.AwsIamIdc, | ||
InstanceId: "some-nice-id", | ||
} | ||
|
||
err = repo.Add(ctx, favorite) | ||
require.NoError(t, err) | ||
|
||
isFavorite, err := repo.IsFavorite(ctx, favorite) | ||
require.NoError(t, err) | ||
require.True(t, isFavorite) | ||
|
||
isFavorite, err = repo.IsFavorite(ctx, &Favorite{ | ||
ProviderCode: providers.AwsIamIdc, | ||
InstanceId: "some-nice-id-2", | ||
}) | ||
require.NoError(t, err) | ||
require.False(t, isFavorite) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.