Skip to content

Commit

Permalink
Renamed methods to improve consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
lesichkovm committed Oct 26, 2023
1 parent 4bd233d commit 9ddc935
Show file tree
Hide file tree
Showing 24 changed files with 163 additions and 155 deletions.
22 changes: 3 additions & 19 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,6 @@

# Dependency directories (remove the comment below to include it)
# vendor/
entity_create.db
entity_update.db
test_entity_create.db
test_entity_automigrate.db
test_entity_delete.db
test_entity_trash.db
attribute.go
test_attributes_create.db
test_entity_update.db
test_attribute_create.db
test_attribute_float.db
test_attribute_string.db
test_store_create.db
test_attribute_int.db
oryxBuildBinary
test_entity_create_with_attributes.db
test_entity_find_by_attribute.db
test_attribute_find_by_handle.db
test_attribute_find.db

# Test databases
*.db
53 changes: 42 additions & 11 deletions AttributeCreate.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,52 @@
package entitystore

import (
"errors"
"log"
"time"

"github.com/doug-martin/goqu/v9"
"github.com/gouniverse/uid"
)

// AttributeCreate creates a new attribute
func (st *Store) AttributeCreate(entityID string, attributeKey string, attributeValue string) (*Attribute, error) {
var newAttribute = st.NewAttribute(NewAttributeOptions{
ID: uid.HumanUid(),
EntityID: entityID,
AttributeKey: attributeKey,
AttributeValue: attributeValue,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
})

return st.AttributeInsert(*newAttribute)
func (st *Store) AttributeCreate(attr *Attribute) error {
if attr == nil {
return errors.New("attribute is required")
}

if attr.AttributeKey() == "" {
return errors.New("attribute key is required field")
}

if attr.ID() == "" {
attr.SetID(uid.HumanUid())
}

if attr.CreatedAt().IsZero() {
attr.SetCreatedAt(time.Now())
}

if attr.UpdatedAt().IsZero() {
attr.SetUpdatedAt(time.Now())
}

q := goqu.Dialect(st.dbDriverName).Insert(st.attributeTableName)
q = q.Rows(attr.ToMap())
sqlStr, _, _ := q.ToSQL()

if st.GetDebug() {
log.Println(sqlStr)
}

_, err := st.database.Exec(sqlStr)

if err != nil {
if st.GetDebug() {
log.Println(err)
}
return err
}

return nil
}
29 changes: 29 additions & 0 deletions AttributeCreateWithKeyAndValue.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package entitystore

import (
"time"

"github.com/gouniverse/uid"
)

// AttributeCreateWithKeyAndValue shortcut to create a new attribute
// by providing only the key and value
// NN. The ID will be auto-assigned
func (st *Store) AttributeCreateWithKeyAndValue(entityID string, attributeKey string, attributeValue string) (*Attribute, error) {
newAttribute := st.NewAttribute(NewAttributeOptions{
ID: uid.HumanUid(),
EntityID: entityID,
AttributeKey: attributeKey,
AttributeValue: attributeValue,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
})

err := st.AttributeCreate(&newAttribute)

if err != nil {
return nil, err
}

return &newAttribute, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package entitystore

import "testing"

func TestAttributeCreate(t *testing.T) {
db := InitDB("test_attribute_create.db")
func TestAttributeCreateWithKeyAndValue(t *testing.T) {
db := InitDB("test_attribute_create_with_key_and_value.db")

store, err := NewStore(NewStoreOptions{
DB: db,
Expand Down
45 changes: 0 additions & 45 deletions AttributeInstert.go

This file was deleted.

2 changes: 1 addition & 1 deletion AttributeList.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (st *Store) AttributeList(options AttributeQueryOptions) (attributeList []A

for i := 0; i < len(attributeMaps); i++ {
attribute := st.NewAttributeFromMap(attributeMaps[i])
attributeList = append(attributeList, *attribute)
attributeList = append(attributeList, attribute)
}

return attributeList, nil
Expand Down
2 changes: 1 addition & 1 deletion AttributeSetString.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ func (st *Store) AttributeSetString(entityID string, attributeKey string, attrib
}

if attr == nil {
attr, err := st.AttributeCreate(entityID, attributeKey, attributeValue)
attr, err := st.AttributeCreateWithKeyAndValue(entityID, attributeKey, attributeValue)
if err != nil {
return err
}
Expand Down
32 changes: 21 additions & 11 deletions EntityCreate.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package entitystore

import (
"errors"
"log"
"time"

Expand All @@ -9,21 +10,30 @@ import (
)

// EntityCreate creates a new entity
func (st *Store) EntityCreate(entityType string) (*Entity, error) {
entity := st.NewEntity(NewEntityOptions{
ID: uid.HumanUid(),
Type: entityType,
Handle: "",
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
})
func (st *Store) EntityCreate(entity *Entity) error {
if entity == nil {
return errors.New("entity cannot be nil")
}

if entity.ID() == "" {
entity.SetID(uid.HumanUid())
}

if entity.CreatedAt().IsZero() {
entity.SetCreatedAt(time.Now())
}

if entity.UpdatedAt().IsZero() {
entity.SetUpdatedAt(time.Now())
}

q := goqu.Dialect(st.dbDriverName).Insert(st.entityTableName)
q = q.Rows(entity.ToMap())

sqlStr, _, errSql := q.ToSQL()

if errSql != nil {
return nil, errSql
return errSql
}

if st.GetDebug() {
Expand All @@ -33,8 +43,8 @@ func (st *Store) EntityCreate(entityType string) (*Entity, error) {
_, err := st.database.Exec(sqlStr)

if err != nil {
return entity, err
return err
}

return entity, nil
return nil
}
28 changes: 28 additions & 0 deletions EntityCreateWithType.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package entitystore

import (
"time"

"github.com/gouniverse/uid"
)

// EntityCreateWithType quick shortcut method
// to create an entity by providing only the type
// NB. The ID will be auto-assigned
func (st *Store) EntityCreateWithType(entityType string) (*Entity, error) {
entity := st.NewEntity(NewEntityOptions{
ID: uid.HumanUid(),
Type: entityType,
Handle: "",
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
})

err := st.EntityCreate(&entity)

if err != nil {
return &entity, err
}

return &entity, nil
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package entitystore

// EntityCreateWithAttributes func
func (st *Store) EntityCreateWithAttributes(entityType string, attributes map[string]string) (*Entity, error) {
// Note the use of tx as the database handle once you are within a transaction
// tx, err := st.db.Begin()
// EntityCreateWithTypeAndAttributes quick shortcut method
// to create an entity by providing only the type as string
// and the attributes as map
// NB. The IDs will be auto-assigned
func (st *Store) EntityCreateWithTypeAndAttributes(entityType string, attributes map[string]string) (*Entity, error) {
err := st.database.BeginTransaction()

if err != nil {
Expand All @@ -16,15 +17,15 @@ func (st *Store) EntityCreateWithAttributes(entityType string, attributes map[st
}
}()

entity, err := st.EntityCreate(entityType)
entity, err := st.EntityCreateWithType(entityType)

if err != nil {
st.database.RollbackTransaction()
return nil, err
}

for k, v := range attributes {
_, err := st.AttributeCreate(entity.ID(), k, v)
_, err := st.AttributeCreateWithKeyAndValue(entity.ID(), k, v)

if err != nil {
st.database.RollbackTransaction()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package entitystore
import "testing"

func TestEntityCreateWithAttributes(t *testing.T) {
db := InitDB("test_entity_create_with_attributes.db")
db := InitDB("test_entity_create_with_type_and_attributes.db")

store, err := NewStore(NewStoreOptions{
DB: db,
Expand All @@ -16,7 +16,7 @@ func TestEntityCreateWithAttributes(t *testing.T) {
t.Fatal("Must be NIL:", err.Error())
}

entity, err := store.EntityCreateWithAttributes("post", map[string]string{
entity, err := store.EntityCreateWithTypeAndAttributes("post", map[string]string{
"name": "Hello world",
})

Expand Down
8 changes: 4 additions & 4 deletions EntityCreate_test.go → EntityCreateWithType_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"time"
)

func TestEntityCreate(t *testing.T) {
db := InitDB("test_entity_create.db")
func TestEntityCreateWithType(t *testing.T) {
db := InitDB("test_entity_create_with_type.db")

store, _ := NewStore(NewStoreOptions{
DB: db,
Expand All @@ -15,7 +15,7 @@ func TestEntityCreate(t *testing.T) {
AutomigrateEnabled: true,
})

entity, _ := store.EntityCreate("post")
entity, _ := store.EntityCreateWithType("post")
if entity == nil {
t.Fatalf("Entity could not be created")
}
Expand All @@ -37,6 +37,6 @@ func TestEntityCreate(t *testing.T) {
}

if entity.UpdatedAt().After(time.Now().Add(1 * time.Minute)) {
t.Fatalf("Entity UpdateddAt is not recent (after 1 min):" + entity.CreatedAt().String())
t.Fatalf("Entity UpdatedAt is not recent (after 1 min):" + entity.CreatedAt().String())
}
}
4 changes: 2 additions & 2 deletions EntityDelete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ func TestEntityDelete(t *testing.T) {
t.Fatal("Must be NIL:", err.Error())
}

entity, err := store.EntityCreate("post")
entity, err := store.EntityCreateWithType("post")

if err != nil {
t.Fatalf("Entiry could not be created: " + err.Error())
t.Fatalf("Entity could not be created: " + err.Error())
}

if entity == nil {
Expand Down
2 changes: 1 addition & 1 deletion EntityFindByAttribute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestEntityFindByAttribute(t *testing.T) {
t.Fatal("Must be NIL:", err.Error())
}

entity, err := store.EntityCreateWithAttributes("post", map[string]string{
entity, err := store.EntityCreateWithTypeAndAttributes("post", map[string]string{
"path": "/",
})

Expand Down
Loading

0 comments on commit 9ddc935

Please sign in to comment.