refactor: separate backend domains and storage adapters
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
package data
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"kra/internal/biz"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type operationPO struct {
|
||||
ID uint `gorm:"primaryKey"`
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
DeletedAt gorm.DeletedAt `gorm:"index"`
|
||||
IP, Method, Path string
|
||||
Status int
|
||||
LatencyMS int64 `gorm:"column:latency_ms"`
|
||||
Agent string `gorm:"type:text"`
|
||||
ErrorMessage string
|
||||
Body string `gorm:"type:text"`
|
||||
Response string `gorm:"column:resp;type:text"`
|
||||
UserID uint
|
||||
RequestID string `gorm:"index"`
|
||||
TraceID string `gorm:"index"`
|
||||
DeviceID string
|
||||
}
|
||||
|
||||
func (operationPO) TableName() string { return "sys_operation_records" }
|
||||
|
||||
func (r *auditRepo) RecordOperation(ctx context.Context, v *biz.OperationRecord) error {
|
||||
return r.data.gormDB.WithContext(ctx).Create(&operationPO{IP: v.IP, Method: v.Method, Path: v.Path, Status: v.Status, LatencyMS: v.LatencyMS, Agent: v.Agent, ErrorMessage: v.ErrorMessage, Body: v.Body, Response: v.Response, UserID: v.UserID, RequestID: v.RequestID, TraceID: v.TraceID, DeviceID: v.DeviceID}).Error
|
||||
}
|
||||
func opFromPO(v operationPO) *biz.OperationRecord {
|
||||
return &biz.OperationRecord{ID: v.ID, CreatedAt: v.CreatedAt, IP: v.IP, Method: v.Method, Path: v.Path, Status: v.Status, LatencyMS: v.LatencyMS, Agent: v.Agent, ErrorMessage: v.ErrorMessage, Body: v.Body, Response: v.Response, UserID: v.UserID, RequestID: v.RequestID, TraceID: v.TraceID, DeviceID: v.DeviceID}
|
||||
}
|
||||
func (r *auditRepo) ListOperations(ctx context.Context, page, size int, q *biz.OperationRecord) ([]*biz.OperationRecord, int64, error) {
|
||||
if page < 1 {
|
||||
page = 1
|
||||
}
|
||||
if size < 1 {
|
||||
size = 10
|
||||
}
|
||||
db := r.data.gormDB.WithContext(ctx).Model(&operationPO{})
|
||||
if q != nil {
|
||||
if q.Path != "" {
|
||||
db = db.Where("path LIKE ?", "%"+q.Path+"%")
|
||||
}
|
||||
if q.Method != "" {
|
||||
db = db.Where("method = ?", q.Method)
|
||||
}
|
||||
if q.Status != 0 {
|
||||
db = db.Where("status = ?", q.Status)
|
||||
}
|
||||
if q.IP != "" {
|
||||
db = db.Where("ip LIKE ?", "%"+q.IP+"%")
|
||||
}
|
||||
if q.UserID != 0 {
|
||||
db = db.Where("user_id = ?", q.UserID)
|
||||
}
|
||||
}
|
||||
var total int64
|
||||
if err := db.Count(&total).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
var pos []operationPO
|
||||
if err := db.Order("id desc").Offset((page - 1) * size).Limit(size).Find(&pos).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
out := make([]*biz.OperationRecord, 0, len(pos))
|
||||
for _, po := range pos {
|
||||
out = append(out, opFromPO(po))
|
||||
}
|
||||
return out, total, nil
|
||||
}
|
||||
func (r *auditRepo) FindOperation(ctx context.Context, id uint) (*biz.OperationRecord, error) {
|
||||
var po operationPO
|
||||
if err := r.data.gormDB.WithContext(ctx).First(&po, id).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return opFromPO(po), nil
|
||||
}
|
||||
func (r *auditRepo) DeleteOperations(ctx context.Context, ids []uint) error {
|
||||
return r.data.gormDB.WithContext(ctx).Delete(&operationPO{}, ids).Error
|
||||
}
|
||||
Reference in New Issue
Block a user