S SmartDocs
Serie: Go go 32 líneas · Actualizado 2026-04-18

user_repo.go

Go/Baasid/internal/repository/user_repo.go

// Package repository encapsulates all SQL access behind small, focused types.
//
// Handlers depend on repositories instead of *gorm.DB directly so you can swap
// in fakes during tests or add caching without rewriting HTTP layers.
package repository

import (
	"fmt"

	"baasid/internal/models"

	"gorm.io/gorm"
)

// UserRepository loads and saves SystemUser rows.
type UserRepository struct {
	db *gorm.DB
}

// NewUserRepository constructs a repository bound to gorm.DB.
func NewUserRepository(db *gorm.DB) *UserRepository {
	return &UserRepository{db: db}
}

// FindByAccount returns the user row for a login name, or gorm.ErrRecordNotFound.
func (r *UserRepository) FindByAccount(account string) (*models.SystemUser, error) {
	var u models.SystemUser
	if err := r.db.Where("account = ?", account).First(&u).Error; err != nil {
		return nil, fmt.Errorf("find user by account: %w", err)
	}
	return &u, nil
}

Artículos relacionados