diff options
| author | Zach Berwaldt <zberwaldt@tutamail.com> | 2024-03-15 18:49:43 -0400 |
|---|---|---|
| committer | Zach Berwaldt <zberwaldt@tutamail.com> | 2024-03-15 18:49:43 -0400 |
| commit | 9cae9c1d2a0b4f7fa72f3075541b9ffafe1a7275 (patch) | |
| tree | 960fa4f96a1328861a06d97180da8601af6855da /api/internal/controllers | |
| parent | 8fab2d03bce82e4dee798ebffb1e93c557f62a4b (diff) | |
Add routes for preference, clean up and add types
Diffstat (limited to 'api/internal/controllers')
| -rw-r--r-- | api/internal/controllers/auth.go | 18 | ||||
| -rw-r--r-- | api/internal/controllers/preferences.go | 45 | ||||
| -rw-r--r-- | api/internal/controllers/user.go | 59 |
3 files changed, 111 insertions, 11 deletions
diff --git a/api/internal/controllers/auth.go b/api/internal/controllers/auth.go index 58653d0..ab2fbbb 100644 --- a/api/internal/controllers/auth.go +++ b/api/internal/controllers/auth.go | |||
| @@ -38,23 +38,27 @@ func AuthHandler (c *gin.Context) { | |||
| 38 | 38 | ||
| 39 | var user models.User | 39 | var user models.User |
| 40 | var preference models.Preference | 40 | var preference models.Preference |
| 41 | var size models.Size | ||
| 42 | 41 | ||
| 43 | row := db.QueryRow("SELECT name, uuid, password, color, size, unit FROM Users u INNER JOIN Preferences p ON p.user_id = u.id INNER JOIN Sizes s ON p.size_id = s.id WHERE u.name = ?", username) | 42 | row := db.QueryRow("SELECT id as 'id', name, uuid, password FROM Users WHERE name = ?", username) |
| 44 | if err := row.Scan(&user.Name, &user.UUID, &user.Password, &preference.Color, &size.Size, &size.Unit); err != nil { | 43 | if err := row.Scan(&user.ID, &user.Name, &user.UUID, &user.Password); err != nil { |
| 45 | if errors.Is(err, sql.ErrNoRows) { | 44 | if errors.Is(err, sql.ErrNoRows) { |
| 46 | c.AbortWithStatus(http.StatusUnauthorized) | 45 | c.JSON(http.StatusNotFound, gin.H{"error": err.Error()}) |
| 47 | return | 46 | return |
| 48 | } | 47 | } |
| 49 | } | 48 | } |
| 50 | 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 | |||
| 51 | if err := bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(password)); err != nil { | 57 | if err := bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(password)); err != nil { |
| 52 | c.AbortWithStatus(http.StatusUnauthorized) | 58 | c.JSON(http.StatusUnauthorized, gin.H{"error": err.Error()}) |
| 53 | return | 59 | return |
| 54 | } | 60 | } |
| 55 | 61 | ||
| 56 | preference.Size = size | ||
| 57 | |||
| 58 | // Generate a simple API token | 62 | // Generate a simple API token |
| 59 | apiToken := generateToken() | 63 | apiToken := generateToken() |
| 60 | c.JSON(http.StatusOK, gin.H{"token": apiToken, "user": user, "preferences": preference}) | 64 | c.JSON(http.StatusOK, gin.H{"token": apiToken, "user": user, "preferences": preference}) |
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/user.go b/api/internal/controllers/user.go index 76dedc8..dbb09cf 100644 --- a/api/internal/controllers/user.go +++ b/api/internal/controllers/user.go | |||
| @@ -1,17 +1,68 @@ | |||
| 1 | package controllers | 1 | package controllers |
| 2 | 2 | ||
| 3 | import ( | 3 | import ( |
| 4 | "github.com/gin-gonic/gin" | 4 | "database/sql" |
| 5 | "errors" | ||
| 6 | "log" | ||
| 5 | "net/http" | 7 | "net/http" |
| 8 | "water/api/internal/database" | ||
| 9 | "water/api/internal/models" | ||
| 10 | |||
| 11 | "github.com/gin-gonic/gin" | ||
| 6 | ) | 12 | ) |
| 7 | 13 | ||
| 8 | func GetUser(c *gin.Context) { | 14 | func GetUser(c *gin.Context) { |
| 9 | c.JSON(http.StatusOK, gin.H{"message": "User found"}) | 15 | c.JSON(http.StatusOK, gin.H{"message": "User found"}) |
| 10 | } | 16 | } |
| 11 | func GetUserPreferences(c *gin.Context) { | 17 | func GetUserPreferences(c *gin.Context) { |
| 12 | c.JSON(http.StatusOK, gin.H{"message": "Preferences fetched successfully"}) | 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) | ||
| 13 | } | 41 | } |
| 14 | 42 | ||
| 15 | func UpdateUserPreferences(c *gin.Context) { | 43 | func UpdateUserPreferences(c *gin.Context) { |
| 16 | c.JSON(http.StatusOK, gin.H{"message": "Preferences updated successfully"}) | 44 | db := database.EstablishDBConnection() |
| 17 | } \ No newline at end of file | 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 | } | ||
