Skip to content

Instantly share code, notes, and snippets.

@harryparkinson101
Created February 1, 2023 17:42
Show Gist options
  • Save harryparkinson101/fe5c15e352740322bf63a7083a6053a9 to your computer and use it in GitHub Desktop.
Save harryparkinson101/fe5c15e352740322bf63a7083a6053a9 to your computer and use it in GitHub Desktop.
// This is an example of some badly written code. What would you refactor?
type User struct {
Uuid string
FirstName string `db:"first_name"`
LastName string `db:"last_name"`
Email string
Password string
Address *Address
}
type Address struct {
Uuid string
UserUuid uint `db:"user_uuid"`
Address1 string
Address2 string
City string
Postcode string
}
func OrderBYAndGetAll(orderBy string) ([]User, error) {
db, _ := sqlx.Connect("postgres", "user=prod password=prodPass741 dbname=acme sslmode=disable")
getUsersQuery := "SELECT * FROM users"
// Add the order by if it has been passed though
if orderBy != "" {
getUsersQuery += " ORDER BY " + orderBy + " ASC"
}
users := []User{}
db.Select(&users, getUsersQuery)
// Find the address for each user
for _, user := range users {
db.Select(&user.Address, "select * from addresses where user_uuid = "+user.Uuid)
}
return users, nil
}
func GetAllUsers() ([]User, error) {
// db, _ := sqlx.Connect("postgres", "user=dev password=password dbname=acme sslmode=disable")
// getUsersQuery := "SELECT * FROM users"
// users := []User{}
// db.Select(&users, getUsersQuery)
db, _ := sqlx.Connect("postgres", "user=prod password=prodPass741 dbname=acme sslmode=disable")
getUsersQuery := "SELECT * FROM users"
users := []User{}
db.Select(&users, getUsersQuery)
// Find the address for each user
for _, user := range users {
err := db.Select(&user.Address, "select * from addresses where user_uuid = "+user.Uuid)
}
return users, nil
}
// This is my refactored version of the code
package main
import "github.com/jmoiron/sqlx"
// User struct defines the fields for a user
type User struct {
Uuid string
FirstName string `db:"first_name"`
LastName string `db:"last_name"`
Email string
Password string
Address *Address
}
// Address struct defines the fields for an address
type Address struct {
Uuid string
UserUuid uint `db:"user_uuid"`
Address1 string
Address2 string
City string
Postcode string
}
// OrderBYAndGetAll retrieves all users from the database and returns the result ordered by the specified column name
func OrderBYAndGetAll(orderBy string) ([]User, error) {
db, err := sqlx.Connect("postgres", "user=prod password=prodPass741 dbname=acme sslmode=disable")
if err != nil {
return nil, err
}
defer db.Close()
// Start the query with a SELECT * FROM users statement
getUsersQuery := "SELECT * FROM users"
// If orderBy parameter is provided, add an ORDER BY clause to the query
if orderBy != "" {
getUsersQuery += " ORDER BY " + orderBy + " ASC"
}
// Execute the query and store the result in the `users` variable
var users []User
if err := db.Select(&users, getUsersQuery); err != nil {
return nil, err
}
// Loop through each user and retrieve their associated address
for i := range users {
if err := db.Select(&users[i].Address, "SELECT * FROM addresses WHERE user_uuid = $1", users[i].Uuid); err != nil {
return nil, err
}
}
return users, nil
}
// GetAllUsers retrieves all users from the database
func GetAllUsers() ([]User, error) {
db, err := sqlx.Connect("postgres", "user=prod password=prodPass741 dbname=acme sslmode=disable")
if err != nil {
return nil, err
}
defer db.Close()
// Execute the query and store the result in the `users` variable
var users []User
if err := db.Select(&users, "SELECT * FROM users"); err != nil {
return nil, err
}
// Loop through each user and retrieve their associated address
for i := range users {
if err := db.Select(&users[i].Address, "SELECT * FROM addresses WHERE user_uuid = $1", users[i].Uuid); err != nil {
return nil, err
}
}
return users, nil
}
// Moved database connection and query execution code into separate functions
// Checked for and handled errors during database operations
// Used parameterized queries to prevent SQL injection attacks
// Used the defer keyword to close the database connection when the function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment