From 58843133f52f3fc5a564043eb12a98db326349cf Mon Sep 17 00:00:00 2001 From: Matthew Dillon Date: Mon, 1 Jun 2015 15:27:17 -0800 Subject: [PATCH] Handle nulltimes --- helpers.go | 15 +++++++++++++++ strains.go | 16 ++++------------ 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/helpers.go b/helpers.go index c3a846f..c2773e1 100644 --- a/helpers.go +++ b/helpers.go @@ -1,5 +1,11 @@ package main +import ( + "time" + + "github.com/lib/pq" +) + // ListOptions specifies general pagination options for fetching a list of results type ListOptions struct { PerPage int `url:",omitempty" json:",omitempty"` @@ -27,3 +33,12 @@ func (o ListOptions) PerPageOrDefault() int { // DefaultPerPage is the default number of items to return in a paginated result set const DefaultPerPage = 10 + +func currentTime() NullTime { + return NullTime{ + pq.NullTime{ + Time: time.Now(), + Valid: true, + }, + } +} diff --git a/strains.go b/strains.go index b72883a..131916f 100644 --- a/strains.go +++ b/strains.go @@ -8,7 +8,6 @@ import ( "net/http" "strconv" "strings" - "time" "github.com/gorilla/context" "github.com/gorilla/mux" @@ -33,8 +32,8 @@ type StrainBase struct { Genbank NullString `db:"genbank" json:"genbank"` IsolatedFrom NullString `db:"isolated_from" json:"isolatedFrom"` Notes NullString `db:"notes" json:"notes"` - CreatedAt time.Time `db:"created_at" json:"createdAt"` - UpdatedAt time.Time `db:"updated_at" json:"updatedAt"` + CreatedAt NullTime `db:"created_at" json:"createdAt"` + UpdatedAt NullTime `db:"updated_at" json:"updatedAt"` DeletedAt NullTime `db:"deleted_at" json:"deletedAt"` CreatedBy int64 `db:"created_by" json:"createdBy"` UpdatedBy int64 `db:"updated_by" json:"updatedBy"` @@ -126,7 +125,7 @@ func serveUpdateStrain(w http.ResponseWriter, r *http.Request) { c := context.Get(r, "claims") var claims Claims = c.(Claims) strainjson.Strain.UpdatedBy = claims.Sub - strainjson.Strain.UpdatedAt = time.Now() + strainjson.Strain.UpdatedAt = currentTime() strainjson.Strain.Id = id err = dbUpdateStrain(strainjson.Strain) @@ -135,14 +134,7 @@ func serveUpdateStrain(w http.ResponseWriter, r *http.Request) { return } - var strain *Strain - strain, err = dbGetStrain(id, mux.Vars(r)["genus"]) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - - data, err := json.Marshal(StrainJSON{Strain: strain}) + data, err := json.Marshal(StrainJSON{Strain: strainjson.Strain}) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return