Refactor current-time helpers
This commit is contained in:
@@ -3,7 +3,6 @@ package repository
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/titpetric/factory"
|
||||
"gopkg.in/Masterminds/squirrel.v1"
|
||||
@@ -139,14 +138,15 @@ func (r chart) Find(filter types.ChartFilter) (set types.ChartSet, f types.Chart
|
||||
|
||||
func (r chart) Create(mod *types.Chart) (*types.Chart, error) {
|
||||
mod.ID = factory.Sonyflake.NextID()
|
||||
mod.CreatedAt = time.Now().Truncate(time.Second)
|
||||
rh.SetCurrentTimeRounded(&mod.CreatedAt)
|
||||
mod.UpdatedAt = nil
|
||||
|
||||
return mod, r.db().Insert(r.table(), mod)
|
||||
}
|
||||
|
||||
func (r chart) Update(mod *types.Chart) (*types.Chart, error) {
|
||||
now := time.Now().Truncate(time.Second)
|
||||
mod.UpdatedAt = &now
|
||||
rh.SetCurrentTimeRounded(&mod.UpdatedAt)
|
||||
|
||||
return mod, r.db().Update(r.table(), mod, "id")
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
"github.com/pkg/errors"
|
||||
@@ -162,7 +161,8 @@ func (r module) Create(mod *types.Module) (*types.Module, error) {
|
||||
var err error
|
||||
|
||||
mod.ID = factory.Sonyflake.NextID()
|
||||
mod.CreatedAt = time.Now().Truncate(time.Second)
|
||||
rh.SetCurrentTimeRounded(&mod.CreatedAt)
|
||||
mod.UpdatedAt = nil
|
||||
|
||||
if err = r.db().Insert(r.table(), mod); err != nil {
|
||||
return nil, err
|
||||
@@ -172,8 +172,7 @@ func (r module) Create(mod *types.Module) (*types.Module, error) {
|
||||
}
|
||||
|
||||
func (r module) Update(mod *types.Module) (*types.Module, error) {
|
||||
now := time.Now().Truncate(time.Second)
|
||||
mod.UpdatedAt = &now
|
||||
rh.SetCurrentTimeRounded(&mod.UpdatedAt)
|
||||
|
||||
return mod, r.db().Update(r.table(), mod, "id")
|
||||
}
|
||||
@@ -195,7 +194,6 @@ func (r module) UpdateFields(moduleID uint64, ff types.ModuleFieldSet, hasRecord
|
||||
return err
|
||||
}
|
||||
|
||||
now := time.Now().Truncate(time.Second)
|
||||
for idx, f := range ff {
|
||||
if e := existing.FindByID(f.ID); e != nil {
|
||||
f.CreatedAt = e.CreatedAt
|
||||
@@ -207,7 +205,7 @@ func (r module) UpdateFields(moduleID uint64, ff types.ModuleFieldSet, hasRecord
|
||||
f.Name = e.Name
|
||||
f.Kind = e.Kind
|
||||
} else {
|
||||
f.UpdatedAt = &now
|
||||
rh.SetCurrentTimeRounded(&f.UpdatedAt)
|
||||
}
|
||||
} else {
|
||||
f.ID = 0
|
||||
@@ -215,8 +213,7 @@ func (r module) UpdateFields(moduleID uint64, ff types.ModuleFieldSet, hasRecord
|
||||
|
||||
if f.ID == 0 {
|
||||
f.ID = factory.Sonyflake.NextID()
|
||||
f.CreatedAt = now
|
||||
f.UpdatedAt = nil
|
||||
rh.SetCurrentTimeRounded(&f.CreatedAt)
|
||||
}
|
||||
|
||||
f.ModuleID = moduleID
|
||||
|
||||
@@ -3,7 +3,6 @@ package repository
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/titpetric/factory"
|
||||
"gopkg.in/Masterminds/squirrel.v1"
|
||||
@@ -137,14 +136,14 @@ func (r *namespace) Find(filter types.NamespaceFilter) (set types.NamespaceSet,
|
||||
|
||||
func (r *namespace) Create(mod *types.Namespace) (*types.Namespace, error) {
|
||||
mod.ID = factory.Sonyflake.NextID()
|
||||
mod.CreatedAt = time.Now()
|
||||
rh.SetCurrentTimeRounded(&mod.CreatedAt)
|
||||
mod.UpdatedAt = nil
|
||||
|
||||
return mod, r.db().Insert(r.table(), mod)
|
||||
}
|
||||
|
||||
func (r *namespace) Update(mod *types.Namespace) (*types.Namespace, error) {
|
||||
now := time.Now()
|
||||
mod.UpdatedAt = &now
|
||||
rh.SetCurrentTimeRounded(&mod.UpdatedAt)
|
||||
|
||||
return mod, r.db().Update(r.table(), mod, "id")
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@ package repository
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/titpetric/factory"
|
||||
"gopkg.in/Masterminds/squirrel.v1"
|
||||
@@ -195,14 +194,14 @@ func (r page) Reorder(namespaceID, parentID uint64, pageIDs []uint64) error {
|
||||
|
||||
func (r page) Create(mod *types.Page) (*types.Page, error) {
|
||||
mod.ID = factory.Sonyflake.NextID()
|
||||
mod.CreatedAt = time.Now().Truncate(time.Second)
|
||||
rh.SetCurrentTimeRounded(&mod.CreatedAt)
|
||||
mod.UpdatedAt = nil
|
||||
|
||||
return mod, r.db().Insert(r.table(), mod)
|
||||
}
|
||||
|
||||
func (r page) Update(mod *types.Page) (*types.Page, error) {
|
||||
now := time.Now().Truncate(time.Second)
|
||||
mod.UpdatedAt = &now
|
||||
rh.SetCurrentTimeRounded(&mod.UpdatedAt)
|
||||
|
||||
return mod, r.db().Update(r.table(), mod, "id")
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"github.com/titpetric/factory"
|
||||
|
||||
"github.com/cortezaproject/corteza-server/messaging/types"
|
||||
"github.com/cortezaproject/corteza-server/pkg/rh"
|
||||
)
|
||||
|
||||
type (
|
||||
@@ -144,7 +145,8 @@ func (r *channel) Find(filter *types.ChannelFilter) (types.ChannelSet, error) {
|
||||
|
||||
func (r *channel) Create(mod *types.Channel) (*types.Channel, error) {
|
||||
mod.ID = factory.Sonyflake.NextID()
|
||||
mod.CreatedAt = time.Now()
|
||||
|
||||
rh.SetCurrentTimeRounded(&mod.CreatedAt)
|
||||
mod.UpdatedAt = nil
|
||||
|
||||
if mod.Type == "" {
|
||||
@@ -155,7 +157,8 @@ func (r *channel) Create(mod *types.Channel) (*types.Channel, error) {
|
||||
}
|
||||
|
||||
func (r *channel) Update(mod *types.Channel) (*types.Channel, error) {
|
||||
mod.UpdatedAt = timeNowPtr()
|
||||
rh.SetCurrentTimeRounded(&mod.UpdatedAt)
|
||||
|
||||
if mod.Type == "" {
|
||||
mod.Type = types.ChannelTypePublic
|
||||
}
|
||||
|
||||
@@ -2,11 +2,11 @@ package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/titpetric/factory"
|
||||
|
||||
"github.com/cortezaproject/corteza-server/messaging/types"
|
||||
"github.com/cortezaproject/corteza-server/pkg/rh"
|
||||
)
|
||||
|
||||
type (
|
||||
@@ -86,7 +86,7 @@ func (r *channelMember) Find(filter *types.ChannelMemberFilter) (types.ChannelMe
|
||||
|
||||
// Create adds channel membership record
|
||||
func (r *channelMember) Create(mod *types.ChannelMember) (*types.ChannelMember, error) {
|
||||
mod.CreatedAt = time.Now()
|
||||
rh.SetCurrentTimeRounded(&mod.CreatedAt)
|
||||
mod.UpdatedAt = nil
|
||||
|
||||
return mod, r.db().Insert("messaging_channel_member", mod)
|
||||
@@ -94,7 +94,7 @@ func (r *channelMember) Create(mod *types.ChannelMember) (*types.ChannelMember,
|
||||
|
||||
// Update modifies existing channel membership record
|
||||
func (r *channelMember) Update(mod *types.ChannelMember) (*types.ChannelMember, error) {
|
||||
mod.UpdatedAt = timeNowPtr()
|
||||
rh.SetCurrentTimeRounded(&mod.UpdatedAt)
|
||||
|
||||
whitelist := []string{"type", "flag", "updated_at", "rel_channel", "rel_user"}
|
||||
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"github.com/titpetric/factory"
|
||||
|
||||
"github.com/cortezaproject/corteza-server/messaging/types"
|
||||
"github.com/cortezaproject/corteza-server/pkg/rh"
|
||||
)
|
||||
|
||||
type (
|
||||
@@ -308,13 +309,13 @@ func (r *message) sanitizeFilter(filter *types.MessageFilter) {
|
||||
|
||||
func (r *message) Create(mod *types.Message) (*types.Message, error) {
|
||||
mod.ID = factory.Sonyflake.NextID()
|
||||
mod.CreatedAt = time.Now()
|
||||
rh.SetCurrentTimeRounded(&mod.CreatedAt)
|
||||
|
||||
return mod, r.db().Insert("messaging_message", mod)
|
||||
}
|
||||
|
||||
func (r *message) Update(mod *types.Message) (*types.Message, error) {
|
||||
mod.UpdatedAt = timeNowPtr()
|
||||
rh.SetCurrentTimeRounded(&mod.UpdatedAt)
|
||||
|
||||
return mod, r.db().Replace("messaging_message", mod)
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ package repository
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
func (r repository) updateColumnByID(tableName, columnName string, value interface{}, id uint64) (err error) {
|
||||
@@ -26,8 +25,3 @@ func isFound(err error, valid bool, nerr error) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func timeNowPtr() *time.Time {
|
||||
n := time.Now()
|
||||
return &n
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"github.com/titpetric/factory"
|
||||
|
||||
"github.com/cortezaproject/corteza-server/messaging/types"
|
||||
"github.com/cortezaproject/corteza-server/pkg/rh"
|
||||
)
|
||||
|
||||
type (
|
||||
@@ -52,7 +53,7 @@ func (r *webhook) Create(webhook *types.Webhook) (*types.Webhook, error) {
|
||||
}
|
||||
|
||||
func (r *webhook) Update(webhook *types.Webhook) (*types.Webhook, error) {
|
||||
webhook.UpdatedAt = timeNowPtr()
|
||||
rh.SetCurrentTimeRounded(&webhook.UpdatedAt)
|
||||
|
||||
return webhook, errors.WithStack(r.db().Replace(r.webhook, webhook))
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
package rh
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
now = func() time.Time {
|
||||
return time.Now()
|
||||
}
|
||||
)
|
||||
|
||||
// SetCurrentTimeRounded sets current time (rounded to seconds) to a given ptr
|
||||
func SetCurrentTimeRounded(v interface{}) {
|
||||
n := now().Truncate(time.Second)
|
||||
|
||||
switch t := v.(type) {
|
||||
case *time.Time:
|
||||
*t = n
|
||||
case **time.Time:
|
||||
_ = t
|
||||
*t = &n
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package rh
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestNow(t *testing.T) {
|
||||
var (
|
||||
r = require.New(t)
|
||||
|
||||
val time.Time
|
||||
ptr *time.Time
|
||||
inv1 int
|
||||
inv2 string
|
||||
)
|
||||
|
||||
SetCurrentTimeRounded(&val)
|
||||
r.NotEmpty(val)
|
||||
|
||||
SetCurrentTimeRounded(&ptr)
|
||||
r.NotNil(ptr)
|
||||
|
||||
SetCurrentTimeRounded(&inv1)
|
||||
r.Empty(inv1)
|
||||
|
||||
SetCurrentTimeRounded(&inv2)
|
||||
r.Empty(inv2)
|
||||
|
||||
}
|
||||
@@ -122,7 +122,7 @@ func (r *application) Create(mod *types.Application) (*types.Application, error)
|
||||
}
|
||||
|
||||
func (r *application) Update(mod *types.Application) (*types.Application, error) {
|
||||
mod.UpdatedAt = timeNowPtr()
|
||||
rh.SetCurrentTimeRounded(&mod.UpdatedAt)
|
||||
|
||||
return mod, r.db().Replace(r.table(), mod)
|
||||
}
|
||||
|
||||
@@ -81,7 +81,7 @@ func (r *organisation) Create(mod *types.Organisation) (*types.Organisation, err
|
||||
}
|
||||
|
||||
func (r *organisation) Update(mod *types.Organisation) (*types.Organisation, error) {
|
||||
mod.UpdatedAt = timeNowPtr()
|
||||
rh.SetCurrentTimeRounded(&mod.UpdatedAt)
|
||||
|
||||
return mod, r.db().Replace(r.organisations, mod)
|
||||
}
|
||||
|
||||
@@ -158,7 +158,7 @@ func (r reminder) Create(mod *types.Reminder) (rm *types.Reminder, err error) {
|
||||
}
|
||||
|
||||
func (r reminder) Update(mod *types.Reminder) (*types.Reminder, error) {
|
||||
mod.UpdatedAt = timeNowPtr()
|
||||
rh.SetCurrentTimeRounded(&mod.UpdatedAt)
|
||||
return mod, r.db().Replace(r.table(), mod)
|
||||
}
|
||||
|
||||
|
||||
@@ -188,7 +188,7 @@ func (r *role) Create(mod *types.Role) (*types.Role, error) {
|
||||
}
|
||||
|
||||
func (r *role) Update(mod *types.Role) (*types.Role, error) {
|
||||
mod.UpdatedAt = timeNowPtr()
|
||||
rh.SetCurrentTimeRounded(&mod.UpdatedAt)
|
||||
|
||||
return mod, r.db().Replace(r.table(), mod)
|
||||
}
|
||||
|
||||
@@ -217,7 +217,7 @@ func (r *user) Create(mod *types.User) (*types.User, error) {
|
||||
}
|
||||
|
||||
func (r *user) Update(mod *types.User) (*types.User, error) {
|
||||
mod.UpdatedAt = timeNowPtr()
|
||||
rh.SetCurrentTimeRounded(&mod.UpdatedAt)
|
||||
return mod, r.db().Replace(r.table(), mod)
|
||||
}
|
||||
|
||||
|
||||
@@ -2,7 +2,6 @@ package repository
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
@@ -17,8 +16,3 @@ func (r repository) updateColumnByID(tableName, columnName string, value interfa
|
||||
func exec(_ interface{}, err error) error {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
|
||||
func timeNowPtr() *time.Time {
|
||||
n := time.Now()
|
||||
return &n
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user