Setting created & update cols.

This commit is contained in:
Matthew Dillon 2014-11-02 20:48:10 -09:00
parent f48ee87ccd
commit bedddfdec1
4 changed files with 26 additions and 2 deletions

View file

@ -2,6 +2,7 @@ package datastore
import (
"strings"
"time"
"github.com/thermokarst/bactdb/models"
)
@ -26,6 +27,9 @@ func (s *generaStore) Get(id int64) (*models.Genus, error) {
}
func (s *generaStore) Create(genus *models.Genus) (bool, error) {
currentTime := time.Now()
genus.CreatedAt = currentTime
genus.UpdatedAt = currentTime
if err := s.dbh.Insert(genus); err != nil {
if strings.Contains(err.Error(), `violates unique constraint "genus_idx"`) {
return false, err
@ -56,6 +60,7 @@ func (s *generaStore) Update(id int64, genus *models.Genus) (bool, error) {
return false, models.ErrGenusNotFound
}
genus.UpdatedAt = time.Now()
changed, err := s.dbh.Update(genus)
if err != nil {
return false, err

View file

@ -1,6 +1,10 @@
package datastore
import "github.com/thermokarst/bactdb/models"
import (
"time"
"github.com/thermokarst/bactdb/models"
)
func init() {
DB.AddTableWithName(models.Species{}, "species").SetKeys(true, "Id")
@ -22,6 +26,9 @@ func (s *speciesStore) Get(id int64) (*models.Species, error) {
}
func (s *speciesStore) Create(species *models.Species) (bool, error) {
currentTime := time.Now()
species.CreatedAt = currentTime
species.UpdatedAt = currentTime
if err := s.dbh.Insert(species); err != nil {
return false, err
}
@ -50,6 +57,7 @@ func (s *speciesStore) Update(id int64, species *models.Species) (bool, error) {
return false, models.ErrSpeciesNotFound
}
species.UpdatedAt = time.Now()
changed, err := s.dbh.Update(species)
if err != nil {
return false, err

View file

@ -1,6 +1,10 @@
package datastore
import "github.com/thermokarst/bactdb/models"
import (
"time"
"github.com/thermokarst/bactdb/models"
)
func init() {
DB.AddTableWithName(models.Strain{}, "strains").SetKeys(true, "Id")
@ -22,6 +26,9 @@ func (s *strainsStore) Get(id int64) (*models.Strain, error) {
}
func (s *strainsStore) Create(strain *models.Strain) (bool, error) {
currentTime := time.Now()
strain.CreatedAt = currentTime
strain.UpdatedAt = currentTime
if err := s.dbh.Insert(strain); err != nil {
return false, err
}
@ -50,6 +57,7 @@ func (s *strainsStore) Update(id int64, strain *models.Strain) (bool, error) {
return false, models.ErrStrainNotFound
}
strain.UpdatedAt = time.Now()
changed, err := s.dbh.Update(strain)
if err != nil {
return false, err

View file

@ -32,6 +32,9 @@ func (s *usersStore) Get(id int64) (*models.User, error) {
func (s *usersStore) Create(user *models.User) (bool, error) {
retries := 3
var wantRetry bool
currentTime := time.Now()
user.CreatedAt = currentTime
user.UpdatedAt = currentTime
retry:
retries--