Skip to content

Commit

Permalink
fix: postgres implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
mgjules committed May 18, 2022
1 parent 3360fbf commit 2c21f80
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 29 deletions.
10 changes: 10 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: '3.1'

services:
db:
image: postgres:14.3-alpine
ports:
- "5432:5432"
environment:
POSTGRES_PASSWORD: deckr
POSTGRES_USER: deckr
14 changes: 7 additions & 7 deletions grpc/deck_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/mgjules/deckr/deck"
"github.com/mgjules/deckr/logger"
"github.com/mgjules/deckr/repo"
"github.com/mgjules/deckr/repo/inmemory"
"github.com/mgjules/deckr/repo/errs"
"github.com/satori/uuid"
codes "google.golang.org/grpc/codes"
status "google.golang.org/grpc/status"
Expand Down Expand Up @@ -74,8 +74,8 @@ func (s *DeckService) OpenDeck(ctx context.Context, req *OpenDeckRequest) (*Open
if err != nil {
s.log.Errorf("open deck: %v", err)

if errors.Is(err, inmemory.ErrDeckNotFound) {
return nil, status.Errorf(codes.NotFound, inmemory.ErrDeckNotFound.Error())
if errors.Is(err, errs.ErrDeckNotFound) {
return nil, status.Errorf(codes.NotFound, errs.ErrDeckNotFound.Error())
}

return nil, status.Errorf(codes.Internal, err.Error())
Expand Down Expand Up @@ -109,8 +109,8 @@ func (s *DeckService) DrawCards(ctx context.Context, req *DrawCardsRequest) (*Dr
if err != nil {
s.log.Errorf("get deck: %v", err)

if errors.Is(err, inmemory.ErrDeckNotFound) {
return nil, status.Errorf(codes.NotFound, inmemory.ErrDeckNotFound.Error())
if errors.Is(err, errs.ErrDeckNotFound) {
return nil, status.Errorf(codes.NotFound, errs.ErrDeckNotFound.Error())
}

return nil, status.Errorf(codes.Internal, err.Error())
Expand Down Expand Up @@ -153,8 +153,8 @@ func (s *DeckService) ShuffleDeck(ctx context.Context, req *ShuffleDeckRequest)
if err != nil {
s.log.Errorf("get deck: %v", err)

if errors.Is(err, inmemory.ErrDeckNotFound) {
return nil, status.Errorf(codes.NotFound, inmemory.ErrDeckNotFound.Error())
if errors.Is(err, errs.ErrDeckNotFound) {
return nil, status.Errorf(codes.NotFound, errs.ErrDeckNotFound.Error())
}

return nil, status.Errorf(codes.Internal, err.Error())
Expand Down
14 changes: 7 additions & 7 deletions http/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/mgjules/deckr/composition"
"github.com/mgjules/deckr/deck"
"github.com/mgjules/deckr/docs"
"github.com/mgjules/deckr/repo/inmemory"
"github.com/mgjules/deckr/repo/errs"
"github.com/satori/uuid"
ginSwagger "github.com/swaggo/gin-swagger"
"github.com/swaggo/gin-swagger/swaggerFiles"
Expand Down Expand Up @@ -122,8 +122,8 @@ func (s *Server) handleOpenDeck() gin.HandlerFunc {
if err != nil {
s.log.Errorf("open deck: %v", err)

if errors.Is(err, inmemory.ErrDeckNotFound) {
c.AbortWithStatusJSON(http.StatusNotFound, Error{inmemory.ErrDeckNotFound.Error()})
if errors.Is(err, errs.ErrDeckNotFound) {
c.AbortWithStatusJSON(http.StatusNotFound, Error{errs.ErrDeckNotFound.Error()})

return
}
Expand Down Expand Up @@ -173,8 +173,8 @@ func (s *Server) handleDrawCards() gin.HandlerFunc {
if err != nil {
s.log.Errorf("get deck: %v", err)

if errors.Is(err, inmemory.ErrDeckNotFound) {
c.AbortWithStatusJSON(http.StatusNotFound, Error{inmemory.ErrDeckNotFound.Error()})
if errors.Is(err, errs.ErrDeckNotFound) {
c.AbortWithStatusJSON(http.StatusNotFound, Error{errs.ErrDeckNotFound.Error()})

return
}
Expand Down Expand Up @@ -237,8 +237,8 @@ func (s *Server) handleShuffleDeck() gin.HandlerFunc {
if err != nil {
s.log.Errorf("get deck: %v", err)

if errors.Is(err, inmemory.ErrDeckNotFound) {
c.AbortWithStatusJSON(http.StatusNotFound, Error{inmemory.ErrDeckNotFound.Error()})
if errors.Is(err, errs.ErrDeckNotFound) {
c.AbortWithStatusJSON(http.StatusNotFound, Error{errs.ErrDeckNotFound.Error()})

return
}
Expand Down
4 changes: 2 additions & 2 deletions http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ type Server struct {

// NewServer creates a new Server.
func NewServer(
prod bool,
debug bool,
host string,
port int,
logger *logger.Logger,
build *build.Info,
repo repo.Repository,
) *Server {
if prod {
if !debug {
gin.SetMode(gin.ReleaseMode)
}

Expand Down
6 changes: 6 additions & 0 deletions repo/errs/errs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package errs

import "errors"

// ErrDeckNotFound is the error returned when a deck is not found.
var ErrDeckNotFound = errors.New("deck not found")
7 changes: 2 additions & 5 deletions repo/inmemory/inmemory.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@ package inmemory

import (
"context"
"errors"
"fmt"
"sync"

"github.com/mgjules/deckr/deck"
"github.com/mgjules/deckr/logger"
"github.com/mgjules/deckr/repo/errs"
)

// ErrDeckNotFound is the error returned when a deck is not found.
var ErrDeckNotFound = errors.New("deck not found")

// Repository is an in-memory implementation of the deckr.Repository interface.
type Repository struct {
log *logger.Logger
Expand All @@ -36,7 +33,7 @@ func (r *Repository) Get(_ context.Context, id string) (*deck.Deck, error) {

saved, ok := r.items[id]
if !ok {
return nil, fmt.Errorf("deck '%s': %w", id, ErrDeckNotFound)
return nil, fmt.Errorf("deck '%s': %w", id, errs.ErrDeckNotFound)
}

d, err := DeckToDomainDeck(saved)
Expand Down
20 changes: 13 additions & 7 deletions repo/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@ import (

"github.com/mgjules/deckr/deck"
"github.com/mgjules/deckr/logger"
"github.com/mgjules/deckr/repo/errs"
"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
Expand All @@ -37,10 +35,16 @@ func NewRepository(uri string, log *logger.Logger) (*Repository, error) {

// Get returns the deck with the given id.
func (r *Repository) Get(_ context.Context, id string) (*deck.Deck, error) {
var saved Deck
saved := Deck{
ID: id,
}

if err := r.db.First(&saved).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, fmt.Errorf("deck '%s': %w", id, errs.ErrDeckNotFound)
}

if err := r.db.First(&saved, id); err != nil {
return nil, fmt.Errorf("deck '%s': %w", id, ErrDeckNotFound)
return nil, fmt.Errorf("deck '%s': %w", id, err)
}

d, err := DeckToDomainDeck(&saved)
Expand All @@ -57,7 +61,9 @@ func (r *Repository) Get(_ context.Context, id string) (*deck.Deck, error) {
func (r *Repository) Save(_ context.Context, d *deck.Deck) error {
save := DomainDeckToDeck(d)

r.db.Save(save)
if err := r.db.Save(save).Error; err != nil {
return fmt.Errorf("save deck '%s': %w", save.ID, err)
}

r.log.Debugf("saved deck '%s'", save.ID)

Expand Down
2 changes: 1 addition & 1 deletion repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func NewRepository(uri string, log *logger.Logger) (Repository, error) {
switch u.Scheme {
case "inmemory":
return inmemory.NewRepository(log), nil
case "postgres":
case "postgresql":
repo, err := postgres.NewRepository(uri, log)
if err != nil {
return nil, fmt.Errorf("new postgres repository: %w", err)
Expand Down

0 comments on commit 2c21f80

Please sign in to comment.