-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Renamed methods to improve consistency
- Loading branch information
1 parent
4bd233d
commit 9ddc935
Showing
24 changed files
with
163 additions
and
155 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
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,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 | ||
} |
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 |
---|---|---|
@@ -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 | ||
} |
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 was deleted.
Oops, something went wrong.
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
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
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 | ||
} |
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
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
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
Oops, something went wrong.