Skip to content
This repository has been archived by the owner on May 11, 2022. It is now read-only.

Commit

Permalink
issue #12: implement insert operations
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilsk committed Nov 3, 2019
1 parent 8a5380a commit d33682f
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 12 deletions.
35 changes: 30 additions & 5 deletions internal/storage/create_board.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,37 @@
package storage

import "go.octolab.org/ecosystem/tablo/internal/model"
import (
"context"
"database/sql"

func (storage *storage) CreateBoard(board model.Board) (model.ID, error) {
storage.builder.
"github.com/Masterminds/squirrel"
"github.com/pkg/errors"

"go.octolab.org/ecosystem/tablo/internal/model"
)

// CreateBoard stores the board into a database.
func (storage *storage) CreateBoard(ctx context.Context, board model.Board) (model.ID, error) {
var id model.ID
tx, err := storage.db.BeginTx(ctx, storage.tx)
if err != nil {
return id, errors.Wrap(err, "create board: cannot begin transaction")
}
defer func() { _ = tx.Rollback() }()
id, err = createBoard(tx, *storage.builder, board)
if err == nil {
return id, errors.Wrap(tx.Commit(), "create board: cannot commit transaction")
}
return id, err
}

func createBoard(tx *sql.Tx, builder squirrel.StatementBuilderType, board model.Board) (model.ID, error) {
var id model.ID
query := builder.
Insert("board").
Columns("title", "emoji", "description").
Values(board.Title, board.Emoji, board.Description).
Suffix(`RETURNING "id"`)
return "", nil
Suffix(`RETURNING "id"`).
RunWith(tx)
return id, errors.Wrap(query.QueryRow().Scan(&id), "create board: cannot insert data")
}
36 changes: 33 additions & 3 deletions internal/storage/create_card.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,37 @@
package storage

import "go.octolab.org/ecosystem/tablo/internal/model"
import (
"context"
"database/sql"

func (storage *storage) CreateCard(card model.Card) (model.ID, error) {
return "", nil
"github.com/Masterminds/squirrel"
"github.com/pkg/errors"

"go.octolab.org/ecosystem/tablo/internal/model"
)

// CreateCard stores the card into a database.
func (storage *storage) CreateCard(ctx context.Context, card model.Card) (model.ID, error) {
var id model.ID
tx, err := storage.db.BeginTx(ctx, storage.tx)
if err != nil {
return id, errors.Wrap(err, "create card: cannot begin transaction")
}
defer func() { _ = tx.Rollback() }()
id, err = createCard(tx, *storage.builder, card)
if err == nil {
return id, errors.Wrap(tx.Commit(), "create card: cannot commit transaction")
}
return id, err
}

func createCard(tx *sql.Tx, builder squirrel.StatementBuilderType, card model.Card) (model.ID, error) {
var id model.ID
query := builder.
Insert("card").
Columns("title", "emoji", "description").
Values(card.Title, card.Emoji, card.Description).
Suffix(`RETURNING "id"`).
RunWith(tx)
return id, errors.Wrap(query.QueryRow().Scan(&id), "create card: cannot insert data")
}
36 changes: 33 additions & 3 deletions internal/storage/create_column.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,37 @@
package storage

import "go.octolab.org/ecosystem/tablo/internal/model"
import (
"context"
"database/sql"

func (storage *storage) CreateColumn(column model.Column) (model.ID, error) {
return "", nil
"github.com/Masterminds/squirrel"
"github.com/pkg/errors"

"go.octolab.org/ecosystem/tablo/internal/model"
)

// CreateColumn stores the column into a database.
func (storage *storage) CreateColumn(ctx context.Context, column model.Column) (model.ID, error) {
var id model.ID
tx, err := storage.db.BeginTx(ctx, storage.tx)
if err != nil {
return id, errors.Wrap(err, "create column: cannot begin transaction")
}
defer func() { _ = tx.Rollback() }()
id, err = createColumn(tx, *storage.builder, column)
if err == nil {
return id, errors.Wrap(tx.Commit(), "create column: cannot commit transaction")
}
return id, err
}

func createColumn(tx *sql.Tx, builder squirrel.StatementBuilderType, column model.Column) (model.ID, error) {
var id model.ID
query := builder.
Insert("column").
Columns("title", "emoji", "description").
Values(column.Title, column.Emoji, column.Description).
Suffix(`RETURNING "id"`).
RunWith(tx)
return id, errors.Wrap(query.QueryRow().Scan(&id), "create column: cannot insert data")
}
2 changes: 1 addition & 1 deletion internal/storage/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const (
sqlite = "sqlite"
)

// Engine returns related to the data source name database a handler
// Engine returns related to the data source name a database handler
// and a query builder or error if something went wrong.
func Engine(dsn string) (*sql.DB, *squirrel.StatementBuilderType, error) {
schema, err := url.Parse(dsn)
Expand Down
5 changes: 5 additions & 0 deletions internal/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"github.com/Masterminds/squirrel"
)

// Must returns new instance of persistent storage
// or panic if something went wrong.
func Must(dsn string) *storage {
storage, err := New(dsn)
if err != nil {
Expand All @@ -14,6 +16,8 @@ func Must(dsn string) *storage {
return storage
}

// New returns new instance of persistent storage
// or error if something went wrong.
func New(dsn string) (*storage, error) {
db, builder, err := Engine(dsn)
if err != nil {
Expand All @@ -24,5 +28,6 @@ func New(dsn string) (*storage, error) {

type storage struct {
db *sql.DB
tx *sql.TxOptions
builder *squirrel.StatementBuilderType
}

0 comments on commit d33682f

Please sign in to comment.