From eb51bdbfef8c2aacf0fdfde279a40de7f74c8d86 Mon Sep 17 00:00:00 2001 From: Zach Berwaldt Date: Mon, 18 Mar 2024 21:27:24 -0400 Subject: clean up, add better error handling --- api/internal/controllers/auth.go | 21 +++++++++++++-------- api/internal/controllers/preferences.go | 13 +++++++++---- api/internal/controllers/stats.go | 27 ++++++++++++++++++++++----- api/internal/controllers/user.go | 14 +++++++++++--- 4 files changed, 55 insertions(+), 20 deletions(-) (limited to 'api/internal/controllers') diff --git a/api/internal/controllers/auth.go b/api/internal/controllers/auth.go index ab2fbbb..b06c6ef 100644 --- a/api/internal/controllers/auth.go +++ b/api/internal/controllers/auth.go @@ -5,21 +5,21 @@ import ( "database/sql" "encoding/base64" "errors" - "github.com/gin-gonic/gin" "net/http" "water/api/internal/models" + "github.com/gin-gonic/gin" + + "water/api/internal/database" + _ "github.com/mattn/go-sqlite3" "golang.org/x/crypto/bcrypt" - "water/api/internal/database" ) - - // AuthHandler is a function that handles users' authentication. It checks if the request // has valid credentials, authenticates the user and sets the user's session. // If the authentication is successful, it will allow the user to access protected routes. -func AuthHandler (c *gin.Context) { +func AuthHandler(c *gin.Context) { username, password, ok := c.Request.BasicAuth() if !ok { c.Header("WWW-Authenticate", `Basic realm="Please enter your username and password."`) @@ -27,7 +27,11 @@ func AuthHandler (c *gin.Context) { return } - db := database.EstablishDBConnection() + 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 { @@ -44,6 +48,8 @@ func AuthHandler (c *gin.Context) { 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()}) } } @@ -64,7 +70,6 @@ func AuthHandler (c *gin.Context) { c.JSON(http.StatusOK, gin.H{"token": apiToken, "user": user, "preferences": preference}) } - // generateToken is a helper function used in the AuthHandler. It generates a random token for API authentication. // This function creates an empty byte slice of length 32 and fills it with cryptographic random data using the rand.Read function. // If an error occurs during the generation, it will return an empty string. @@ -76,4 +81,4 @@ func generateToken() string { return "" } return base64.StdEncoding.EncodeToString(token) -} \ No newline at end of file +} diff --git a/api/internal/controllers/preferences.go b/api/internal/controllers/preferences.go index a1bcf4f..da52e74 100644 --- a/api/internal/controllers/preferences.go +++ b/api/internal/controllers/preferences.go @@ -1,22 +1,27 @@ package controllers import ( - "github.com/gin-gonic/gin" - "net/http" "database/sql" + "net/http" "water/api/internal/database" "water/api/internal/models" + + "github.com/gin-gonic/gin" ) func GetSizes(c *gin.Context) { - db := database.EstablishDBConnection() + 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()}) } }(db) - + rows, err := db.Query("SELECT id, size, unit FROM Sizes") if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) diff --git a/api/internal/controllers/stats.go b/api/internal/controllers/stats.go index 2234787..889b923 100644 --- a/api/internal/controllers/stats.go +++ b/api/internal/controllers/stats.go @@ -2,10 +2,11 @@ package controllers import ( "database/sql" - "github.com/gin-gonic/gin" "net/http" "water/api/internal/database" "water/api/internal/models" + + "github.com/gin-gonic/gin" ) // TODO: add comments to all exported members of package. @@ -13,7 +14,11 @@ import ( // GetAllStatistics connects to the database and queries for all statistics in the database. // If none have been found it will return an error, otherwise a 200 code is sent along with the list of statistics. func GetAllStatistics(c *gin.Context) { - db := database.EstablishDBConnection() + 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 { @@ -59,7 +64,11 @@ func PostNewStatistic(c *gin.Context) { return } - db := database.EstablishDBConnection() + 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 { @@ -83,7 +92,11 @@ func PostNewStatistic(c *gin.Context) { } func GetWeeklyStatistics(c *gin.Context) { - db := database.EstablishDBConnection() + 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 { @@ -118,7 +131,11 @@ func GetWeeklyStatistics(c *gin.Context) { } func GetDailyUserStatistics(c *gin.Context) { - db := database.EstablishDBConnection() + 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 { diff --git a/api/internal/controllers/user.go b/api/internal/controllers/user.go index dbb09cf..fa9617a 100644 --- a/api/internal/controllers/user.go +++ b/api/internal/controllers/user.go @@ -15,7 +15,11 @@ func GetUser(c *gin.Context) { c.JSON(http.StatusOK, gin.H{"message": "User found"}) } func GetUserPreferences(c *gin.Context) { - db := database.EstablishDBConnection() + 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 { @@ -41,7 +45,11 @@ func GetUserPreferences(c *gin.Context) { } func UpdateUserPreferences(c *gin.Context) { - db := database.EstablishDBConnection() + 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 { @@ -58,7 +66,7 @@ func UpdateUserPreferences(c *gin.Context) { log.Printf("newPreferences: %v", newPreferences) - _, err := db.Exec("UPDATE Preferences SET color = ?, size_id = ? WHERE id = ?", newPreferences.Color, newPreferences.SizeID, newPreferences.ID) + _, 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 -- cgit v1.1