-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecords.go
89 lines (84 loc) · 2.38 KB
/
records.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package main
import (
"net/http"
"time"
"github.com/devilcove/timetraced/database"
"github.com/devilcove/timetraced/models"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
)
func getStatus(user string) (models.StatusResponse, error) {
durations := make(map[string]time.Duration)
status := models.Status{}
response := models.StatusResponse{}
records, err := database.GetTodaysRecordsForUser(user)
if err != nil {
return response, err
}
status.Current = models.Tracked(user)
for _, record := range records {
if record.End.IsZero() {
record.End = time.Now()
status.Elapsed = record.Duration()
}
durations[record.Project] = durations[record.Project] + record.End.Sub(record.Start)
status.DailyTotal = status.DailyTotal + record.Duration()
if record.Project == status.Current {
status.Total = status.Total + record.Duration()
}
}
response.Current = status.Current
response.Elapsed = models.FmtDuration(status.Elapsed)
response.CurrentTotal = models.FmtDuration(status.Total)
response.DailyTotal = models.FmtDuration(status.DailyTotal)
for k := range durations {
value := models.FmtDuration(durations[k])
duration := models.Duration{
Project: k,
Elapsed: value,
}
response.Durations = append(response.Durations, duration)
}
return response, nil
}
func getRecord(c *gin.Context) {
id, err := uuid.Parse(c.Param("id"))
if err != nil {
processError(c, http.StatusBadRequest, err.Error())
return
}
record, err := database.GetRecord(id)
if err != nil {
processError(c, http.StatusInternalServerError, err.Error())
return
}
c.HTML(http.StatusOK, "editRecord", record)
}
func editRecord(c *gin.Context) {
var err error
edit := models.EditRecord{}
if err := c.Bind(&edit); err != nil {
processError(c, http.StatusBadRequest, err.Error())
return
}
record, err := database.GetRecord(uuid.MustParse(edit.ID))
if err != nil {
processError(c, http.StatusInternalServerError, err.Error())
return
}
record.End, err = time.Parse("2006-01-0215:04", edit.End+edit.EndTime)
if err != nil {
processError(c, http.StatusBadRequest, err.Error())
return
}
record.Start, err = time.Parse("2006-01-0215:04", edit.Start+edit.StartTime)
if err != nil {
processError(c, http.StatusBadRequest, err.Error())
return
}
if err := database.SaveRecord(&record); err != nil {
processError(c, http.StatusInternalServerError, err.Error())
return
}
displayStatus(c)
}