more refactors
This commit is contained in:
@@ -2,7 +2,6 @@ package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
@@ -28,12 +27,6 @@ type AuditLog struct {
|
||||
User *User `bun:"rel:belongs-to,join:user_id=id"`
|
||||
}
|
||||
|
||||
type AuditLogs struct {
|
||||
AuditLogs []*AuditLog
|
||||
Total int
|
||||
PageOpts PageOpts
|
||||
}
|
||||
|
||||
// CreateAuditLog creates a new audit log entry
|
||||
func CreateAuditLog(ctx context.Context, tx bun.Tx, log *AuditLog) error {
|
||||
if log == nil {
|
||||
@@ -50,80 +43,64 @@ func CreateAuditLog(ctx context.Context, tx bun.Tx, log *AuditLog) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
type AuditLogFilters struct {
|
||||
UserID *int
|
||||
Action *string
|
||||
ResourceType *string
|
||||
Result *string
|
||||
type AuditLogFilter struct {
|
||||
*ListFilter
|
||||
}
|
||||
|
||||
func NewAuditLogFilter() *AuditLogFilter {
|
||||
return &AuditLogFilter{NewListFilter()}
|
||||
}
|
||||
|
||||
func (a *AuditLogFilter) UserID(id int) *AuditLogFilter {
|
||||
a.Add("al.user_id", id)
|
||||
return a
|
||||
}
|
||||
|
||||
func (a *AuditLogFilter) Action(action string) *AuditLogFilter {
|
||||
a.Add("al.action", action)
|
||||
return a
|
||||
}
|
||||
|
||||
func (a *AuditLogFilter) ResourceType(resourceType string) *AuditLogFilter {
|
||||
a.Add("al.resource_type", resourceType)
|
||||
return a
|
||||
}
|
||||
|
||||
func (a *AuditLogFilter) Result(result string) *AuditLogFilter {
|
||||
a.Add("al.result", result)
|
||||
return a
|
||||
}
|
||||
|
||||
// GetAuditLogs retrieves audit logs with optional filters and pagination
|
||||
func GetAuditLogs(ctx context.Context, tx bun.Tx, pageOpts *PageOpts, filters *AuditLogFilters) (*AuditLogs, error) {
|
||||
query := tx.NewSelect().
|
||||
Model((*AuditLog)(nil)).
|
||||
Relation("User")
|
||||
|
||||
// Apply filters if provided
|
||||
if filters != nil {
|
||||
if filters.UserID != nil {
|
||||
query = query.Where("al.user_id = ?", *filters.UserID)
|
||||
}
|
||||
if filters.Action != nil {
|
||||
query = query.Where("al.action = ?", *filters.Action)
|
||||
}
|
||||
if filters.ResourceType != nil {
|
||||
query = query.Where("al.resource_type = ?", *filters.ResourceType)
|
||||
}
|
||||
if filters.Result != nil {
|
||||
query = query.Where("al.result = ?", *filters.Result)
|
||||
}
|
||||
func GetAuditLogs(ctx context.Context, tx bun.Tx, pageOpts *PageOpts, filters *AuditLogFilter) (*List[AuditLog], error) {
|
||||
defaultPageOpts := &PageOpts{
|
||||
Page: 1,
|
||||
PerPage: 50,
|
||||
Order: bun.OrderDesc,
|
||||
OrderBy: "created_at",
|
||||
}
|
||||
|
||||
// Get total count
|
||||
total, err := query.Count(ctx)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "query.Count")
|
||||
}
|
||||
|
||||
// Get paginated results
|
||||
query, pageOpts = setPageOpts(query, pageOpts, 1, 50, bun.OrderDesc, "created_at")
|
||||
logs := new([]*AuditLog)
|
||||
err = query.Scan(ctx, &logs)
|
||||
if err != nil && err != sql.ErrNoRows {
|
||||
return nil, errors.Wrap(err, "query.Scan")
|
||||
}
|
||||
|
||||
list := &AuditLogs{
|
||||
AuditLogs: *logs,
|
||||
Total: total,
|
||||
PageOpts: *pageOpts,
|
||||
}
|
||||
|
||||
return list, nil
|
||||
return GetList[AuditLog](tx, pageOpts, defaultPageOpts).
|
||||
Relation("User").
|
||||
Filter(filters.filters...).
|
||||
GetAll(ctx)
|
||||
}
|
||||
|
||||
// GetAuditLogsByUser retrieves audit logs for a specific user
|
||||
func GetAuditLogsByUser(ctx context.Context, tx bun.Tx, userID int, pageOpts *PageOpts) (*AuditLogs, error) {
|
||||
func GetAuditLogsByUser(ctx context.Context, tx bun.Tx, userID int, pageOpts *PageOpts) (*List[AuditLog], error) {
|
||||
if userID <= 0 {
|
||||
return nil, errors.New("userID must be positive")
|
||||
}
|
||||
|
||||
filters := &AuditLogFilters{
|
||||
UserID: &userID,
|
||||
}
|
||||
filters := NewAuditLogFilter().UserID(userID)
|
||||
|
||||
return GetAuditLogs(ctx, tx, pageOpts, filters)
|
||||
}
|
||||
|
||||
// GetAuditLogsByAction retrieves audit logs for a specific action
|
||||
func GetAuditLogsByAction(ctx context.Context, tx bun.Tx, action string, pageOpts *PageOpts) (*AuditLogs, error) {
|
||||
func GetAuditLogsByAction(ctx context.Context, tx bun.Tx, action string, pageOpts *PageOpts) (*List[AuditLog], error) {
|
||||
if action == "" {
|
||||
return nil, errors.New("action cannot be empty")
|
||||
}
|
||||
|
||||
filters := &AuditLogFilters{
|
||||
Action: &action,
|
||||
}
|
||||
filters := NewAuditLogFilter().Action(action)
|
||||
|
||||
return GetAuditLogs(ctx, tx, pageOpts, filters)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user