Chuỗi bài: Go
go
32 dòng
· Cập nhật 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
}
Bài viết liên quan
Go
go
Cập nhật 2026-04-18
main.go
main.go — go source code from the Go learning materials (Go/Baasid/cmd/hashpw/main.go).
Đọc bài viết →
Go
go
Cập nhật 2026-04-18
main.go
main.go — go source code from the Go learning materials (Go/Baasid/cmd/server/main.go).
Đọc bài viết →
Go
go
Cập nhật 2026-04-18
docs.go
docs.go — go source code from the Go learning materials (Go/Baasid/docs/docs.go).
Đọc bài viết →
Go
go
Cập nhật 2026-04-18
jwt.go
jwt.go — go source code from the Go learning materials (Go/Baasid/internal/auth/jwt.go).
Đọc bài viết →
Go
go
Cập nhật 2026-04-18
password.go
password.go — go source code from the Go learning materials (Go/Baasid/internal/auth/password.go).
Đọc bài viết →
Go
go
Cập nhật 2026-04-18
password_test.go
password_test.go — go source code from the Go learning materials (Go/Baasid/internal/auth/password_test.go).
Đọc bài viết →