-
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.
- Loading branch information
Showing
7 changed files
with
324 additions
and
17 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
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,46 @@ | ||
package postgres | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/lib/pq" | ||
"github.com/mgjules/deckr/deck" | ||
"gorm.io/gorm" | ||
) | ||
|
||
// Deck represents a deck of cards. | ||
type Deck struct { | ||
gorm.Model | ||
ID string `gorm:"primaryKey"` | ||
Shuffled bool | ||
Composition string `gorm:"type:varchar(64)"` | ||
Codes pq.StringArray `gorm:"type:varchar(3)[]"` | ||
} | ||
|
||
// DomainDeckToDeck transforms a domain deck to a repo deck. | ||
func DomainDeckToDeck(d *deck.Deck) *Deck { | ||
var rd Deck | ||
rd.ID = d.ID() | ||
rd.Shuffled = d.IsShuffled() | ||
rd.Composition = d.Composition() | ||
for _, card := range d.Cards() { | ||
rd.Codes = append(rd.Codes, card.Code().String()) | ||
} | ||
|
||
return &rd | ||
} | ||
|
||
// DeckToDomainDeck transforms a repo deck to a domain deck. | ||
func DeckToDomainDeck(rd *Deck) (*deck.Deck, error) { | ||
d, err := deck.New( | ||
deck.WithID(rd.ID), | ||
deck.WithShuffled(rd.Shuffled), | ||
deck.WithComposition(rd.Composition), | ||
deck.WithCodes(rd.Codes...), | ||
) | ||
if err != nil { | ||
return nil, fmt.Errorf("new deck: %w", err) | ||
} | ||
|
||
return d, 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,76 @@ | ||
package postgres | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/mgjules/deckr/deck" | ||
"github.com/mgjules/deckr/logger" | ||
"gorm.io/driver/postgres" | ||
"gorm.io/gorm" | ||
) | ||
|
||
// ErrDeckNotFound is the error returned when a deck is not found. | ||
var ErrDeckNotFound = errors.New("deck not found") | ||
|
||
// Repository is a PostgreSQL implementation of the deckr.Repository interface. | ||
type Repository struct { | ||
log *logger.Logger | ||
db *gorm.DB | ||
} | ||
|
||
// NewRepository creates a new PostgreSQL repository. | ||
func NewRepository(uri string, log *logger.Logger) (*Repository, error) { | ||
db, err := gorm.Open(postgres.New(postgres.Config{ | ||
DSN: uri, | ||
})) | ||
if err != nil { | ||
return nil, fmt.Errorf("open postgres connection: %w", err) | ||
} | ||
|
||
return &Repository{ | ||
log: log, | ||
db: db, | ||
}, nil | ||
} | ||
|
||
// Get returns the deck with the given id. | ||
func (r *Repository) Get(_ context.Context, id string) (*deck.Deck, error) { | ||
var saved Deck | ||
|
||
if err := r.db.First(&saved, id); err != nil { | ||
return nil, fmt.Errorf("deck '%s': %w", id, ErrDeckNotFound) | ||
} | ||
|
||
d, err := DeckToDomainDeck(&saved) | ||
if err != nil { | ||
return nil, fmt.Errorf("deck '%s': %w", id, err) | ||
} | ||
|
||
r.log.Debugf("get deck '%s'", d.ID) | ||
|
||
return d, nil | ||
} | ||
|
||
// Save saves the given deck. | ||
func (r *Repository) Save(_ context.Context, d *deck.Deck) error { | ||
save := DomainDeckToDeck(d) | ||
|
||
r.db.Save(save) | ||
|
||
r.log.Debugf("saved deck '%s'", save.ID) | ||
|
||
return nil | ||
} | ||
|
||
// Migrate migratess the deck model. | ||
func (r *Repository) Migrate(_ context.Context) error { | ||
if err := r.db.AutoMigrate(&Deck{}); err != nil { | ||
return fmt.Errorf("migrate deck model: %w", err) | ||
} | ||
|
||
r.log.Debug("migrated deck model") | ||
|
||
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