initial commit
This commit is contained in:
96
internal/repository/postgres/auth/user_role.go
Normal file
96
internal/repository/postgres/auth/user_role.go
Normal file
@@ -0,0 +1,96 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/google/uuid"
|
||||
"go.uber.org/fx"
|
||||
"gorm.io/gorm"
|
||||
|
||||
domainAuth "base/internal/domain/auth"
|
||||
)
|
||||
|
||||
type userRoleRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewUserRoleRepository(lc fx.Lifecycle, db *gorm.DB) domainAuth.UserRoleRepository {
|
||||
lc.Append(
|
||||
fx.Hook{
|
||||
OnStart: func(ctx context.Context) error {
|
||||
return db.AutoMigrate(UserRoleModel{})
|
||||
},
|
||||
OnStop: func(ctx context.Context) error {
|
||||
return nil
|
||||
},
|
||||
})
|
||||
return &userRoleRepository{db: db}
|
||||
}
|
||||
|
||||
func (r *userRoleRepository) Create(ctx context.Context, userID, roleID uuid.UUID) error {
|
||||
model := &UserRoleModel{
|
||||
UserID: userID,
|
||||
RoleID: roleID,
|
||||
}
|
||||
return r.db.WithContext(ctx).Create(model).Error
|
||||
}
|
||||
|
||||
func (r *userRoleRepository) FindByUserID(ctx context.Context, userID uuid.UUID) ([]*domainAuth.Role, error) {
|
||||
var roleModels []RoleModel
|
||||
if err := r.db.WithContext(ctx).
|
||||
Table("roles").
|
||||
Joins("INNER JOIN user_roles ON roles.id = user_roles.role_id").
|
||||
Where("user_roles.user_id = ? AND user_roles.deleted_at IS NULL", userID).
|
||||
Find(&roleModels).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
roles := make([]*domainAuth.Role, len(roleModels))
|
||||
for i, model := range roleModels {
|
||||
roles[i] = toRoleDomain(&model)
|
||||
}
|
||||
return roles, nil
|
||||
}
|
||||
|
||||
func (r *userRoleRepository) FindByRoleID(ctx context.Context, roleID uuid.UUID) ([]*domainAuth.User, error) {
|
||||
var userModels []UserModel
|
||||
if err := r.db.WithContext(ctx).
|
||||
Table("users").
|
||||
Joins("INNER JOIN user_roles ON users.id = user_roles.user_id").
|
||||
Where("user_roles.role_id = ? AND user_roles.deleted_at IS NULL", roleID).
|
||||
Find(&userModels).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
users := make([]*domainAuth.User, len(userModels))
|
||||
for i, model := range userModels {
|
||||
users[i] = toUserDomain(&model)
|
||||
}
|
||||
return users, nil
|
||||
}
|
||||
|
||||
func (r *userRoleRepository) Delete(ctx context.Context, userID, roleID uuid.UUID) error {
|
||||
return r.db.WithContext(ctx).
|
||||
Where("user_id = ? AND role_id = ?", userID, roleID).
|
||||
Delete(&UserRoleModel{}).Error
|
||||
}
|
||||
|
||||
func (r *userRoleRepository) DeleteByUserID(ctx context.Context, userID uuid.UUID) error {
|
||||
return r.db.WithContext(ctx).
|
||||
Where("user_id = ?", userID).
|
||||
Delete(&UserRoleModel{}).Error
|
||||
}
|
||||
|
||||
func (r *userRoleRepository) DeleteByRoleID(ctx context.Context, roleID uuid.UUID) error {
|
||||
return r.db.WithContext(ctx).
|
||||
Where("role_id = ?", roleID).
|
||||
Delete(&UserRoleModel{}).Error
|
||||
}
|
||||
|
||||
func (r *userRoleRepository) Exists(ctx context.Context, userID, roleID uuid.UUID) (bool, error) {
|
||||
var count int64
|
||||
if err := r.db.WithContext(ctx).
|
||||
Model(&UserRoleModel{}).
|
||||
Where("user_id = ? AND role_id = ?", userID, roleID).
|
||||
Count(&count).Error; err != nil {
|
||||
return false, err
|
||||
}
|
||||
return count > 0, nil
|
||||
}
|
||||
Reference in New Issue
Block a user