diff options
| author | zberwaldt <17715430+zberwaldt@users.noreply.github.com> | 2024-03-15 22:03:11 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-03-15 22:03:11 -0400 |
| commit | 6f8cfbd6cc3d5adbda38e74013c68e3d4745766d (patch) | |
| tree | b3f045cd06d6622e23441b442e8f3861050ed444 /api/internal/controllers | |
| parent | fac21fa0a72d4a7f1a01ccd44e3acf9c90fd95bd (diff) | |
| parent | fd1332a3df191577e91c6d846a8b5db1747099fd (diff) | |
Merge pull request #1 from zberwaldt/staging
Staging to Prod
Diffstat (limited to 'api/internal/controllers')
| -rw-r--r-- | api/internal/controllers/auth.go | 79 | ||||
| -rw-r--r-- | api/internal/controllers/preferences.go | 45 | ||||
| -rw-r--r-- | api/internal/controllers/stats.go | 168 | ||||
| -rw-r--r-- | api/internal/controllers/user.go | 68 |
4 files changed, 360 insertions, 0 deletions
diff --git a/api/internal/controllers/auth.go b/api/internal/controllers/auth.go new file mode 100644 index 0000000..ab2fbbb --- /dev/null +++ b/api/internal/controllers/auth.go | |||
| @@ -0,0 +1,79 @@ | |||
| 1 | package controllers | ||
| 2 | |||
| 3 | import ( | ||
| 4 | "crypto/rand" | ||
| 5 | "database/sql" | ||
| 6 | "encoding/base64" | ||
| 7 | "errors" | ||
| 8 | "github.com/gin-gonic/gin" | ||
| 9 | "net/http" | ||
| 10 | "water/api/internal/models" | ||
| 11 | |||
| 12 | _ "github.com/mattn/go-sqlite3" | ||
| 13 | "golang.org/x/crypto/bcrypt" | ||
| 14 | "water/api/internal/database" | ||
| 15 | ) | ||
| 16 | |||
| 17 | |||
| 18 | |||
| 19 | // AuthHandler is a function that handles users' authentication. It checks if the request | ||
| 20 | // has valid credentials, authenticates the user and sets the user's session. | ||
| 21 | // If the authentication is successful, it will allow the user to access protected routes. | ||
| 22 | func AuthHandler (c *gin.Context) { | ||
| 23 | username, password, ok := c.Request.BasicAuth() | ||
| 24 | if !ok { | ||
| 25 | c.Header("WWW-Authenticate", `Basic realm="Please enter your username and password."`) | ||
| 26 | c.AbortWithStatus(http.StatusUnauthorized) | ||
| 27 | return | ||
| 28 | } | ||
| 29 | |||
| 30 | db := database.EstablishDBConnection() | ||
| 31 | defer func(db *sql.DB) { | ||
| 32 | err := db.Close() | ||
| 33 | if err != nil { | ||
| 34 | c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) | ||
| 35 | return | ||
| 36 | } | ||
| 37 | }(db) | ||
| 38 | |||
| 39 | var user models.User | ||
| 40 | var preference models.Preference | ||
| 41 | |||
| 42 | row := db.QueryRow("SELECT id as 'id', name, uuid, password FROM Users WHERE name = ?", username) | ||
| 43 | if err := row.Scan(&user.ID, &user.Name, &user.UUID, &user.Password); err != nil { | ||
| 44 | if errors.Is(err, sql.ErrNoRows) { | ||
| 45 | c.JSON(http.StatusNotFound, gin.H{"error": err.Error()}) | ||
| 46 | return | ||
| 47 | } | ||
| 48 | } | ||
| 49 | |||
| 50 | row = db.QueryRow("SELECT id, color, size_id, user_id FROM Preferences where user_id = ?", user.ID) | ||
| 51 | if err := row.Scan(&preference.ID, &preference.Color, &preference.SizeID, &preference.UserID); err != nil { | ||
| 52 | if errors.Is(err, sql.ErrNoRows) { | ||
| 53 | c.JSON(http.StatusNotFound, gin.H{"error": err.Error()}) | ||
| 54 | } | ||
| 55 | } | ||
| 56 | |||
| 57 | if err := bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(password)); err != nil { | ||
| 58 | c.JSON(http.StatusUnauthorized, gin.H{"error": err.Error()}) | ||
| 59 | return | ||
| 60 | } | ||
| 61 | |||
| 62 | // Generate a simple API token | ||
| 63 | apiToken := generateToken() | ||
| 64 | c.JSON(http.StatusOK, gin.H{"token": apiToken, "user": user, "preferences": preference}) | ||
| 65 | } | ||
| 66 | |||
| 67 | |||
| 68 | // generateToken is a helper function used in the AuthHandler. It generates a random token for API authentication. | ||
| 69 | // This function creates an empty byte slice of length 32 and fills it with cryptographic random data using the rand.Read function. | ||
| 70 | // If an error occurs during the generation, it will return an empty string. | ||
| 71 | // The generated cryptographic random data is then encoded into a base64 string and returned. | ||
| 72 | func generateToken() string { | ||
| 73 | token := make([]byte, 32) | ||
| 74 | _, err := rand.Read(token) | ||
| 75 | if err != nil { | ||
| 76 | return "" | ||
| 77 | } | ||
| 78 | return base64.StdEncoding.EncodeToString(token) | ||
| 79 | } \ No newline at end of file | ||
diff --git a/api/internal/controllers/preferences.go b/api/internal/controllers/preferences.go new file mode 100644 index 0000000..a1bcf4f --- /dev/null +++ b/api/internal/controllers/preferences.go | |||
| @@ -0,0 +1,45 @@ | |||
| 1 | package controllers | ||
| 2 | |||
| 3 | import ( | ||
| 4 | "github.com/gin-gonic/gin" | ||
| 5 | "net/http" | ||
| 6 | "database/sql" | ||
| 7 | "water/api/internal/database" | ||
| 8 | "water/api/internal/models" | ||
| 9 | ) | ||
| 10 | |||
| 11 | func GetSizes(c *gin.Context) { | ||
| 12 | db := database.EstablishDBConnection() | ||
| 13 | defer func(db *sql.DB) { | ||
| 14 | err := db.Close() | ||
| 15 | if err != nil { | ||
| 16 | c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) | ||
| 17 | } | ||
| 18 | }(db) | ||
| 19 | |||
| 20 | rows, err := db.Query("SELECT id, size, unit FROM Sizes") | ||
| 21 | if err != nil { | ||
| 22 | c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) | ||
| 23 | return | ||
| 24 | } | ||
| 25 | defer func(rows *sql.Rows) { | ||
| 26 | err := rows.Close() | ||
| 27 | if err != nil { | ||
| 28 | c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) | ||
| 29 | return | ||
| 30 | } | ||
| 31 | }(rows) | ||
| 32 | |||
| 33 | var data []models.Size | ||
| 34 | |||
| 35 | for rows.Next() { | ||
| 36 | var size models.Size | ||
| 37 | if err := rows.Scan(&size.ID, &size.Size, &size.Unit); err != nil { | ||
| 38 | c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) | ||
| 39 | return | ||
| 40 | } | ||
| 41 | data = append(data, size) | ||
| 42 | } | ||
| 43 | |||
| 44 | c.JSON(http.StatusOK, data) | ||
| 45 | } | ||
diff --git a/api/internal/controllers/stats.go b/api/internal/controllers/stats.go new file mode 100644 index 0000000..2234787 --- /dev/null +++ b/api/internal/controllers/stats.go | |||
| @@ -0,0 +1,168 @@ | |||
| 1 | package controllers | ||
| 2 | |||
| 3 | import ( | ||
| 4 | "database/sql" | ||
| 5 | "github.com/gin-gonic/gin" | ||
| 6 | "net/http" | ||
| 7 | "water/api/internal/database" | ||
| 8 | "water/api/internal/models" | ||
| 9 | ) | ||
| 10 | |||
| 11 | // TODO: add comments to all exported members of package. | ||
| 12 | |||
| 13 | // 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 | func GetAllStatistics(c *gin.Context) { | ||
| 16 | db := database.EstablishDBConnection() | ||
| 17 | defer func(db *sql.DB) { | ||
| 18 | err := db.Close() | ||
| 19 | if err != nil { | ||
| 20 | c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) | ||
| 21 | return | ||
| 22 | } | ||
| 23 | }(db) | ||
| 24 | |||
| 25 | 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") | ||
| 26 | if err != nil { | ||
| 27 | c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) | ||
| 28 | return | ||
| 29 | } | ||
| 30 | defer func(rows *sql.Rows) { | ||
| 31 | err := rows.Close() | ||
| 32 | if err != nil { | ||
| 33 | c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) | ||
| 34 | return | ||
| 35 | } | ||
| 36 | }(rows) | ||
| 37 | |||
| 38 | var data []models.Statistic | ||
| 39 | |||
| 40 | for rows.Next() { | ||
| 41 | var stat models.Statistic | ||
| 42 | var user models.User | ||
| 43 | if err := rows.Scan(&stat.Date, &stat.Quantity, &user.UUID, &user.Name); err != nil { | ||
| 44 | c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) | ||
| 45 | return | ||
| 46 | } | ||
| 47 | stat.User = user | ||
| 48 | data = append(data, stat) | ||
| 49 | } | ||
| 50 | |||
| 51 | c.JSON(http.StatusOK, data) | ||
| 52 | } | ||
| 53 | |||
| 54 | func PostNewStatistic(c *gin.Context) { | ||
| 55 | var stat models.StatisticPost | ||
| 56 | |||
| 57 | if err := c.BindJSON(&stat); err != nil { | ||
| 58 | c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | ||
| 59 | return | ||
| 60 | } | ||
| 61 | |||
| 62 | db := database.EstablishDBConnection() | ||
| 63 | defer func(db *sql.DB) { | ||
| 64 | err := db.Close() | ||
| 65 | if err != nil { | ||
| 66 | c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) | ||
| 67 | return | ||
| 68 | } | ||
| 69 | }(db) | ||
| 70 | |||
| 71 | result, err := db.Exec("INSERT INTO statistics (date, user_id, quantity) values (?, ?, ?)", stat.Date, stat.UserID, stat.Quantity) | ||
| 72 | |||
| 73 | if err != nil { | ||
| 74 | c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | ||
| 75 | } | ||
| 76 | |||
| 77 | id, err := result.LastInsertId() | ||
| 78 | if err != nil { | ||
| 79 | c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | ||
| 80 | } | ||
| 81 | |||
| 82 | c.JSON(http.StatusCreated, gin.H{"status": "created", "id": id}) | ||
| 83 | } | ||
| 84 | |||
| 85 | func GetWeeklyStatistics(c *gin.Context) { | ||
| 86 | db := database.EstablishDBConnection() | ||
| 87 | defer func(db *sql.DB) { | ||
| 88 | err := db.Close() | ||
| 89 | if err != nil { | ||
| 90 | c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) | ||
| 91 | return | ||
| 92 | } | ||
| 93 | }(db) | ||
| 94 | |||
| 95 | rows, err := db.Query("SELECT date, total FROM `WeeklyStatisticsView`") | ||
| 96 | if err != nil { | ||
| 97 | c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) | ||
| 98 | return | ||
| 99 | } | ||
| 100 | defer func(rows *sql.Rows) { | ||
| 101 | err := rows.Close() | ||
| 102 | if err != nil { | ||
| 103 | c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) | ||
| 104 | return | ||
| 105 | } | ||
| 106 | }(rows) | ||
| 107 | |||
| 108 | var data []models.WeeklyStatistic | ||
| 109 | for rows.Next() { | ||
| 110 | var weeklyStat models.WeeklyStatistic | ||
| 111 | if err := rows.Scan(&weeklyStat.Date, &weeklyStat.Total); err != nil { | ||
| 112 | c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) | ||
| 113 | } | ||
| 114 | data = append(data, weeklyStat) | ||
| 115 | } | ||
| 116 | |||
| 117 | c.JSON(http.StatusOK, data) | ||
| 118 | } | ||
| 119 | |||
| 120 | func GetDailyUserStatistics(c *gin.Context) { | ||
| 121 | db := database.EstablishDBConnection() | ||
| 122 | defer func(db *sql.DB) { | ||
| 123 | err := db.Close() | ||
| 124 | if err != nil { | ||
| 125 | c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) | ||
| 126 | return | ||
| 127 | } | ||
| 128 | }(db) | ||
| 129 | |||
| 130 | rows, err := db.Query("SELECT name, total FROM DailyUserStatistics") | ||
| 131 | |||
| 132 | if err != nil { | ||
| 133 | c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) | ||
| 134 | return | ||
| 135 | } | ||
| 136 | defer func(rows *sql.Rows) { | ||
| 137 | err := rows.Close() | ||
| 138 | if err != nil { | ||
| 139 | c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) | ||
| 140 | return | ||
| 141 | } | ||
| 142 | }(rows) | ||
| 143 | |||
| 144 | var data []models.DailyUserTotals | ||
| 145 | for rows.Next() { | ||
| 146 | var stat models.DailyUserTotals | ||
| 147 | if err := rows.Scan(&stat.Name, &stat.Total); err != nil { | ||
| 148 | c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) | ||
| 149 | return | ||
| 150 | } | ||
| 151 | data = append(data, stat) | ||
| 152 | } | ||
| 153 | |||
| 154 | c.JSON(http.StatusOK, data) | ||
| 155 | |||
| 156 | } | ||
| 157 | |||
| 158 | func GetUserStatistics(c *gin.Context) { | ||
| 159 | c.JSON(http.StatusOK, gin.H{"status": "ok", "uuid": c.Param("uuid")}) | ||
| 160 | } | ||
| 161 | |||
| 162 | func UpdateUserStatistic(c *gin.Context) { | ||
| 163 | c.JSON(http.StatusNoContent, gin.H{"status": "No Content"}) | ||
| 164 | } | ||
| 165 | |||
| 166 | func DeleteUserStatistic(c *gin.Context) { | ||
| 167 | c.JSON(http.StatusNoContent, gin.H{"status": "No Content"}) | ||
| 168 | } | ||
diff --git a/api/internal/controllers/user.go b/api/internal/controllers/user.go new file mode 100644 index 0000000..dbb09cf --- /dev/null +++ b/api/internal/controllers/user.go | |||
| @@ -0,0 +1,68 @@ | |||
| 1 | package controllers | ||
| 2 | |||
| 3 | import ( | ||
| 4 | "database/sql" | ||
| 5 | "errors" | ||
| 6 | "log" | ||
| 7 | "net/http" | ||
| 8 | "water/api/internal/database" | ||
| 9 | "water/api/internal/models" | ||
| 10 | |||
| 11 | "github.com/gin-gonic/gin" | ||
| 12 | ) | ||
| 13 | |||
| 14 | func GetUser(c *gin.Context) { | ||
| 15 | c.JSON(http.StatusOK, gin.H{"message": "User found"}) | ||
| 16 | } | ||
| 17 | func GetUserPreferences(c *gin.Context) { | ||
| 18 | db := database.EstablishDBConnection() | ||
| 19 | defer func(db *sql.DB) { | ||
| 20 | err := db.Close() | ||
| 21 | if err != nil { | ||
| 22 | c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) | ||
| 23 | return | ||
| 24 | } | ||
| 25 | }(db) | ||
| 26 | |||
| 27 | var preference models.Preference | ||
| 28 | |||
| 29 | row := db.QueryRow("SELECT id, user_id, color, size_id FROM Preferences WHERE user_id = ?", c.Param("id")) | ||
| 30 | if err := row.Scan(&preference.ID, &preference.UserID, &preference.Color, &preference.SizeID); err != nil { | ||
| 31 | if errors.Is(err, sql.ErrNoRows) { | ||
| 32 | c.JSON(http.StatusNotFound, gin.H{"error": err.Error()}) | ||
| 33 | return | ||
| 34 | } else { | ||
| 35 | c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) | ||
| 36 | return | ||
| 37 | } | ||
| 38 | } | ||
| 39 | |||
| 40 | c.JSON(http.StatusOK, preference) | ||
| 41 | } | ||
| 42 | |||
| 43 | func UpdateUserPreferences(c *gin.Context) { | ||
| 44 | db := database.EstablishDBConnection() | ||
| 45 | defer func(db *sql.DB) { | ||
| 46 | err := db.Close() | ||
| 47 | if err != nil { | ||
| 48 | c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) | ||
| 49 | return | ||
| 50 | } | ||
| 51 | }(db) | ||
| 52 | |||
| 53 | var newPreferences models.Preference | ||
| 54 | if err := c.BindJSON(&newPreferences); err != nil { | ||
| 55 | c.JSON(http.StatusUnprocessableEntity, gin.H{"error": err.Error()}) | ||
| 56 | return | ||
| 57 | } | ||
| 58 | |||
| 59 | log.Printf("newPreferences: %v", newPreferences) | ||
| 60 | |||
| 61 | _, err := db.Exec("UPDATE Preferences SET color = ?, size_id = ? WHERE id = ?", newPreferences.Color, newPreferences.SizeID, newPreferences.ID) | ||
| 62 | if err != nil { | ||
| 63 | c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) | ||
| 64 | return | ||
| 65 | } | ||
| 66 | |||
| 67 | c.Status(http.StatusNoContent) | ||
| 68 | } | ||
