Store created event
This commit is contained in:
parent
e714e66703
commit
d7cee536e7
@ -2,12 +2,14 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"context"
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"math/big"
|
"math/big"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
h "gitlab.codemonkeysoftware.net/b/hatmill"
|
h "gitlab.codemonkeysoftware.net/b/hatmill"
|
||||||
@ -23,9 +25,10 @@ const baseURL = "http://localhost:8080"
|
|||||||
const dbFileName = "./henwen.db"
|
const dbFileName = "./henwen.db"
|
||||||
|
|
||||||
const (
|
const (
|
||||||
PathRoot = "/"
|
pathRoot = "/"
|
||||||
PathCreate = "/create"
|
pathCreate = "/create"
|
||||||
PathDoCreate = "/create/do"
|
pathDoCreate = "/create/do"
|
||||||
|
pathAdmin = "/admin"
|
||||||
)
|
)
|
||||||
|
|
||||||
var store *henwen.Store
|
var store *henwen.Store
|
||||||
@ -47,7 +50,7 @@ func writePage(w io.Writer, contents h.Term) error {
|
|||||||
func pageRoot() h.Term {
|
func pageRoot() h.Term {
|
||||||
return h.Terms{
|
return h.Terms{
|
||||||
e.H2()(h.Text("Welcome!")),
|
e.H2()(h.Text("Welcome!")),
|
||||||
e.A(a.Href(PathCreate))(
|
e.A(a.Href(pathCreate))(
|
||||||
h.Text("Create event"),
|
h.Text("Create event"),
|
||||||
),
|
),
|
||||||
}
|
}
|
||||||
@ -62,7 +65,7 @@ const (
|
|||||||
func pageCreate() h.Term {
|
func pageCreate() h.Term {
|
||||||
return h.Terms{
|
return h.Terms{
|
||||||
e.H2()(h.Text("Create an event")),
|
e.H2()(h.Text("Create an event")),
|
||||||
e.Form(a.Action(PathDoCreate), a.Method("POST"))(
|
e.Form(a.Action(pathDoCreate), a.Method("POST"))(
|
||||||
e.Label(a.For(fieldNameEventName))(h.Text("Event name")),
|
e.Label(a.For(fieldNameEventName))(h.Text("Event name")),
|
||||||
e.Input(a.Name(fieldNameEventName)),
|
e.Input(a.Name(fieldNameEventName)),
|
||||||
|
|
||||||
@ -77,20 +80,27 @@ func pageCreate() h.Term {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeAdminURL(eventID, adminCode string) string {
|
func pageAdmin() h.Term {
|
||||||
return fmt.Sprintf("%s/%s/admin/%s", baseURL, eventID, adminCode)
|
return h.Text("check the log")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const keyEventID = "event_id"
|
||||||
|
const keyAdminCode = "admin_code"
|
||||||
|
|
||||||
func pageDoCreate(name string, earliest, latest time.Time) h.Term {
|
func pageDoCreate(name string, earliest, latest time.Time) h.Term {
|
||||||
id, err := genString()
|
event, err := store.CreateEvent(context.Background(), henwen.CreateEventCommand{
|
||||||
|
Name: name,
|
||||||
|
EarliestDate: earliest,
|
||||||
|
LatestDate: latest,
|
||||||
|
Duration: 1,
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return h.Text(err.Error())
|
return h.Text(err.Error())
|
||||||
}
|
}
|
||||||
adminCode, err := genString()
|
var adminQuery = make(url.Values)
|
||||||
if err != nil {
|
adminQuery.Add(keyEventID, event.AlphaID)
|
||||||
return h.Text(err.Error())
|
adminQuery.Add(keyAdminCode, event.AdminCode)
|
||||||
}
|
adminURL := baseURL + pathAdmin + "?" + adminQuery.Encode()
|
||||||
adminURL := makeAdminURL(id, adminCode)
|
|
||||||
|
|
||||||
return h.Terms{
|
return h.Terms{
|
||||||
e.H2()(h.Text("Created event!")),
|
e.H2()(h.Text("Created event!")),
|
||||||
@ -133,14 +143,20 @@ func genString() (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
var err error
|
||||||
|
store, err = henwen.NewStore(dbFileName, genString)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
mux := http.NewServeMux()
|
mux := http.NewServeMux()
|
||||||
mux.HandleFunc(PathRoot, func(w http.ResponseWriter, r *http.Request) {
|
mux.HandleFunc(pathRoot, func(w http.ResponseWriter, r *http.Request) {
|
||||||
_ = writePage(w, pageRoot())
|
_ = writePage(w, pageRoot())
|
||||||
})
|
})
|
||||||
mux.HandleFunc(PathCreate, func(w http.ResponseWriter, r *http.Request) {
|
mux.HandleFunc(pathCreate, func(w http.ResponseWriter, r *http.Request) {
|
||||||
_ = writePage(w, pageCreate())
|
_ = writePage(w, pageCreate())
|
||||||
})
|
})
|
||||||
mux.HandleFunc(PathDoCreate, func(w http.ResponseWriter, r *http.Request) {
|
mux.HandleFunc(pathDoCreate, func(w http.ResponseWriter, r *http.Request) {
|
||||||
const dateFmt = "2006-01-02"
|
const dateFmt = "2006-01-02"
|
||||||
earliest, err := time.Parse(dateFmt, r.FormValue(fieldNameEarliest))
|
earliest, err := time.Parse(dateFmt, r.FormValue(fieldNameEarliest))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -160,6 +176,18 @@ func main() {
|
|||||||
|
|
||||||
_ = writePage(w, pageDoCreate(eventName, earliest, latest))
|
_ = writePage(w, pageDoCreate(eventName, 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())
|
||||||
|
})
|
||||||
|
|
||||||
srv := http.Server{
|
srv := http.Server{
|
||||||
Addr: Addr,
|
Addr: Addr,
|
||||||
|
54
store.go
54
store.go
@ -2,9 +2,13 @@ package henwen
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"crawshaw.io/sqlite"
|
||||||
"crawshaw.io/sqlite/sqlitex"
|
"crawshaw.io/sqlite/sqlitex"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -36,6 +40,7 @@ func NewStore(filename string, genString GenString) (*Store, error) {
|
|||||||
genString: genString,
|
genString: genString,
|
||||||
}
|
}
|
||||||
if needCreate {
|
if needCreate {
|
||||||
|
log.Println("creating schema")
|
||||||
err = store.createSchema()
|
err = store.createSchema()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
defer pool.Close()
|
defer pool.Close()
|
||||||
@ -54,7 +59,7 @@ const schema = `
|
|||||||
CREATE TABLE event (
|
CREATE TABLE event (
|
||||||
id INTEGER PRIMARY KEY,
|
id INTEGER PRIMARY KEY,
|
||||||
alpha_id TEXT NOT NULL,
|
alpha_id TEXT NOT NULL,
|
||||||
admin_alpha_id TEXT NOT NULL,
|
admin_code TEXT NOT NULL,
|
||||||
name TEXT NOT NULL,
|
name TEXT NOT NULL,
|
||||||
earliest_date DATE NOT NULL,
|
earliest_date DATE NOT NULL,
|
||||||
latest_date DATE NOT NULL,
|
latest_date DATE NOT NULL,
|
||||||
@ -92,7 +97,7 @@ type CreateEventCommand struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type CreateEventResult struct {
|
type CreateEventResult struct {
|
||||||
AlphaID, AdminAlphaID string
|
AlphaID, AdminCode string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Store) CreateEvent(ctx context.Context, cmd CreateEventCommand) (result CreateEventResult, err error) {
|
func (s *Store) CreateEvent(ctx context.Context, cmd CreateEventCommand) (result CreateEventResult, err error) {
|
||||||
@ -104,18 +109,18 @@ func (s *Store) CreateEvent(ctx context.Context, cmd CreateEventCommand) (result
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
adminAlphaID, err := s.genString()
|
adminCode, err := s.genString()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const dateFmt = "2006-01-02"
|
const dateFmt = "2006-01-02"
|
||||||
const query = `
|
const query = `
|
||||||
INSERT INTO event(alpha_id, admin_alpha_id, name, earliest_date, latest_date, duration)
|
INSERT INTO event(alpha_id, admin_code, name, earliest_date, latest_date, duration)
|
||||||
VALUES (?, ?, ?, ?, ?, ?);`
|
VALUES (?, ?, ?, ?, ?, ?);`
|
||||||
err = sqlitex.Exec(conn, query, nil,
|
err = sqlitex.Exec(conn, query, nil,
|
||||||
alphaID,
|
alphaID,
|
||||||
adminAlphaID,
|
adminCode,
|
||||||
cmd.Name,
|
cmd.Name,
|
||||||
cmd.EarliestDate.Format(dateFmt),
|
cmd.EarliestDate.Format(dateFmt),
|
||||||
cmd.LatestDate.Format(dateFmt),
|
cmd.LatestDate.Format(dateFmt),
|
||||||
@ -125,7 +130,44 @@ func (s *Store) CreateEvent(ctx context.Context, cmd CreateEventCommand) (result
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
result.AdminAlphaID = adminAlphaID
|
result.AdminCode = adminCode
|
||||||
result.AlphaID = alphaID
|
result.AlphaID = alphaID
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type GetEventQuery struct {
|
||||||
|
AlphaID, AdminCode string
|
||||||
|
}
|
||||||
|
|
||||||
|
type GetEventResult struct {
|
||||||
|
Name string
|
||||||
|
EarliestDate, LatestDate time.Time
|
||||||
|
Duration int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Store) GetEvent(ctx context.Context, query GetEventQuery) error {
|
||||||
|
conn := s.pool.Get(ctx)
|
||||||
|
defer s.pool.Put(conn)
|
||||||
|
|
||||||
|
const dbQuery = `
|
||||||
|
SELECT name, earliest_date, latest_date, duration
|
||||||
|
FROM event
|
||||||
|
WHERE alpha_id = ? AND admin_code = ?;`
|
||||||
|
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)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if !found {
|
||||||
|
return errors.New("not found")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user