diff options
Diffstat (limited to 'api/internal/controllers/stats.go')
-rw-r--r-- | api/internal/controllers/stats.go | 27 |
1 files changed, 22 insertions, 5 deletions
diff --git a/api/internal/controllers/stats.go b/api/internal/controllers/stats.go index 2234787..889b923 100644 --- a/api/internal/controllers/stats.go +++ b/api/internal/controllers/stats.go | |||
@@ -2,10 +2,11 @@ package controllers | |||
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | "database/sql" | 4 | "database/sql" |
5 | "github.com/gin-gonic/gin" | ||
6 | "net/http" | 5 | "net/http" |
7 | "water/api/internal/database" | 6 | "water/api/internal/database" |
8 | "water/api/internal/models" | 7 | "water/api/internal/models" |
8 | |||
9 | "github.com/gin-gonic/gin" | ||
9 | ) | 10 | ) |
10 | 11 | ||
11 | // TODO: add comments to all exported members of package. | 12 | // TODO: add comments to all exported members of package. |
@@ -13,7 +14,11 @@ import ( | |||
13 | // GetAllStatistics connects to the database and queries for all statistics in the database. | 14 | // GetAllStatistics connects to the database and queries for all statistics in the database. |
14 | // If none have been found it will return an error, otherwise a 200 code is sent along with the list of statistics. | 15 | // If none have been found it will return an error, otherwise a 200 code is sent along with the list of statistics. |
15 | func GetAllStatistics(c *gin.Context) { | 16 | func GetAllStatistics(c *gin.Context) { |
16 | db := database.EstablishDBConnection() | 17 | db, err := database.EstablishDBConnection() |
18 | if err != nil { | ||
19 | c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) | ||
20 | return | ||
21 | } | ||
17 | defer func(db *sql.DB) { | 22 | defer func(db *sql.DB) { |
18 | err := db.Close() | 23 | err := db.Close() |
19 | if err != nil { | 24 | if err != nil { |
@@ -59,7 +64,11 @@ func PostNewStatistic(c *gin.Context) { | |||
59 | return | 64 | return |
60 | } | 65 | } |
61 | 66 | ||
62 | db := database.EstablishDBConnection() | 67 | db, err := database.EstablishDBConnection() |
68 | if err != nil { | ||
69 | c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) | ||
70 | return | ||
71 | } | ||
63 | defer func(db *sql.DB) { | 72 | defer func(db *sql.DB) { |
64 | err := db.Close() | 73 | err := db.Close() |
65 | if err != nil { | 74 | if err != nil { |
@@ -83,7 +92,11 @@ func PostNewStatistic(c *gin.Context) { | |||
83 | } | 92 | } |
84 | 93 | ||
85 | func GetWeeklyStatistics(c *gin.Context) { | 94 | func GetWeeklyStatistics(c *gin.Context) { |
86 | db := database.EstablishDBConnection() | 95 | db, err := database.EstablishDBConnection() |
96 | if err != nil { | ||
97 | c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) | ||
98 | return | ||
99 | } | ||
87 | defer func(db *sql.DB) { | 100 | defer func(db *sql.DB) { |
88 | err := db.Close() | 101 | err := db.Close() |
89 | if err != nil { | 102 | if err != nil { |
@@ -118,7 +131,11 @@ func GetWeeklyStatistics(c *gin.Context) { | |||
118 | } | 131 | } |
119 | 132 | ||
120 | func GetDailyUserStatistics(c *gin.Context) { | 133 | func GetDailyUserStatistics(c *gin.Context) { |
121 | db := database.EstablishDBConnection() | 134 | db, err := database.EstablishDBConnection() |
135 | if err != nil { | ||
136 | c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) | ||
137 | return | ||
138 | } | ||
122 | defer func(db *sql.DB) { | 139 | defer func(db *sql.DB) { |
123 | err := db.Close() | 140 | err := db.Close() |
124 | if err != nil { | 141 | if err != nil { |