Skip to content

Commit

Permalink
feat: CreateEntity returning *Entity
Browse files Browse the repository at this point in the history
  • Loading branch information
t0t07 committed Nov 10, 2021
1 parent d4016f4 commit 9964c53
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
7 changes: 4 additions & 3 deletions internal/database/metadatav2/mock_metadata/store.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions internal/database/metadatav2/postgres/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@ import (
"github.com/oom-ai/oomstore/pkg/oomstore/types"
)

func (db *DB) CreateEntity(ctx context.Context, opt types.CreateEntityOpt) error {
query := "insert into feature_entity(name, length, description) values($1, $2, $3)"
_, err := db.ExecContext(ctx, query, opt.Name, opt.Length, opt.Description)
func (db *DB) CreateEntity(ctx context.Context, opt types.CreateEntityOpt) (*types.Entity, error) {
var entity types.Entity
query := "insert into feature_entity(name, length, description) values($1, $2, $3) returning *"
err := db.GetContext(ctx, &entity, query, opt.Name, opt.Length, opt.Description)
if er, ok := err.(*pq.Error); ok {
if er.Code == pgerrcode.UniqueViolation {
return fmt.Errorf("entity %s already exists", opt.Name)
return nil, fmt.Errorf("entity %s already exists", opt.Name)
}
}
return err
return &entity, err
}

func (db *DB) UpdateEntity(ctx context.Context, opt types.UpdateEntityOpt) (int64, error) {
Expand Down
2 changes: 1 addition & 1 deletion internal/database/metadatav2/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

type Store interface {
// entity
CreateEntity(ctx context.Context, opt types.CreateEntityOpt) error
CreateEntity(ctx context.Context, opt types.CreateEntityOpt) (*types.Entity, error)
UpdateEntity(ctx context.Context, opt types.UpdateEntityOpt) (int64, error)
GetEntity(ctx context.Context, name string) *typesv2.Entity
ListEntity(ctx context.Context) typesv2.EntityList
Expand Down

0 comments on commit 9964c53

Please sign in to comment.