From 0c99d186a41b77a12773f5d8e0a6a49ffe1929fe Mon Sep 17 00:00:00 2001 From: velvettear Date: Wed, 1 Feb 2023 14:10:00 +0100 Subject: [PATCH] some minor improvements --- .vscode/launch.json | 6 +++++- internal/api/server.go | 13 +++++++------ internal/database/timestamp.go | 8 ++++---- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 47419d1..cce3d2e 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -6,7 +6,11 @@ "type": "go", "request": "launch", "mode": "auto", - "program": "${workspaceFolder}/main.go" + "program": "${workspaceFolder}/main.go", + "args": [ + "-c", + "/home/velvettear/worklog/config.yml" + ] } ] } \ No newline at end of file diff --git a/internal/api/server.go b/internal/api/server.go index 2efa314..22a3fc8 100644 --- a/internal/api/server.go +++ b/internal/api/server.go @@ -9,6 +9,7 @@ import ( "velvettear/worklog/internal/config" "velvettear/worklog/internal/database" "velvettear/worklog/internal/log" + "velvettear/worklog/internal/tools" "velvettear/worklog/internal/workday" ) @@ -109,7 +110,7 @@ func handlePost(user string, writer http.ResponseWriter, request *http.Request) if !success { if workday.ID > 0 { writer.WriteHeader(400) - writer.Write([]byte("workday for today has already been started at " + strconv.Itoa(workday.Start.Hour()) + ":" + strconv.Itoa(workday.Start.Minute()) + ":" + strconv.Itoa(workday.Start.Second()) + "\n")) + writer.Write([]byte("workday for today has already been started at " + tools.TimeToHHMMSS(workday.Start) + "\n")) } else { writer.WriteHeader(500) writer.Write([]byte("encountered an error starting a new workday\n")) @@ -117,21 +118,21 @@ func handlePost(user string, writer http.ResponseWriter, request *http.Request) break } writer.WriteHeader(200) - writer.Write([]byte("started new workday for user '" + workday.User + "' at " + strconv.Itoa(workday.Start.Hour()) + ":" + strconv.Itoa(workday.Start.Minute()) + ":" + strconv.Itoa(workday.Start.Second()) + "\n")) + writer.Write([]byte("started new workday for user '" + workday.User + "' at " + tools.TimeToHHMMSS(workday.Start) + "\n")) case "/stop": - success, workday := database.StopTimestamp() + success, workday := database.StopTimestamp(user) if !success { if workday.ID > 0 { writer.WriteHeader(500) - writer.Write([]byte("encountered an error stopping workday for user '" + workday.User + "'\n")) + writer.Write([]byte("encountered an error stopping workday for user '" + user + "'\n")) } else { writer.WriteHeader(400) - writer.Write([]byte("there is no open workday to stop\n")) + writer.Write([]byte("there is no open workday for user '" + user + "' to stop\n")) } break } writer.WriteHeader(200) - writer.Write([]byte("stopped workday for user '" + workday.User + "' started at " + strconv.Itoa(workday.Start.Hour()) + ":" + strconv.Itoa(workday.Start.Minute()) + ":" + strconv.Itoa(workday.Start.Second()) + "\n")) + writer.Write([]byte("stopped workday for user '" + workday.User + "' started at " + tools.TimeToHHMMSS(workday.Start) + "\n")) default: handled = false } diff --git a/internal/database/timestamp.go b/internal/database/timestamp.go index 9c3d737..b7f5039 100644 --- a/internal/database/timestamp.go +++ b/internal/database/timestamp.go @@ -32,15 +32,15 @@ func StartTimestamp(user string) (bool, Timestamp) { return success, workday } -func StopTimestamp() (bool, Timestamp) { +func StopTimestamp(user string) (bool, Timestamp) { var timestamp Timestamp - result := connection.Last(×tamp) + result := connection.Where("end = ? and user = ?", tools.ZeroDate, user).Last(×tamp) if result.Error != nil { - log.Error("encountered an error selecting the last workday", result.Error.Error()) + log.Error("encountered an error selecting the last workday for user '"+user+"'", result.Error.Error()) return false, timestamp } if timestamp.ID == 0 || !timestamp.End.Equal(tools.ZeroDate) { - log.Debug("there is no open workday to stop") + log.Debug("there is no open workday for user '" + user + "' to stop") timestamp.ID = 0 return false, timestamp }