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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
package controllers
import (
"database/sql"
"github.com/gin-gonic/gin"
"net/http"
"water/api/internal/database"
"water/api/internal/models"
)
// TODO: add comments to all exported members of package.
// GetAllStatistics connects to the database and queries for all statistics in the database.
// If none have been found it will return an error, otherwise a 200 code is sent along with the list of statistics.
func GetAllStatistics(c *gin.Context) {
db := database.EstablishDBConnection()
defer func(db *sql.DB) {
err := db.Close()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
}(db)
rows, err := db.Query("SELECT s.date, s.quantity, u.uuid, u.name FROM Statistics s INNER JOIN Users u ON u.id = s.user_id")
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
defer func(rows *sql.Rows) {
err := rows.Close()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
}(rows)
var data []models.Statistic
for rows.Next() {
var stat models.Statistic
var user models.User
if err := rows.Scan(&stat.Date, &stat.Quantity, &user.UUID, &user.Name); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
stat.User = user
data = append(data, stat)
}
c.JSON(http.StatusOK, data)
}
func PostNewStatistic(c *gin.Context) {
var stat models.StatisticPost
if err := c.BindJSON(&stat); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
db := database.EstablishDBConnection()
defer func(db *sql.DB) {
err := db.Close()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
}(db)
result, err := db.Exec("INSERT INTO statistics (date, user_id, quantity) values (?, ?, ?)", stat.Date, stat.UserID, stat.Quantity)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
}
id, err := result.LastInsertId()
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
}
c.JSON(http.StatusCreated, gin.H{"status": "created", "id": id})
}
func GetWeeklyStatistics(c *gin.Context) {
db := database.EstablishDBConnection()
defer func(db *sql.DB) {
err := db.Close()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
}(db)
rows, err := db.Query("SELECT date, total FROM `WeeklyStatisticsView`")
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
defer func(rows *sql.Rows) {
err := rows.Close()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
}(rows)
var data []models.WeeklyStatistic
for rows.Next() {
var weeklyStat models.WeeklyStatistic
if err := rows.Scan(&weeklyStat.Date, &weeklyStat.Total); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
}
data = append(data, weeklyStat)
}
c.JSON(http.StatusOK, data)
}
func GetDailyUserStatistics(c *gin.Context) {
db := database.EstablishDBConnection()
defer func(db *sql.DB) {
err := db.Close()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
}(db)
rows, err := db.Query("SELECT name, total FROM DailyUserStatistics")
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
defer func(rows *sql.Rows) {
err := rows.Close()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
}(rows)
var data []models.DailyUserTotals
for rows.Next() {
var stat models.DailyUserTotals
if err := rows.Scan(&stat.Name, &stat.Total); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
data = append(data, stat)
}
c.JSON(http.StatusOK, data)
}
func GetUserStatistics(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"status": "ok", "uuid": c.Param("uuid")})
}
func UpdateUserStatistic(c *gin.Context) {
c.JSON(http.StatusNoContent, gin.H{"status": "No Content"})
}
func DeleteUserStatistic(c *gin.Context) {
c.JSON(http.StatusNoContent, gin.H{"status": "No Content"})
}
|