diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c50ada2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*.db +*.db-shm +*.db-wal \ No newline at end of file diff --git a/http/server.go b/http/server.go index d19dfb5..1b91368 100644 --- a/http/server.go +++ b/http/server.go @@ -57,18 +57,22 @@ func pageRoot() h.Term { } const ( - fieldNameEarliest = "earliestDate" - fieldNameLatest = "latestDate" - fieldNameEventName = "eventName" + fieldNameEarliest = "earliestDate" + fieldNameLatest = "latestDate" + fieldNameEventName = "eventName" + fieldNameDescription = "eventDescription" ) func pageCreate() h.Term { return h.Terms{ e.H2()(h.Text("Create an event")), - e.Form(a.Action(pathDoCreate), a.Method("POST"))( + e.Form(a.Action(pathDoCreate), a.Method(http.MethodPost))( e.Label(a.For(fieldNameEventName))(h.Text("Event name")), e.Input(a.Name(fieldNameEventName)), + e.Label(a.For(fieldNameDescription))(h.Text("Description")), + e.Textarea(a.Name(fieldNameEventName), a.Placeholder("What's going on?"))(), + e.Label(a.For(fieldNameEarliest))(h.Text("Earliest date")), e.Input(a.Name(fieldNameEarliest), a.Type("date")), @@ -80,16 +84,56 @@ func pageCreate() h.Term { } } -func pageAdmin() h.Term { - return h.Text("check the log") +func pageAdmin(alphaID, adminCode string) h.Term { + event, err := store.GetEvent(context.Background(), henwen.GetEventQuery{ + AlphaID: alphaID, + AdminCode: adminCode, + }) + if err != nil { + return h.Text(err.Error()) + } + return h.Terms{ + e.H2()(h.Text("Edit your event")), + e.Form(a.Action(pathDoCreate), a.Method(http.MethodPost))( + e.Label(a.For(fieldNameEventName))(h.Text("Event name")), + e.Input( + a.Name(fieldNameEventName), + a.Value(event.Name), + ), + e.Br(), + + e.Label(a.For(fieldNameDescription))(h.Text("Description")), + e.Textarea(a.Name(fieldNameEventName))(h.Text(event.Description)), + e.Br(), + + e.Label(a.For(fieldNameEarliest))(h.Text("Earliest date")), + e.Input( + a.Name(fieldNameEarliest), + a.Type("date"), + a.Value(event.EarliestDate.Format(dateFmt)), + ), + e.Br(), + + e.Label(a.For(fieldNameLatest))(h.Text("Latest date")), + e.Input( + a.Name(fieldNameLatest), + a.Type("date"), + a.Value(event.LatestDate.Format(dateFmt)), + ), + e.Br(), + + e.Input(a.Type("submit")), + ), + } } const keyEventID = "event_id" const keyAdminCode = "admin_code" -func pageDoCreate(name string, earliest, latest time.Time) h.Term { +func pageDoCreate(name, description string, earliest, latest time.Time) h.Term { event, err := store.CreateEvent(context.Background(), henwen.CreateEventCommand{ Name: name, + Description: description, EarliestDate: earliest, LatestDate: latest, Duration: 1, @@ -102,6 +146,8 @@ func pageDoCreate(name string, earliest, latest time.Time) h.Term { adminQuery.Add(keyAdminCode, event.AdminCode) adminURL := baseURL + pathAdmin + "?" + adminQuery.Encode() + const dateDisplayFmt = "Monday, January 2, 2006" + return h.Terms{ e.H2()(h.Text("Created event!")), e.P()( @@ -114,10 +160,10 @@ func pageDoCreate(name string, earliest, latest time.Time) h.Term { h.Text(name), e.H3()(h.Text("Earliest date")), - h.Text(earliest.Format(time.ANSIC)), + h.Text(earliest.Format(dateDisplayFmt)), e.H3()(h.Text("Latest date")), - h.Text(latest.Format(time.ANSIC)), + h.Text(latest.Format(dateDisplayFmt)), } } @@ -142,6 +188,8 @@ func genString() (string, error) { return buf.String(), nil } +const dateFmt = "2006-01-02" + func main() { var err error store, err = henwen.NewStore(dbFileName, genString) @@ -157,7 +205,6 @@ func main() { _ = writePage(w, pageCreate()) }) mux.HandleFunc(pathDoCreate, func(w http.ResponseWriter, r *http.Request) { - const dateFmt = "2006-01-02" earliest, err := time.Parse(dateFmt, r.FormValue(fieldNameEarliest)) if err != nil { fmt.Fprint(w, "bad earliest date") @@ -173,20 +220,13 @@ func main() { fmt.Fprint(w, "event name is required") return } + description := r.FormValue(fieldNameDescription) - _ = writePage(w, pageDoCreate(eventName, earliest, latest)) + _ = writePage(w, pageDoCreate(eventName, description, earliest, latest)) }) mux.HandleFunc(pathAdmin, func(w http.ResponseWriter, r *http.Request) { query := r.URL.Query() - err := store.GetEvent(context.Background(), henwen.GetEventQuery{ - AlphaID: query.Get(keyEventID), - AdminCode: query.Get(keyAdminCode), - }) - if err != nil { - fmt.Fprint(w, err.Error()) - return - } - _ = writePage(w, pageAdmin()) + _ = writePage(w, pageAdmin(query.Get(keyEventID), query.Get(keyAdminCode))) }) srv := http.Server{ diff --git a/store.go b/store.go index 828d896..44d2b27 100644 --- a/store.go +++ b/store.go @@ -3,7 +3,6 @@ package henwen import ( "context" "errors" - "fmt" "log" "os" "time" @@ -61,6 +60,7 @@ const schema = ` alpha_id TEXT NOT NULL, admin_code TEXT NOT NULL, name TEXT NOT NULL, + description TEXT NOT NULL, earliest_date DATE NOT NULL, latest_date DATE NOT NULL, duration INTEGER NOT NULL, @@ -92,6 +92,7 @@ func (s *Store) createSchema() error { type CreateEventCommand struct { Name string + Description string EarliestDate, LatestDate time.Time Duration int } @@ -100,6 +101,8 @@ type CreateEventResult struct { AlphaID, AdminCode string } +const dateFmt = "2006-01-02" + func (s *Store) CreateEvent(ctx context.Context, cmd CreateEventCommand) (result CreateEventResult, err error) { conn := s.pool.Get(ctx) defer s.pool.Put(conn) @@ -114,14 +117,14 @@ func (s *Store) CreateEvent(ctx context.Context, cmd CreateEventCommand) (result return } - const dateFmt = "2006-01-02" const query = ` - INSERT INTO event(alpha_id, admin_code, name, earliest_date, latest_date, duration) - VALUES (?, ?, ?, ?, ?, ?);` + INSERT INTO event(alpha_id, admin_code, name, description, earliest_date, latest_date, duration) + VALUES (?, ?, ?, ?, ?, ?, ?);` err = sqlitex.Exec(conn, query, nil, alphaID, adminCode, cmd.Name, + cmd.Description, cmd.EarliestDate.Format(dateFmt), cmd.LatestDate.Format(dateFmt), cmd.Duration, @@ -141,33 +144,44 @@ type GetEventQuery struct { type GetEventResult struct { Name string + Description string EarliestDate, LatestDate time.Time Duration int } -func (s *Store) GetEvent(ctx context.Context, query GetEventQuery) error { +func (s *Store) GetEvent(ctx context.Context, query GetEventQuery) (GetEventResult, error) { conn := s.pool.Get(ctx) defer s.pool.Put(conn) const dbQuery = ` - SELECT name, earliest_date, latest_date, duration + SELECT name, description, earliest_date, latest_date, duration FROM event - WHERE alpha_id = ? AND admin_code = ?;` + WHERE alpha_id = ?;` + var result GetEventResult var found bool err := sqlitex.Exec(conn, dbQuery, func(stmt *sqlite.Stmt) error { found = true - fmt.Printf("Name: %s\n", stmt.ColumnText(0)) - fmt.Printf("Earliest date: %s\n", stmt.ColumnText(1)) - fmt.Printf("Latest date: %s\n", stmt.ColumnText(2)) - fmt.Printf("Duration: %d\n", stmt.ColumnInt(3)) - return nil - }, query.AlphaID, query.AdminCode) + + result.Name = stmt.ColumnText(0) + result.Description = stmt.ColumnText(1) + earliestDateString := stmt.ColumnText(2) + latestDateString := stmt.ColumnText(3) + result.Duration = stmt.ColumnInt(4) + + var err error + result.EarliestDate, err = time.Parse(dateFmt, earliestDateString) + if err != nil { + return err + } + result.LatestDate, err = time.Parse(dateFmt, latestDateString) + return err + }, query.AlphaID) if err != nil { - return err + return GetEventResult{}, err } if !found { - return errors.New("not found") + return GetEventResult{}, errors.New("not found") } - return nil + return result, nil }