ulflow_phattt2901 f4ef71b63b
Some checks failed
CI Pipeline / Security Scan (push) Failing after 5m24s
CI Pipeline / Lint (push) Failing after 5m30s
CI Pipeline / Test (push) Has been skipped
CI Pipeline / Build (push) Has been skipped
CI Pipeline / Notification (push) Successful in 1s
feat: implement user authentication system with JWT and role-based access control
2025-05-24 11:24:19 +07:00

89 lines
2.5 KiB
Go

package persistence
import (
"context"
"errors"
"starter-kit/internal/domain/user"
"gorm.io/gorm"
)
type userRepository struct {
db *gorm.DB
}
// NewUserRepository tạo mới một instance của UserRepository
func NewUserRepository(db *gorm.DB) user.Repository {
return &userRepository{db: db}
}
func (r *userRepository) Create(ctx context.Context, u *user.User) error {
return r.db.WithContext(ctx).Create(u).Error
}
func (r *userRepository) GetByID(ctx context.Context, id string) (*user.User, error) {
var u user.User
err := r.db.WithContext(ctx).Preload("Roles").First(&u, "id = ?", id).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
}
return &u, err
}
func (r *userRepository) GetByUsername(ctx context.Context, username string) (*user.User, error) {
var u user.User
err := r.db.WithContext(ctx).Preload("Roles").First(&u, "username = ?", username).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
}
return &u, err
}
func (r *userRepository) GetByEmail(ctx context.Context, email string) (*user.User, error) {
var u user.User
err := r.db.WithContext(ctx).Preload("Roles").First(&u, "email = ?", email).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
}
return &u, err
}
func (r *userRepository) Update(ctx context.Context, u *user.User) error {
return r.db.WithContext(ctx).Save(u).Error
}
func (r *userRepository) Delete(ctx context.Context, id string) error {
return r.db.WithContext(ctx).Delete(&user.User{}, "id = ?", id).Error
}
func (r *userRepository) AddRole(ctx context.Context, userID string, roleID int) error {
return r.db.WithContext(ctx).Exec(
"INSERT INTO user_roles (user_id, role_id) VALUES (?, ?) ON CONFLICT DO NOTHING",
userID, roleID,
).Error
}
func (r *userRepository) RemoveRole(ctx context.Context, userID string, roleID int) error {
return r.db.WithContext(ctx).Exec(
"DELETE FROM user_roles WHERE user_id = ? AND role_id = ?",
userID, roleID,
).Error
}
func (r *userRepository) HasRole(ctx context.Context, userID string, roleID int) (bool, error) {
var count int64
err := r.db.WithContext(ctx).Model(&user.User{}).
Joins("JOIN user_roles ON user_roles.user_id = users.id").
Where("users.id = ? AND user_roles.role_id = ?", userID, roleID).
Count(&count).Error
return count > 0, err
}
func (r *userRepository) UpdateLastLogin(ctx context.Context, userID string) error {
now := gorm.Expr("NOW()")
return r.db.WithContext(ctx).Model(&user.User{}).
Where("id = ?", userID).
Update("last_login_at", now).Error
}