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.
72 lines
1.8 KiB
72 lines
1.8 KiB
package repository |
|
|
|
import ( |
|
"gofaster/internal/model" |
|
|
|
"gorm.io/gorm" |
|
) |
|
|
|
type ActionLogRepo struct { |
|
BaseRepo |
|
} |
|
|
|
func NewActionLogRepo(db *gorm.DB) *ActionLogRepo { |
|
return &ActionLogRepo{BaseRepo{db: 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 |
|
}
|
|
|