package controllers import ( "database/sql" "errors" "net/http" "water/api/internal/database" "water/api/internal/models" "github.com/gin-gonic/gin" ) func GetUser(c *gin.Context) { c.JSON(http.StatusOK, gin.H{"message": "User found"}) } func GetUserPreferences(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) var preference models.Preference row := db.QueryRow("SELECT id, user_id, color, size_id FROM Preferences WHERE user_id = ?", c.Param("id")) if err := row.Scan(&preference.ID, &preference.UserID, &preference.Color, &preference.SizeID); err != nil { if errors.Is(err, sql.ErrNoRows) { c.JSON(http.StatusNotFound, gin.H{"error": err.Error()}) return } else { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } } c.JSON(http.StatusOK, preference) } func UpdateUserPreferences(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) var newPreferences models.Preference if err := c.BindJSON(&newPreferences); err != nil { c.JSON(http.StatusUnprocessableEntity, gin.H{"error": err.Error()}) return } _, err = db.Exec("UPDATE Preferences SET color = ?, size_id = ? WHERE id = ?", newPreferences.Color, newPreferences.SizeID, newPreferences.ID) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } c.Status(http.StatusNoContent) }