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 | |
| parent | 8fab2d03bce82e4dee798ebffb1e93c557f62a4b (diff) | |
Add routes for preference, clean up and add types
Diffstat (limited to 'api')
| -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 | ||||
| -rw-r--r-- | api/internal/middleware/middleware.go | 7 | ||||
| -rw-r--r-- | api/internal/models/auth.go | 2 | ||||
| -rw-r--r-- | api/internal/models/preferences.go | 8 | ||||
| -rw-r--r-- | api/internal/models/statistics.go | 2 | ||||
| -rw-r--r-- | api/internal/models/user.go | 2 | ||||
| -rw-r--r-- | api/internal/router/router.go | 5 |
9 files changed, 125 insertions, 23 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 | } | ||
diff --git a/api/internal/middleware/middleware.go b/api/internal/middleware/middleware.go index 819f1e5..aa27fb8 100644 --- a/api/internal/middleware/middleware.go +++ b/api/internal/middleware/middleware.go | |||
| @@ -2,10 +2,11 @@ package middleware | |||
| 2 | 2 | ||
| 3 | import ( | 3 | import ( |
| 4 | "errors" | 4 | "errors" |
| 5 | "github.com/gin-gonic/gin" | ||
| 6 | "log" | 5 | "log" |
| 7 | "net/http" | 6 | "net/http" |
| 8 | "strings" | 7 | "strings" |
| 8 | |||
| 9 | "github.com/gin-gonic/gin" | ||
| 9 | ) | 10 | ) |
| 10 | 11 | ||
| 11 | func TokenRequired() gin.HandlerFunc { | 12 | func TokenRequired() gin.HandlerFunc { |
| @@ -27,7 +28,7 @@ func CORSMiddleware() gin.HandlerFunc { | |||
| 27 | c.Writer.Header().Set("Access-Control-Allow-Origin", "*") | 28 | c.Writer.Header().Set("Access-Control-Allow-Origin", "*") |
| 28 | c.Writer.Header().Set("Access-Control-Allow-Credentials", "true") | 29 | c.Writer.Header().Set("Access-Control-Allow-Credentials", "true") |
| 29 | c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With") | 30 | c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With") |
| 30 | c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS, GET, PUT") | 31 | c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS, GET, PUT, PATCH") |
| 31 | 32 | ||
| 32 | if c.Request.Method == "OPTIONS" { | 33 | if c.Request.Method == "OPTIONS" { |
| 33 | log.Println(c.Request.Header) | 34 | log.Println(c.Request.Header) |
| @@ -52,4 +53,4 @@ func checkForTokenInContext(c *gin.Context) (string, error) { | |||
| 52 | } | 53 | } |
| 53 | 54 | ||
| 54 | return parts[1], nil | 55 | return parts[1], nil |
| 55 | } \ No newline at end of file | 56 | } |
diff --git a/api/internal/models/auth.go b/api/internal/models/auth.go index 41344d5..fa7dbe4 100644 --- a/api/internal/models/auth.go +++ b/api/internal/models/auth.go | |||
| @@ -3,7 +3,7 @@ package models | |||
| 3 | import "time" | 3 | import "time" |
| 4 | 4 | ||
| 5 | type Token struct { | 5 | type Token struct { |
| 6 | ID int64 `json:"-"` | 6 | ID int64 `json:"id"` |
| 7 | UserID int64 `json:"user_id"` | 7 | UserID int64 `json:"user_id"` |
| 8 | Token string `json:"token"` | 8 | Token string `json:"token"` |
| 9 | CreatedAt time.Time `json:"created_at"` | 9 | CreatedAt time.Time `json:"created_at"` |
diff --git a/api/internal/models/preferences.go b/api/internal/models/preferences.go index cbbd47c..8022099 100644 --- a/api/internal/models/preferences.go +++ b/api/internal/models/preferences.go | |||
| @@ -1,14 +1,14 @@ | |||
| 1 | package models | 1 | package models |
| 2 | 2 | ||
| 3 | type Preference struct { | 3 | type Preference struct { |
| 4 | ID int64 `json:"-"` | 4 | ID int64 `json:"id"` |
| 5 | Color string `json:"color"` | 5 | Color string `json:"color"` |
| 6 | UserID int64 `json:"-"` | 6 | UserID int64 `json:"user_id"` |
| 7 | Size Size `json:"size"` | 7 | SizeID int64 `json:"size_id"` |
| 8 | } | 8 | } |
| 9 | 9 | ||
| 10 | type Size struct { | 10 | type Size struct { |
| 11 | ID int64 `json:"-"` | 11 | ID int64 `json:"id"` |
| 12 | Size int64 `json:"size"` | 12 | Size int64 `json:"size"` |
| 13 | Unit string `json:"unit"` | 13 | Unit string `json:"unit"` |
| 14 | } | 14 | } |
diff --git a/api/internal/models/statistics.go b/api/internal/models/statistics.go index 457e6a0..7dceb3a 100644 --- a/api/internal/models/statistics.go +++ b/api/internal/models/statistics.go | |||
| @@ -3,7 +3,7 @@ package models | |||
| 3 | import "time" | 3 | import "time" |
| 4 | 4 | ||
| 5 | type Statistic struct { | 5 | type Statistic struct { |
| 6 | ID int64 `json:"-"` | 6 | ID int64 `json:"id"` |
| 7 | Date time.Time `json:"date"` | 7 | Date time.Time `json:"date"` |
| 8 | User User `json:"user"` | 8 | User User `json:"user"` |
| 9 | Quantity int `json:"quantity"` | 9 | Quantity int `json:"quantity"` |
diff --git a/api/internal/models/user.go b/api/internal/models/user.go index 2a3e6fd..ca5daa4 100644 --- a/api/internal/models/user.go +++ b/api/internal/models/user.go | |||
| @@ -3,7 +3,7 @@ package models | |||
| 3 | import "github.com/google/uuid" | 3 | import "github.com/google/uuid" |
| 4 | 4 | ||
| 5 | type User struct { | 5 | type User struct { |
| 6 | ID int64 `json:"-"` | 6 | ID int64 `json:"id"` |
| 7 | Name string `json:"name"` | 7 | Name string `json:"name"` |
| 8 | UUID uuid.UUID `json:"uuid"` | 8 | UUID uuid.UUID `json:"uuid"` |
| 9 | Password string `json:"-"` | 9 | Password string `json:"-"` |
diff --git a/api/internal/router/router.go b/api/internal/router/router.go index adf96d0..3c86b8c 100644 --- a/api/internal/router/router.go +++ b/api/internal/router/router.go | |||
| @@ -17,13 +17,14 @@ func SetupRouter() *gin.Engine { | |||
| 17 | api := r.Group("api/v1") | 17 | api := r.Group("api/v1") |
| 18 | 18 | ||
| 19 | api.POST("/auth", controllers.AuthHandler) | 19 | api.POST("/auth", controllers.AuthHandler) |
| 20 | api.GET("/sizes", middleware.TokenRequired(), controllers.GetSizes) | ||
| 21 | api.PATCH("/user/preferences", controllers.UpdateUserPreferences) | ||
| 20 | 22 | ||
| 21 | user := api.Group("/user/:uuid") | 23 | user := api.Group("/user/:id") |
| 22 | user.Use(middleware.TokenRequired()) | 24 | user.Use(middleware.TokenRequired()) |
| 23 | { | 25 | { |
| 24 | user.GET("", controllers.GetUser) | 26 | user.GET("", controllers.GetUser) |
| 25 | user.GET("preferences", controllers.GetUserPreferences) | 27 | user.GET("preferences", controllers.GetUserPreferences) |
| 26 | user.PATCH("preferences", controllers.UpdateUserPreferences) | ||
| 27 | } | 28 | } |
| 28 | 29 | ||
| 29 | stats := api.Group("/stats") | 30 | stats := api.Group("/stats") |
