This repository has been archived by the owner on May 11, 2022. It is now read-only.
-
-
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.
issue #12: implement insert operations
- Loading branch information
Showing
5 changed files
with
102 additions
and
12 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
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") | ||
} |
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 |
---|---|---|
@@ -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") | ||
} |
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 |
---|---|---|
@@ -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") | ||
} |
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