35 lines
957 B
Go
35 lines
957 B
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/google/uuid"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type UserDepartmentRepository struct{ db *gorm.DB }
|
|
|
|
func NewUserDepartmentRepository(db *gorm.DB) *UserDepartmentRepository {
|
|
return &UserDepartmentRepository{db: db}
|
|
}
|
|
|
|
type userDepartmentRow struct {
|
|
UserID uuid.UUID `gorm:"column:user_id"`
|
|
DepartmentID uuid.UUID `gorm:"column:department_id"`
|
|
}
|
|
|
|
// ListActiveByDepartmentIDs returns active user-department memberships for given department IDs.
|
|
func (r *UserDepartmentRepository) ListActiveByDepartmentIDs(ctx context.Context, departmentIDs []uuid.UUID) ([]userDepartmentRow, error) {
|
|
db := DBFromContext(ctx, r.db)
|
|
rows := make([]userDepartmentRow, 0)
|
|
if len(departmentIDs) == 0 {
|
|
return rows, nil
|
|
}
|
|
err := db.WithContext(ctx).
|
|
Table("user_department").
|
|
Select("user_id, department_id").
|
|
Where("department_id IN ? AND removed_at IS NULL", departmentIDs).
|
|
Find(&rows).Error
|
|
return rows, err
|
|
}
|