repository

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 23, 2025 License: MIT Imports: 8 Imported by: 0

README

Repository Directory

This directory contains the repository implementations used throughout the gFly application. Repositories provide an abstraction layer between the domain models and the database, encapsulating the logic for retrieving and persisting data.

Overview

Repositories in gFly follow a consistent structure:

  • Each repository is defined by an interface
  • Concrete implementations of these interfaces provide the actual data access logic
  • A repository pool is used to collect and access all repositories

Repository Pattern

The repository pattern is used to:

  • Decouple business logic from data access logic
  • Provide a consistent API for data access
  • Make testing easier by allowing repository implementations to be mocked
  • Centralize data access code to avoid duplication

Available Repositories

Role Repository

The RoleRepository implements the IRoleRepository interface and provides methods for working with user roles.

Interface Methods:

  • GetRolesByUserID(userID int) []models.Role: Retrieves all roles assigned to a specific user
  • AddRoleForUserID(userID int, slug string) error: Assigns a role to a user by role slug

Implementation Details:

  • Uses the model builder (mb) for database operations
  • Uses the query builder (`qb.) for constructing SQL queries
  • Handles errors and logs them appropriately

Repository Pool

The repository pool is a singleton that provides access to all repositories in the application:

// Repositories struct for collect all app repositories.
type Repositories struct {
    IRoleRepository
}

// Pool a repository pool to store all
var Pool = &Repositories{
    &RoleRepository{},
}

Usage Example

// Get roles for a user
roles := repository.Pool.GetRolesByUserID(userId)

// Add a role to a user
err := repository.Pool.AddRoleForUserID(userId, "admin")
if err != nil {
    // Handle error
}

Creating New Repositories

To create a new repository:

  1. Define an interface for the repository
  2. Create a struct that implements the interface
  3. Add the interface to the Repositories struct
  4. Initialize the implementation in the Pool variable

Example:

// Define the interface
type IUserRepository interface {
    GetUserByID(id int) (*models.User, error)
    // Other methods...
}

// Create the implementation
type UserRepository struct {
}

// Implement the methods
func (q *UserRepository) GetUserByID(id int) (*models.User, error) {
    // Implementation...
}

// Update the Repositories struct and Pool
type Repositories struct {
    IRoleRepository
    IUserRepository
}

var Pool = &Repositories{
    &RoleRepository{},
    &UserRepository{},
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Pool = &Repositories{
	&roleRepository{},
	&userRepository{},
}

Pool a repository pool to store all

Functions

func Convert

func Convert[T fmt.Stringer](items []T) []string

Types

type IRoleRepository

type IRoleRepository interface {
	// GetRolesByUserID retrieves all roles associated with the given user ID.
	// Returns a slice of Role models containing the role data.
	//
	// Parameters:
	//   - userID (int): The unique identifier of the user
	//
	// Returns:
	//   - []models.Role: Slice of Role models containing the role data
	GetRolesByUserID(userID int) []models.Role

	// GetRolesBySlug retrieves a list of roles that match the specified slug values.
	// Parameters:
	//   - roleSlugs (...types.Role): A variadic parameter representing the slugs to filter roles by.
	// Returns:
	//   - ([]models.Role): A slice of roles matching the provided slugs.
	GetRolesBySlug(roleSlugs ...types.Role) []models.Role

	// AddRoleForUserID assigns a role to a user by their ID.
	// Creates a new user-role mapping in the database.
	//
	// Parameters:
	//   - userID (int): The unique identifier of the user
	//   - roleSlug (types.Role): The slug name of the role to assign
	//
	// Returns:
	//   - error: Returns nil on success, error on failure
	AddRoleForUserID(userID int, roleSlug types.Role) error

	// SyncRolesWithUser synchronizes ro
	// les for a given user.
	// It associates the user with the specified roles by slug.
	// Parameters:
	//   - userID (int): The unique identifier of the user.
	//   - roleSlugs (...types.Role): A variadic parameter representing the slugs of roles to synchronize.
	// Returns:
	//   - (error): An error if the synchronization fails.
	SyncRolesWithUser(userID int, roleSlugs ...types.Role) (err error)
}

IRoleRepository defines the interface for managing user roles. It provides methods for retrieving and assigning roles to users.

Methods:

  • GetRolesByUserID(userID int) []models.Role: Retrieves roles of specific user. Retrieves all roles associated with a specific user ID.
  • GetRolesBySlug(roleSlugs ...types.Role) []models.Role: Retrieves a list of roles that match the specified slug values.
  • AddRoleForUserID(userID int, roleSlug types.Role) error: Add a role via slug for user. Creates a new user-role mapping in the database.
  • SyncRolesWithUser(userID int, roleSlugs ...types.Role) (err error): Synchronizes roles for a given user, associating the user with the specified roles.

type IUserRepository

type IUserRepository interface {
	// GetUserByEmail retrieves a user by their email address.
	// Parameters:
	//   - email (string): The email address of the user to retrieve.
	//
	// Returns:
	//   - (*models.User): The user associated with the given email address, or nil if not found.
	GetUserByEmail(email string) *models.User

	// GetUserByToken retrieves a user based on their authentication token.
	// Parameters:
	//   - token (string): The authentication token of the user to retrieve.
	//
	// Returns:
	//   - (*models.User): The user associated with the given token, or nil if not found.
	GetUserByToken(token string) *models.User
}

IUserRepository represents the interface for managing user-related data in the repository. It provides methods for CRUD operations and additional utility functions.

Methods:

  • GetUserByEmail(email string) *models.User: Retrieves a user by their email address.
  • GetUserByToken(token string) *models.User: Retrieves a user by their authentication token.
  • SelectUser(page, limit int) ([]*models.User, int, error): Retrieves a paginated list of users with the total count.

type Repositories

type Repositories struct {
	IRoleRepository
	IUserRepository
}

Repositories struct for collect all app repositories.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL