Use Date instead of Time for dates

This commit is contained in:
Brandon Dyck 2020-04-21 08:25:15 -06:00
parent 99dd0df77a
commit c4aaa1bca5
5 changed files with 52 additions and 43 deletions

View File

@ -5,10 +5,10 @@ import (
"errors" "errors"
"log" "log"
"os" "os"
"time"
"crawshaw.io/sqlite" "crawshaw.io/sqlite"
"crawshaw.io/sqlite/sqlitex" "crawshaw.io/sqlite/sqlitex"
"github.com/rickb777/date"
) )
type GenString func(length int) (string, error) type GenString func(length int) (string, error)
@ -118,9 +118,9 @@ func (s *Store) createSchema() error {
} }
type CreateEventCommand struct { type CreateEventCommand struct {
Name string Name string
Description string Description string
EarliestDate, LatestDate time.Time Earliest, Latest date.Date
} }
type CreateEventResult struct { type CreateEventResult struct {
@ -154,8 +154,8 @@ func (s *Store) CreateEvent(ctx context.Context, cmd CreateEventCommand) (result
adminCode, adminCode,
cmd.Name, cmd.Name,
cmd.Description, cmd.Description,
cmd.EarliestDate.Format(dbDateLayout), cmd.Earliest.Format(dbDateLayout),
cmd.LatestDate.Format(dbDateLayout), cmd.Latest.Format(dbDateLayout),
) )
if err != nil { if err != nil {
return return
@ -171,9 +171,9 @@ type GetEventMetadataQuery struct {
} }
type GetEventMetadataResult struct { type GetEventMetadataResult struct {
Name string Name string
Description string Description string
EarliestDate, LatestDate time.Time Earliest, Latest date.Date
} }
func (s *Store) GetEventMetadata(ctx context.Context, query GetEventMetadataQuery) (GetEventMetadataResult, error) { func (s *Store) GetEventMetadata(ctx context.Context, query GetEventMetadataQuery) (GetEventMetadataResult, error) {
@ -196,11 +196,11 @@ func (s *Store) GetEventMetadata(ctx context.Context, query GetEventMetadataQuer
latestDateString := stmt.ColumnText(3) latestDateString := stmt.ColumnText(3)
var err error var err error
result.EarliestDate, err = time.Parse(dbDateLayout, earliestDateString) result.Earliest, err = date.Parse(dbDateLayout, earliestDateString)
if err != nil { if err != nil {
return err return err
} }
result.LatestDate, err = time.Parse(dbDateLayout, latestDateString) result.Latest, err = date.Parse(dbDateLayout, latestDateString)
return err return err
}, query.AlphaID) }, query.AlphaID)
if err != nil { if err != nil {
@ -242,7 +242,7 @@ func (s *Store) GetEventResponses(ctx context.Context, query GetEventResponsesQu
type CreateEventResponseCommand struct { type CreateEventResponseCommand struct {
EventAlphaID string EventAlphaID string
DateHours map[time.Time]int DateHours map[date.Date]int
} }
type CreateEventResponseResult struct { type CreateEventResponseResult struct {

View File

@ -3,9 +3,9 @@ package back_test
import ( import (
"context" "context"
"testing" "testing"
"time"
"github.com/matryer/is" "github.com/matryer/is"
"github.com/rickb777/date"
"gitlab.codemonkeysoftware.net/b/henwen/back" "gitlab.codemonkeysoftware.net/b/henwen/back"
) )
@ -23,15 +23,15 @@ func TestCreateEvent(t *testing.T) {
is.NoErr(err) is.NoErr(err)
defer store.Close() defer store.Close()
earliest := time.Date(2020, 04, 05, 0, 0, 0, 0, time.UTC) earliest := date.New(2020, 4, 5)
latest := time.Date(2020, 04, 05, 0, 0, 0, 0, time.UTC) latest := date.New(2020, 4, 5)
const name = "abc" const name = "abc"
const description = "def" const description = "def"
createResult, err := store.CreateEvent(context.Background(), back.CreateEventCommand{ createResult, err := store.CreateEvent(context.Background(), back.CreateEventCommand{
Name: name, Name: name,
Description: description, Description: description,
EarliestDate: earliest, Earliest: earliest,
LatestDate: latest, Latest: latest,
}) })
is.NoErr(err) is.NoErr(err)
@ -41,8 +41,8 @@ func TestCreateEvent(t *testing.T) {
is.NoErr(err) is.NoErr(err)
is.Equal(metadataResult.Name, name) is.Equal(metadataResult.Name, name)
is.Equal(metadataResult.Description, description) is.Equal(metadataResult.Description, description)
is.True(metadataResult.EarliestDate.Equal(earliest)) is.True(metadataResult.Earliest.Equal(earliest))
is.True(metadataResult.LatestDate.Equal(latest)) is.True(metadataResult.Latest.Equal(latest))
} }
func TestGetEventResponses(t *testing.T) { func TestGetEventResponses(t *testing.T) {
@ -51,10 +51,10 @@ func TestGetEventResponses(t *testing.T) {
createEvent := func(is *is.I) (eventID string) { createEvent := func(is *is.I) (eventID string) {
event, err := store.CreateEvent(context.Background(), back.CreateEventCommand{ event, err := store.CreateEvent(context.Background(), back.CreateEventCommand{
Name: "blah", Name: "blah",
Description: "stuff happening", Description: "stuff happening",
EarliestDate: time.Now(), Earliest: date.Today(),
LatestDate: time.Now().AddDate(0,0,1), Latest: date.Today().Add(1),
}) })
is.NoErr(err) is.NoErr(err)
return event.AlphaID return event.AlphaID
@ -98,4 +98,4 @@ func TestGetEventResponses(t *testing.T) {
is.Equal(getTotalResponses(is, eventID), 1) is.Equal(getTotalResponses(is, eventID), 1)
}) })
} }

View File

@ -9,6 +9,7 @@ import (
"strconv" "strconv"
"time" "time"
"github.com/rickb777/date"
hm "gitlab.codemonkeysoftware.net/b/hatmill" hm "gitlab.codemonkeysoftware.net/b/hatmill"
a "gitlab.codemonkeysoftware.net/b/hatmill/attribute" a "gitlab.codemonkeysoftware.net/b/hatmill/attribute"
e "gitlab.codemonkeysoftware.net/b/hatmill/element" e "gitlab.codemonkeysoftware.net/b/hatmill/element"
@ -84,14 +85,15 @@ func disableIf(cond bool, input hm.Term) hm.Term {
} }
type voteState struct { type voteState struct {
name string name string
earliestDate, latestDate time.Time earliest, latest date.Date
} }
func voteForm(disabled bool, st voteState) hm.Term { func voteForm(disabled bool, st voteState) hm.Term {
var dates []time.Time dateSpan := st.latest.Sub(st.earliest)
for curr := st.earliestDate; curr.Before(st.latestDate.Add(time.Hour)); curr = curr.AddDate(0, 0, 1) { var dates []date.Date
dates = append(dates, curr) for offset := date.PeriodOfDays(0); offset <= dateSpan; offset++ {
dates = append(dates, st.earliest.Add(offset))
} }
var ths = hm.Terms{e.Th()()} var ths = hm.Terms{e.Th()()}
@ -133,8 +135,8 @@ func (h *handler) handleVote(w http.ResponseWriter, r *http.Request) {
} }
state := voteState{ state := voteState{
earliestDate: event.EarliestDate, earliest: event.Earliest,
latestDate: event.LatestDate, latest: event.Latest,
} }
body := hm.Terms{ body := hm.Terms{
e.P()(hm.Text(event.Description)), e.P()(hm.Text(event.Description)),
@ -148,9 +150,9 @@ func (h *handler) handleDoVote(w http.ResponseWriter, r *http.Request) {
// TODO Use actual data. // TODO Use actual data.
state := voteState{ state := voteState{
name: "Suzie Q", name: "Suzie Q",
earliestDate: time.Date(2006, time.May, 3, 0, 0, 0, 0, time.UTC), earliest: date.New(2006, time.May, 3),
latestDate: time.Date(2006, time.May, 8, 0, 0, 0, 0, time.UTC), latest: date.New(2006, time.May, 8),
} }
body := hm.Terms{ body := hm.Terms{
e.P()(hm.Text("At Billy's house. Bring presents. Eat cake.")), e.P()(hm.Text("At Billy's house. Bring presents. Eat cake.")),
@ -188,12 +190,12 @@ func (h *handler) handleCreate(w http.ResponseWriter, r *http.Request) {
func (h *handler) handleDoCreate(w http.ResponseWriter, r *http.Request) { func (h *handler) handleDoCreate(w http.ResponseWriter, r *http.Request) {
// TODO consider redirecting to admin // TODO consider redirecting to admin
earliest, err := time.Parse(formDateLayout, r.FormValue(fieldNameEarliest)) earliest, err := date.Parse(formDateLayout, r.FormValue(fieldNameEarliest))
if err != nil { if err != nil {
fmt.Fprint(w, "bad earliest date") fmt.Fprint(w, "bad earliest date")
return return
} }
latest, err := time.Parse(formDateLayout, r.FormValue(fieldNameLatest)) latest, err := date.Parse(formDateLayout, r.FormValue(fieldNameLatest))
if err != nil { if err != nil {
fmt.Fprint(w, "bad latest date") fmt.Fprint(w, "bad latest date")
return return
@ -206,10 +208,10 @@ func (h *handler) handleDoCreate(w http.ResponseWriter, r *http.Request) {
description := r.FormValue(fieldNameDescription) description := r.FormValue(fieldNameDescription)
event, err := h.store.CreateEvent(context.Background(), back.CreateEventCommand{ event, err := h.store.CreateEvent(context.Background(), back.CreateEventCommand{
Name: eventName, Name: eventName,
Description: description, Description: description,
EarliestDate: earliest, Earliest: earliest,
LatestDate: latest, Latest: latest,
}) })
if err != nil { if err != nil {
fmt.Fprint(w, err) fmt.Fprint(w, err)
@ -292,7 +294,7 @@ func (h *handler) handleAdmin(w http.ResponseWriter, r *http.Request) {
e.Input( e.Input(
a.Name(fieldNameEarliest), a.Name(fieldNameEarliest),
a.Type("date"), a.Type("date"),
a.Value(metadata.EarliestDate.Format(formDateLayout)), a.Value(metadata.Earliest.Format(formDateLayout)),
), ),
e.Br(), e.Br(),
@ -300,7 +302,7 @@ func (h *handler) handleAdmin(w http.ResponseWriter, r *http.Request) {
e.Input( e.Input(
a.Name(fieldNameLatest), a.Name(fieldNameLatest),
a.Type("date"), a.Type("date"),
a.Value(metadata.LatestDate.Format(formDateLayout)), a.Value(metadata.Latest.Format(formDateLayout)),
), ),
e.Br(), e.Br(),

1
go.mod
View File

@ -5,5 +5,6 @@ go 1.14
require ( require (
crawshaw.io/sqlite v0.2.5 crawshaw.io/sqlite v0.2.5
github.com/matryer/is v1.3.0 github.com/matryer/is v1.3.0
github.com/rickb777/date v1.12.4
gitlab.codemonkeysoftware.net/b/hatmill v0.0.5 gitlab.codemonkeysoftware.net/b/hatmill v0.0.5
) )

6
go.sum
View File

@ -4,5 +4,11 @@ crawshaw.io/sqlite v0.2.5/go.mod h1:igAO5JulrQ1DbdZdtVq48mnZUBAPOeFzer7VhDWNtW4=
github.com/leanovate/gopter v0.2.4/go.mod h1:gNcbPWNEWRe4lm+bycKqxUYoH5uoVje5SkOJ3uoLer8= github.com/leanovate/gopter v0.2.4/go.mod h1:gNcbPWNEWRe4lm+bycKqxUYoH5uoVje5SkOJ3uoLer8=
github.com/matryer/is v1.3.0 h1:9qiso3jaJrOe6qBRJRBt2Ldht05qDiFP9le0JOIhRSI= github.com/matryer/is v1.3.0 h1:9qiso3jaJrOe6qBRJRBt2Ldht05qDiFP9le0JOIhRSI=
github.com/matryer/is v1.3.0/go.mod h1:2fLPjFQM9rhQ15aVEtbuwhJinnOqrmgXPNdZsdwlWXA= github.com/matryer/is v1.3.0/go.mod h1:2fLPjFQM9rhQ15aVEtbuwhJinnOqrmgXPNdZsdwlWXA=
github.com/rickb777/date v1.12.4 h1:+6IzcCCS/1t17DrmnEvrznyq7nM8vPwir6/UhlyohKw=
github.com/rickb777/date v1.12.4/go.mod h1:xP0eo/I5qmUt97yRGClHZfyLZ3ikMw6v6SU5MOGZTE0=
github.com/rickb777/plural v1.2.0 h1:5tvEc7UBCZ7l8h/2UeybSkt/uu1DQsZFOFdNevmUhlE=
github.com/rickb777/plural v1.2.0/go.mod h1:UdpyWFCGbo3mvK3f/PfZOAOrkjzJlYN/sD46XNWJ+Es=
gitlab.codemonkeysoftware.net/b/hatmill v0.0.5 h1:7L70G1Qmdm7bSwH58qWY5psXOwoqcvokLhc4BPSIjWo= gitlab.codemonkeysoftware.net/b/hatmill v0.0.5 h1:7L70G1Qmdm7bSwH58qWY5psXOwoqcvokLhc4BPSIjWo=
gitlab.codemonkeysoftware.net/b/hatmill v0.0.5/go.mod h1:3jggrD9qkK8kXt0d3+Kwg3zti2PfPcociz7r3oxtjWU= gitlab.codemonkeysoftware.net/b/hatmill v0.0.5/go.mod h1:3jggrD9qkK8kXt0d3+Kwg3zti2PfPcociz7r3oxtjWU=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=