3
0

upd(all): use factory.Database.MustGet over .Get

This commit is contained in:
Tit Petric
2018-07-17 18:23:02 +02:00
parent 8753c18e0b
commit 80f9e9b9bc
6 changed files with 19 additions and 84 deletions
-8
View File
@@ -13,14 +13,6 @@ import (
"github.com/titpetric/factory"
)
const (
defaultAddr = ":3000"
defaultDsn = "crust:crust@tcp(db1:3306)/crust?collation=utf8mb4_general_ci"
envVarKey_HTTP_ADDR = "CRM_HTTP_ADDR"
envVarKey_DB_DSN = "CRM_DB_DSN"
)
func handleError(err error, message string) {
if message == "" {
message = "Error making API call"
+4 -16
View File
@@ -22,10 +22,7 @@ func Channel() channel {
}
func (r channel) FindById(ctx context.Context, id uint64) (*types.Channel, error) {
db, err := factory.Database.Get()
if err != nil {
return nil, ErrDatabaseError
}
db := factory.Database.MustGet()
mod := &types.Channel{}
if err := db.Get(mod, "SELECT * FROM channels WHERE id = ? AND "+sqlChannelScope, id); err != nil {
@@ -38,10 +35,7 @@ func (r channel) FindById(ctx context.Context, id uint64) (*types.Channel, error
}
func (r channel) Find(ctx context.Context, filter *types.ChannelFilter) ([]*types.Channel, error) {
db, err := factory.Database.Get()
if err != nil {
return nil, ErrDatabaseError
}
db := factory.Database.MustGet()
var params = make([]interface{}, 0)
sql := "SELECT * FROM channels WHERE " + sqlChannelScope
@@ -64,10 +58,7 @@ func (r channel) Find(ctx context.Context, filter *types.ChannelFilter) ([]*type
}
func (r channel) Create(ctx context.Context, mod *types.Channel) (*types.Channel, error) {
db, err := factory.Database.Get()
if err != nil {
return nil, ErrDatabaseError
}
db := factory.Database.MustGet()
mod.SetID(factory.Sonyflake.NextID())
if err := db.Insert("channels", mod); err != nil {
@@ -78,10 +69,7 @@ func (r channel) Create(ctx context.Context, mod *types.Channel) (*types.Channel
}
func (r channel) Update(ctx context.Context, mod *types.Channel) (*types.Channel, error) {
db, err := factory.Database.Get()
if err != nil {
return nil, ErrDatabaseError
}
db := factory.Database.MustGet()
if err := db.Replace("channels", mod); err != nil {
return nil, ErrDatabaseError
+2 -8
View File
@@ -7,10 +7,7 @@ import (
)
func simpleUpdate(ctx context.Context, tableName, columnName string, value interface{}, id uint64) error {
db, err := factory.Database.Get()
if err != nil {
return ErrDatabaseError
}
db := factory.Database.MustGet()
sql := fmt.Sprintf("UPDATE %s SET %s = ? WHERE id = ?", tableName, columnName)
@@ -22,10 +19,7 @@ func simpleUpdate(ctx context.Context, tableName, columnName string, value inter
}
func simpleDelete(ctx context.Context, tableName string, id uint64) error {
db, err := factory.Database.Get()
if err != nil {
return ErrDatabaseError
}
db := factory.Database.MustGet()
sql := fmt.Sprintf("DELETE %s WHERE id = ?", tableName)
+4 -16
View File
@@ -22,10 +22,7 @@ func Organisation() organisation {
}
func (r organisation) FindById(ctx context.Context, id uint64) (*types.Organisation, error) {
db, err := factory.Database.Get()
if err != nil {
return nil, ErrDatabaseError
}
db := factory.Database.MustGet()
mod := &types.Organisation{}
if err := db.Get(mod, "SELECT * FROM organisations WHERE id = ? AND "+sqlOrganisationScope, id); err != nil {
@@ -38,10 +35,7 @@ func (r organisation) FindById(ctx context.Context, id uint64) (*types.Organisat
}
func (r organisation) Find(ctx context.Context, filter *types.OrganisationFilter) ([]*types.Organisation, error) {
db, err := factory.Database.Get()
if err != nil {
return nil, ErrDatabaseError
}
db := factory.Database.MustGet()
var params = make([]interface{}, 0)
sql := "SELECT * FROM organisations WHERE " + sqlOrganisationScope
@@ -64,10 +58,7 @@ func (r organisation) Find(ctx context.Context, filter *types.OrganisationFilter
}
func (r organisation) Create(ctx context.Context, mod *types.Organisation) (*types.Organisation, error) {
db, err := factory.Database.Get()
if err != nil {
return nil, ErrDatabaseError
}
db := factory.Database.MustGet()
mod.SetID(factory.Sonyflake.NextID())
if err := db.Insert("organisations", mod); err != nil {
@@ -78,10 +69,7 @@ func (r organisation) Create(ctx context.Context, mod *types.Organisation) (*typ
}
func (r organisation) Update(ctx context.Context, mod *types.Organisation) (*types.Organisation, error) {
db, err := factory.Database.Get()
if err != nil {
return nil, ErrDatabaseError
}
db := factory.Database.MustGet()
if err := db.Replace("organisations", mod); err != nil {
return nil, ErrDatabaseError
+4 -16
View File
@@ -22,10 +22,7 @@ func Team() team {
}
func (r team) FindById(ctx context.Context, id uint64) (*types.Team, error) {
db, err := factory.Database.Get()
if err != nil {
return nil, ErrDatabaseError
}
db := factory.Database.MustGet()
mod := &types.Team{}
if err := db.Get(mod, "SELECT * FROM teams WHERE id = ? AND "+sqlTeamScope, id); err != nil {
@@ -38,10 +35,7 @@ func (r team) FindById(ctx context.Context, id uint64) (*types.Team, error) {
}
func (r team) Find(ctx context.Context, filter *types.TeamFilter) ([]*types.Team, error) {
db, err := factory.Database.Get()
if err != nil {
return nil, ErrDatabaseError
}
db := factory.Database.MustGet()
var params = make([]interface{}, 0)
sql := "SELECT * FROM teams WHERE " + sqlTeamScope
@@ -64,10 +58,7 @@ func (r team) Find(ctx context.Context, filter *types.TeamFilter) ([]*types.Team
}
func (r team) Create(ctx context.Context, mod *types.Team) (*types.Team, error) {
db, err := factory.Database.Get()
if err != nil {
return nil, ErrDatabaseError
}
db := factory.Database.MustGet()
mod.SetID(factory.Sonyflake.NextID())
if err := db.Insert("teams", mod); err != nil {
@@ -78,10 +69,7 @@ func (r team) Create(ctx context.Context, mod *types.Team) (*types.Team, error)
}
func (r team) Update(ctx context.Context, mod *types.Team) (*types.Team, error) {
db, err := factory.Database.Get()
if err != nil {
return nil, ErrDatabaseError
}
db := factory.Database.MustGet()
if err := db.Replace("teams", mod); err != nil {
return nil, ErrDatabaseError
+5 -20
View File
@@ -23,10 +23,7 @@ func User() user {
}
func (r user) FindByUsername(ctx context.Context, username string) (*types.User, error) {
db, err := factory.Database.Get()
if err != nil {
return nil, ErrDatabaseError
}
db := factory.Database.MustGet()
mod := &types.User{}
if err := db.Get(mod, "SELECT * FROM users WHERE username = ? AND "+sqlUserScope, username); err != nil {
@@ -39,10 +36,7 @@ func (r user) FindByUsername(ctx context.Context, username string) (*types.User,
}
func (r user) FindById(ctx context.Context, id uint64) (*types.User, error) {
db, err := factory.Database.Get()
if err != nil {
return nil, ErrDatabaseError
}
db := factory.Database.MustGet()
mod := &types.User{}
if err := db.Get(mod, "SELECT * FROM users WHERE id = ? AND "+sqlUserScope, id); err != nil {
@@ -55,10 +49,7 @@ func (r user) FindById(ctx context.Context, id uint64) (*types.User, error) {
}
func (r user) Find(ctx context.Context, filter *types.UserFilter) ([]*types.User, error) {
db, err := factory.Database.Get()
if err != nil {
return nil, ErrDatabaseError
}
db := factory.Database.MustGet()
var params = make([]interface{}, 0)
sql := "SELECT * FROM users WHERE " + sqlUserScope
@@ -81,10 +72,7 @@ func (r user) Find(ctx context.Context, filter *types.UserFilter) ([]*types.User
}
func (r user) Create(ctx context.Context, mod *types.User) (*types.User, error) {
db, err := factory.Database.Get()
if err != nil {
return nil, ErrDatabaseError
}
db := factory.Database.MustGet()
mod.SetID(factory.Sonyflake.NextID())
if err := db.Insert("users", mod); err != nil {
@@ -95,10 +83,7 @@ func (r user) Create(ctx context.Context, mod *types.User) (*types.User, error)
}
func (r user) Update(ctx context.Context, mod *types.User) (*types.User, error) {
db, err := factory.Database.Get()
if err != nil {
return nil, ErrDatabaseError
}
db := factory.Database.MustGet()
if err := db.Replace("users", mod); err != nil {
return nil, ErrDatabaseError