Skip to content

Commit

Permalink
fix: homepage url endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
edotau committed May 1, 2024
1 parent c14739a commit bf6f564
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 48 deletions.
82 changes: 41 additions & 41 deletions backend/packages/api/userController.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,45 +120,45 @@ func Logout(c *fiber.Ctx) error {
}

func GetAllUsers(c *fiber.Ctx, dbConn *sql.DB) error {
// Define a slice to hold all user records
var users []*db.User

// Execute the query to fetch all users
rows, err := dbConn.Query(db.GetAllUsersQuery)
if err != nil {
// Handle any errors that occurred during the query execution
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"success": false,
"errors": []string{"Database error when fetching users", err.Error()},
})
}
defer rows.Close()

// Iterate over the rows and scan data into user structs
for rows.Next() {
var user db.User
if err := rows.Scan(&user.ID, &user.Name, &user.Email); err != nil {
// Handle any errors that occurred during row scanning
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"success": false,
"errors": []string{"Error scanning user data", err.Error()},
})
}
// Append the user struct to the slice
users = append(users, &user)
}

// Check for errors after iteration
if err = rows.Err(); err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"success": false,
"errors": []string{"Error iterating over user rows", err.Error()},
})
}

// Return the slice of user data as a JSON array
return c.Status(fiber.StatusOK).JSON(fiber.Map{
"success": true,
"users": users,
})
// Define a slice to hold all user records
var users []*db.User

// Execute the query to fetch all users
rows, err := dbConn.Query(db.GetAllUsersQuery)
if err != nil {
// Handle any errors that occurred during the query execution
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"success": false,
"errors": []string{"Database error when fetching users", err.Error()},
})
}
defer rows.Close()

// Iterate over the rows and scan data into user structs
for rows.Next() {
var user db.User
if err := rows.Scan(&user.ID, &user.Name, &user.Email); err != nil {
// Handle any errors that occurred during row scanning
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"success": false,
"errors": []string{"Error scanning user data", err.Error()},
})
}
// Append the user struct to the slice
users = append(users, &user)
}

// Check for errors after iteration
if err = rows.Err(); err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"success": false,
"errors": []string{"Error iterating over user rows", err.Error()},
})
}

// Return the slice of user data as a JSON array
return c.Status(fiber.StatusOK).JSON(fiber.Map{
"success": true,
"users": users,
})
}
9 changes: 4 additions & 5 deletions backend/packages/db/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ const (
LoginQuery = `SELECT * from users WHERE email = $1`
UpdateUserPasswordQuery = `UPDATE users SET password = $2 WHERE id = $1`
DeleteUser = `DELETE FROM users WHERE email = $1`
CreateUserQuery = `INSERT INTO users(id, name, password, email) VALUES (DEFAULT, $1 , $2, $3);`
GetUserByIDQuery = `SELECT * FROM users WHERE id = $1`
GetUserByEmailQuery = `SELECT * FROM users WHERE email = $1`
GetAllUsersQuery = `SELECT id, email, name FROM users;`

CreateUserQuery = `INSERT INTO users(id, name, password, email) VALUES (DEFAULT, $1 , $2, $3);`
GetUserByIDQuery = `SELECT * FROM users WHERE id = $1`
GetUserByEmailQuery = `SELECT * FROM users WHERE email = $1`
GetAllUsersQuery = `SELECT id, email, name FROM users;`
)
4 changes: 2 additions & 2 deletions ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "1.0",
"author": "auventuresllc",
"license": "MIT",
"homepage": "https://audotventures.github.io/",
"homepage": "https://edotau.github.io/",
"description": "Full stack application with golang, postgres, reactjs",
"private": true,
"dependencies": {
Expand All @@ -28,7 +28,7 @@
},
"scripts": {
"start": "react-scripts start",
"deploy": "gh-pages -d build",
"deploy": "react-scripts build && gh-pages -d build",
"lint": "yarn eslint ./src",
"clean": "yarn eslint --fix ./src",
"build": "react-scripts build"
Expand Down

0 comments on commit bf6f564

Please sign in to comment.