User Repository
Documentation for the User Repository in the ClueQuest SDK for Go.
Structure
The UserRepository
struct is defined as follows:
type UserRepository struct {
db *gorm.DB
}
GetAllUsers
The GetAllUsers
method retrieves all users from the database.
Function
func (r *UserRepository) GetAllUsers() ([]models.User, error) {
var users []models.User
if err := r.getDB().Find(&users).Error; err != nil {
return nil, err
}
return users, nil
}
Usage
You can use the GetAllUsers
method as follows:
userRepository := repositories.NewUserRepository(db)
users, err := userRepository.GetAllUsers()
if err != nil {
// handle error
}
for _, user := range users {
fmt.Println(user.Name)
}
GetUserByID
The GetUserByID
method retrieves a user by their ID.
Function
func (r *UserRepository) GetUserByID(id uuid.UUID) (*models.User, error) {
var user models.User
if err := r.getDB().First(&user, "id = ?", id).Error; err != nil {
return nil, err
}
return &user, nil
}
Usage
You can use the GetUserByID
method as follows:
userRepository := repositories.NewUserRepository(db)
user, err := userRepository.GetUserByID(userID)
if err != nil {
// handle error
}
fmt.Println(user.Name)
GetUserByEmail
The GetUserByEmail
method retrieves a user by their email address.
Function
func (r *UserRepository) GetUserByEmail(email string) (*models.User, error) {
var user models.User
if err := r.getDB().First(&user, "email = ?", email).Error; err != nil {
return nil, err
}
return &user, nil
}
Usage
You can use the GetUserByEmail
method as follows:
userRepository := repositories.NewUserRepository(db)
user, err := userRepository.GetUserByEmail(email)
if err != nil {
// handle error
}
fmt.Println(user.Name)
CreateUser
The CreateUser
method creates a new user in the database.
Function
func (r *UserRepository) CreateUser(user *models.User) error {
return r.getDB().Create(user).Error
}
Usage
You can use the CreateUser
method as follows:
userRepository := repositories.NewUserRepository(db)
newUser := &models.User{
FirstName: "John",
LastName: "Doe",
Username: "johndoe",
Email: "john.doe@example.com",
Password: "securepassword", // Will be hashed with Argon2 before saving
}
if err := userRepository.CreateUser(newUser); err != nil {
// handle error
}
fmt.Println("User created with ID:", newUser.ID)
UpdateUser
The UpdateUser
method updates an existing user in the database.
Function
func (r *UserRepository) UpdateUser(user *models.User) error {
return r.getDB().Save(user).Error
}
Usage
You can use the UpdateUser
method as follows:
userRepository := repositories.NewUserRepository(db)
user, err := userRepository.GetUserByID(userID)
if err != nil {
// handle error
}
user.LastName = "Smith"
if err := userRepository.UpdateUser(user); err != nil {
// handle error
}
fmt.Println("User updated:", user)
UpdatePassword
The UpdatePassword
method updates a user's password in the database.
Function
func (r *UserRepository) UpdatePassword(id uuid.UUID, password string) error {
return r.getDB().Model(&models.User{}).Where("id = ?", id).Update("password", password).Error
}
Usage
You can use the UpdatePassword
method as follows:
userRepository := repositories.NewUserRepository(db)
if err := userRepository.UpdatePassword(userID, "newsecurepassword"); err != nil {
// handle error
}
fmt.Println("Password updated for user ID:", userID)
DeleteUser
The DeleteUser
method deletes a user from the database.
Function
func (r *UserRepository) DeleteUser(id uuid.UUID) error {
return r.getDB().Delete(&models.User{}, "id = ?", id).Error
}
Usage
You can use the DeleteUser
method as follows:
userRepository := repositories.NewUserRepository(db)
if err := userRepository.DeleteUser(userID); err != nil {
// handle error
}
fmt.Println("User deleted with ID:", userID)
CountUsers
The CountUsers
method returns the total number of users in the database.
Function
func (r *UserRepository) CountUsers() (int64, error) {
var count int64
if err := r.getDB().Model(&models.User{}).Count(&count).Error; err != nil {
return 0, err
}
return count, nil
}
Usage
You can use the CountUsers
method as follows:
userRepository := repositories.NewUserRepository(db)
count, err := userRepository.CountUsers()
if err != nil {
// handle error
}
fmt.Println("Total users:", count)
getDB
The getDB
method returns the database connection.
Function
func (r *UserRepository) getDB() *gorm.DB {
return r.db
}