package controllers import ( "database/sql" "net/http" "water/api/internal/database" "water/api/internal/models" "github.com/gin-gonic/gin" ) // 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, err := database.EstablishDBConnection() if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } 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, err := database.EstablishDBConnection() if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } 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, err := database.EstablishDBConnection() if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } 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, err := database.EstablishDBConnection() if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } 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"}) }