summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSteven Le Rouzic <steven.lerouzic@gmail.com>2024-04-10 00:14:11 +0200
committerSteven Le Rouzic <steven.lerouzic@gmail.com>2024-04-10 00:14:11 +0200
commit49f7be2a35539bae2d43594c9b39268eec7837d0 (patch)
tree69b43e7ce19522b26e57bb1e82a51e9a03ee7523
parentc60dd3357fc84e14795f5f864e9fa9ce7150179f (diff)
Time !
-rw-r--r--.gitignore1
-rw-r--r--env.bat3
-rw-r--r--model/time.go34
-rw-r--r--model/timer.go6
-rw-r--r--model/uuid.go4
-rw-r--r--timer.go25
-rw-r--r--view/timer.templ6
-rw-r--r--view/timer_templ.go28
-rw-r--r--view/timers_list.templ2
-rw-r--r--view/timers_list_templ.go4
10 files changed, 93 insertions, 20 deletions
diff --git a/.gitignore b/.gitignore
index 1feae78..84332bf 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1 +1,2 @@
*.exe
+tmp-build
diff --git a/env.bat b/env.bat
new file mode 100644
index 0000000..94e3e5b
--- /dev/null
+++ b/env.bat
@@ -0,0 +1,3 @@
+set PATH=C:\TDM-GCC-64\bin;%PATH%
+set CGOENABLED=1
+set GOTMPDIR=.\tmp-build
diff --git a/model/time.go b/model/time.go
new file mode 100644
index 0000000..b7dc3d2
--- /dev/null
+++ b/model/time.go
@@ -0,0 +1,34 @@
+package model
+
+import (
+ "time"
+ "errors"
+ sqldriver "database/sql/driver"
+)
+
+type Time time.Time
+
+func MakeTimeNow() Time {
+ return Time(time.Now().UTC())
+}
+
+func (self Time) String() string {
+ return time.Time(self).String()
+}
+
+func (self Time) Value() (sqldriver.Value, error) {
+ return time.Time(self).Format(time.RFC3339), nil
+}
+
+func (self *Time) Scan(value any) error {
+ if valueStr, ok := value.(string); ok {
+ parsedTime, err := time.Parse(time.RFC3339, valueStr)
+ if err == nil {
+ *self = Time(parsedTime.UTC())
+ }
+ return err
+ } else {
+ return errors.New("Expected a string")
+ }
+}
+
diff --git a/model/timer.go b/model/timer.go
index 73bf7bc..4c81962 100644
--- a/model/timer.go
+++ b/model/timer.go
@@ -1,7 +1,9 @@
package model
type Timer struct {
- Id UUID
- Name string
+ Id UUID
+ Name string
+ StartTime Time
+ EndTime Time
}
diff --git a/model/uuid.go b/model/uuid.go
index f072d92..8852cc8 100644
--- a/model/uuid.go
+++ b/model/uuid.go
@@ -9,13 +9,13 @@ type UUID struct {
payload uuid.UUID
}
-func NewUUID() UUID {
+func MakeUUID() UUID {
id, _ := uuid.NewRandom()
return UUID { payload: id }
}
func (self UUID) Value() (sqldriver.Value, error) {
- return self.payload.MarshalBinary()
+ return self.payload[:], nil
}
func (self *UUID) Scan(value any) error {
diff --git a/timer.go b/timer.go
index 07ee39e..9b2280b 100644
--- a/timer.go
+++ b/timer.go
@@ -19,28 +19,32 @@ func initializeDatabase(db *sql.DB) error {
_, err = tx.Exec(`
CREATE TABLE Timer (
- id BLOB NOT NULL UNIQUE,
- name TEXT NOT NULL,
+ Id BLOB NOT NULL UNIQUE,
+ Name TEXT NOT NULL,
+ StartTime TEXT NOT NULL,
+ EndTime TEXT NOT NULL,
PRIMARY KEY (id)
)
`)
if err != nil { return err }
- id := model.NewUUID()
+ now := model.MakeTimeNow()
+
+ id := model.MakeUUID()
_, err = tx.Exec(`
- INSERT INTO Timer VALUES ($1, $2)`, id, "My timer");
+ INSERT INTO Timer VALUES ($1, $2, $3, $4)`, id, "My timer", now, now);
if err != nil { return err }
- id = model.NewUUID()
+ id = model.MakeUUID()
_, err = tx.Exec(`
- INSERT INTO Timer VALUES ($1, $2)`, id, "My timer 2");
+ INSERT INTO Timer VALUES ($1, $2, $3, $4)`, id, "My timer 2", now, now);
if err != nil { return err }
return tx.Commit()
}
func queryAllTimers(db *sql.DB) []model.Timer {
- rows, err := db.Query("SELECT id, name FROM Timer")
+ rows, err := db.Query("SELECT Id, Name FROM Timer")
if err != nil { log.Fatalln(err) }
timers := []model.Timer{}
@@ -58,10 +62,10 @@ func queryTimer(db *sql.DB, idStr string) *model.Timer {
var id model.UUID
if err := id.Scan(idStr); err != nil { return nil }
- row := db.QueryRow("SELECT id, name FROM Timer WHERE id=$1", id)
+ row := db.QueryRow("SELECT Id, Name, StartTime, EndTime FROM Timer WHERE Id=$1", id)
var t model.Timer
- if err := row.Scan(&t.Id, &t.Name); err == nil {
+ if err := row.Scan(&t.Id, &t.Name, &t.StartTime, &t.EndTime); err == nil {
return &t
}
@@ -96,7 +100,7 @@ func (server *myServer) handleTimer(w http.ResponseWriter, r *http.Request) {
}
func main() {
- log.Println("Hello")
+ log.Println("Starting...")
db, err := sql.Open("sqlite3", ":memory:")
if err != nil {
@@ -116,6 +120,7 @@ func main() {
http.HandleFunc("/timer/{timerId}", myServer.handleTimer)
http.HandleFunc("/", myServer.handleMain)
+ log.Println("Started!")
http.ListenAndServe("0.0.0.0:80", nil)
}
diff --git a/view/timer.templ b/view/timer.templ
index 18f0a7b..fa258eb 100644
--- a/view/timer.templ
+++ b/view/timer.templ
@@ -5,6 +5,8 @@ import (
)
templ TimerView(timer model.Timer) {
- <h1>This is timer { timer.Name }</h1>
- <a href="/">Back to list</a>
+ <h1>This is timer { timer.Name } </h1>
+ <p><a href="/">Back to list</a></p>
+ <p>Start time: { timer.StartTime.String() }</p>
+ <p>End time: { timer.EndTime.String() }</p>
}
diff --git a/view/timer_templ.go b/view/timer_templ.go
index 609ee87..7fee7d3 100644
--- a/view/timer_templ.go
+++ b/view/timer_templ.go
@@ -40,7 +40,33 @@ func TimerView(timer model.Timer) templ.Component {
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
- _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("</h1><a href=\"/\">Back to list</a>")
+ _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("</h1><p><a href=\"/\">Back to list</a></p><p>Start time: ")
+ if templ_7745c5c3_Err != nil {
+ return templ_7745c5c3_Err
+ }
+ var templ_7745c5c3_Var3 string
+ templ_7745c5c3_Var3, templ_7745c5c3_Err = templ.JoinStringErrs(timer.StartTime.String())
+ if templ_7745c5c3_Err != nil {
+ return templ.Error{Err: templ_7745c5c3_Err, FileName: `view\timer.templ`, Line: 10, Col: 45}
+ }
+ _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var3))
+ if templ_7745c5c3_Err != nil {
+ return templ_7745c5c3_Err
+ }
+ _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("</p><p>End time: ")
+ if templ_7745c5c3_Err != nil {
+ return templ_7745c5c3_Err
+ }
+ var templ_7745c5c3_Var4 string
+ templ_7745c5c3_Var4, templ_7745c5c3_Err = templ.JoinStringErrs(timer.EndTime.String())
+ if templ_7745c5c3_Err != nil {
+ return templ.Error{Err: templ_7745c5c3_Err, FileName: `view\timer.templ`, Line: 11, Col: 41}
+ }
+ _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var4))
+ if templ_7745c5c3_Err != nil {
+ return templ_7745c5c3_Err
+ }
+ _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("</p>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
diff --git a/view/timers_list.templ b/view/timers_list.templ
index 7367c50..e1731d4 100644
--- a/view/timers_list.templ
+++ b/view/timers_list.templ
@@ -6,7 +6,7 @@ import (
)
templ timer(t model.Timer) {
- <p><a href={ templ.URL(fmt.Sprintf("/timer/%s", t.Id)) }>{ t.Name }</a></p>
+ <p><a href={ templ.URL(fmt.Sprint("/timer/", t.Id)) }>{ t.Name }</a></p>
}
templ TimersList(timers []model.Timer) {
diff --git a/view/timers_list_templ.go b/view/timers_list_templ.go
index fd5320c..cbfe2eb 100644
--- a/view/timers_list_templ.go
+++ b/view/timers_list_templ.go
@@ -32,7 +32,7 @@ func timer(t model.Timer) templ.Component {
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
- var templ_7745c5c3_Var2 templ.SafeURL = templ.URL(fmt.Sprintf("/timer/%s", t.Id))
+ var templ_7745c5c3_Var2 templ.SafeURL = templ.URL(fmt.Sprint("/timer/", t.Id))
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(string(templ_7745c5c3_Var2)))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
@@ -44,7 +44,7 @@ func timer(t model.Timer) templ.Component {
var templ_7745c5c3_Var3 string
templ_7745c5c3_Var3, templ_7745c5c3_Err = templ.JoinStringErrs(t.Name)
if templ_7745c5c3_Err != nil {
- return templ.Error{Err: templ_7745c5c3_Err, FileName: `view\timers_list.templ`, Line: 9, Col: 69}
+ return templ.Error{Err: templ_7745c5c3_Err, FileName: `view\timers_list.templ`, Line: 9, Col: 66}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var3))
if templ_7745c5c3_Err != nil {