You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
73 lines
1.9 KiB
73 lines
1.9 KiB
package repository |
|
|
|
import ( |
|
"gofaster/internal/shared/model" |
|
"gofaster/internal/shared/repository" |
|
|
|
"gorm.io/gorm" |
|
) |
|
|
|
type ActionLogRepo struct { |
|
repository.BaseRepo |
|
} |
|
|
|
func NewActionLogRepo(db *gorm.DB) *ActionLogRepo { |
|
return &ActionLogRepo{BaseRepo: *repository.NewBaseRepo(db)} |
|
} |
|
|
|
func (r *ActionLogRepo) Create(log *model.ActionLog) error { |
|
return r.DB().Create(log).Error |
|
} |
|
|
|
func (r *ActionLogRepo) GetByID(id uint) (*model.ActionLog, error) { |
|
var log model.ActionLog |
|
err := r.DB().First(&log, id).Error |
|
return &log, err |
|
} |
|
|
|
func (r *ActionLogRepo) ListByUser(userID uint, page, pageSize int) ([]model.ActionLog, int64, error) { |
|
var logs []model.ActionLog |
|
var count int64 |
|
|
|
err := r.DB().Model(&model.ActionLog{}).Where("user_id = ?", userID).Count(&count).Error |
|
if err != nil { |
|
return nil, 0, err |
|
} |
|
|
|
offset := (page - 1) * pageSize |
|
err = r.DB().Where("user_id = ?", userID).Offset(offset).Limit(pageSize).Order("created_at DESC").Find(&logs).Error |
|
return logs, count, err |
|
} |
|
|
|
func (r *ActionLogRepo) List(page, pageSize int) ([]model.ActionLog, int64, error) { |
|
var logs []model.ActionLog |
|
var count int64 |
|
|
|
err := r.DB().Model(&model.ActionLog{}).Count(&count).Error |
|
if err != nil { |
|
return nil, 0, err |
|
} |
|
|
|
offset := (page - 1) * pageSize |
|
err = r.DB().Offset(offset).Limit(pageSize).Order("created_at DESC").Find(&logs).Error |
|
return logs, count, err |
|
} |
|
|
|
func (r *ActionLogRepo) Search(query string, page, pageSize int) ([]model.ActionLog, int64, error) { |
|
var logs []model.ActionLog |
|
var count int64 |
|
|
|
searchQuery := "%" + query + "%" |
|
countQuery := r.DB().Model(&model.ActionLog{}). |
|
Where("username LIKE ? OR action LIKE ? OR path LIKE ? OR method LIKE ?", |
|
searchQuery, searchQuery, searchQuery, searchQuery) |
|
|
|
err := countQuery.Count(&count).Error |
|
if err != nil { |
|
return nil, 0, err |
|
} |
|
|
|
offset := (page - 1) * pageSize |
|
err = countQuery.Offset(offset).Limit(pageSize).Order("created_at DESC").Find(&logs).Error |
|
return logs, count, err |
|
}
|
|
|