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"
"log"
"os"
"time"
"crawshaw.io/sqlite"
"crawshaw.io/sqlite/sqlitex"
"github.com/rickb777/date"
)
type GenString func(length int) (string, error)
@ -120,7 +120,7 @@ func (s *Store) createSchema() error {
type CreateEventCommand struct {
Name string
Description string
EarliestDate, LatestDate time.Time
Earliest, Latest date.Date
}
type CreateEventResult struct {
@ -154,8 +154,8 @@ func (s *Store) CreateEvent(ctx context.Context, cmd CreateEventCommand) (result
adminCode,
cmd.Name,
cmd.Description,
cmd.EarliestDate.Format(dbDateLayout),
cmd.LatestDate.Format(dbDateLayout),
cmd.Earliest.Format(dbDateLayout),
cmd.Latest.Format(dbDateLayout),
)
if err != nil {
return
@ -173,7 +173,7 @@ type GetEventMetadataQuery struct {
type GetEventMetadataResult struct {
Name string
Description string
EarliestDate, LatestDate time.Time
Earliest, Latest date.Date
}
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)
var err error
result.EarliestDate, err = time.Parse(dbDateLayout, earliestDateString)
result.Earliest, err = date.Parse(dbDateLayout, earliestDateString)
if err != nil {
return err
}
result.LatestDate, err = time.Parse(dbDateLayout, latestDateString)
result.Latest, err = date.Parse(dbDateLayout, latestDateString)
return err
}, query.AlphaID)
if err != nil {
@ -242,7 +242,7 @@ func (s *Store) GetEventResponses(ctx context.Context, query GetEventResponsesQu
type CreateEventResponseCommand struct {
EventAlphaID string
DateHours map[time.Time]int
DateHours map[date.Date]int
}
type CreateEventResponseResult struct {

View File

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

View File

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

1
go.mod
View File

@ -5,5 +5,6 @@ go 1.14
require (
crawshaw.io/sqlite v0.2.5
github.com/matryer/is v1.3.0
github.com/rickb777/date v1.12.4
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/matryer/is v1.3.0 h1:9qiso3jaJrOe6qBRJRBt2Ldht05qDiFP9le0JOIhRSI=
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/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=