90 lines
2.7 KiB
Go
90 lines
2.7 KiB
Go
package repository
|
|
|
|
import (
|
|
"users_management/m/model/entity"
|
|
"github.com/google/uuid"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type ActivityLogRepo interface {
|
|
Create(log entity.ActivityLog) error
|
|
GetByUserID(userID uuid.UUID, limit, offset int) ([]entity.ActivityLog, error)
|
|
GetAllForAdmins(limit, offset int) ([]entity.ActivityLog, error)
|
|
GetByUserRole(role string, limit, offset int) ([]entity.ActivityLog, error)
|
|
CountByUserID(userID uuid.UUID) (int64, error)
|
|
CountAll() (int64, error)
|
|
CountByUserRole(role string) (int64, error)
|
|
}
|
|
|
|
type activityLogRepo struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
func NewActivityLogRepo(db *gorm.DB) ActivityLogRepo {
|
|
return &activityLogRepo{
|
|
db: db,
|
|
}
|
|
}
|
|
|
|
func (r *activityLogRepo) Create(log entity.ActivityLog) error {
|
|
return r.db.Create(&log).Error
|
|
}
|
|
|
|
func (r *activityLogRepo) GetByUserID(userID uuid.UUID, limit, offset int) ([]entity.ActivityLog, error) {
|
|
var logs []entity.ActivityLog
|
|
err := r.db.Where("user_id = ?", userID).
|
|
Preload("User").
|
|
Preload("User.Role").
|
|
Order("timestamp DESC").
|
|
Limit(limit).
|
|
Offset(offset).
|
|
Find(&logs).Error
|
|
return logs, err
|
|
}
|
|
|
|
func (r *activityLogRepo) GetAllForAdmins(limit, offset int) ([]entity.ActivityLog, error) {
|
|
var logs []entity.ActivityLog
|
|
err := r.db.Preload("User").
|
|
Preload("User.Role").
|
|
Order("timestamp DESC").
|
|
Limit(limit).
|
|
Offset(offset).
|
|
Find(&logs).Error
|
|
return logs, err
|
|
}
|
|
|
|
func (r *activityLogRepo) GetByUserRole(role string, limit, offset int) ([]entity.ActivityLog, error) {
|
|
var logs []entity.ActivityLog
|
|
err := r.db.Joins("JOIN users ON users.id = activity_logs.user_id").
|
|
Joins("JOIN roles ON roles.id = users.role_id").
|
|
Where("roles.name = ?", role).
|
|
Preload("User").
|
|
Preload("User.Role").
|
|
Order("activity_logs.timestamp DESC").
|
|
Limit(limit).
|
|
Offset(offset).
|
|
Find(&logs).Error
|
|
return logs, err
|
|
}
|
|
|
|
func (r *activityLogRepo) CountByUserID(userID uuid.UUID) (int64, error) {
|
|
var count int64
|
|
err := r.db.Model(&entity.ActivityLog{}).Where("user_id = ?", userID).Count(&count).Error
|
|
return count, err
|
|
}
|
|
|
|
func (r *activityLogRepo) CountAll() (int64, error) {
|
|
var count int64
|
|
err := r.db.Model(&entity.ActivityLog{}).Count(&count).Error
|
|
return count, err
|
|
}
|
|
|
|
func (r *activityLogRepo) CountByUserRole(role string) (int64, error) {
|
|
var count int64
|
|
err := r.db.Model(&entity.ActivityLog{}).
|
|
Joins("JOIN users ON users.id = activity_logs.user_id").
|
|
Joins("JOIN roles ON roles.id = users.role_id").
|
|
Where("roles.name = ?", role).
|
|
Count(&count).Error
|
|
return count, err
|
|
} |