diff options
Diffstat (limited to 'api/internal/controllers/user.go')
-rw-r--r-- | api/internal/controllers/user.go | 59 |
1 files changed, 55 insertions, 4 deletions
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 | } | ||