137 lines
3.9 KiB
Go
137 lines
3.9 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/geo-platform/tenant-api/internal/tenant/repository/generated"
|
|
)
|
|
|
|
type AuditLogInput struct {
|
|
OperatorID int64
|
|
TenantID *int64
|
|
Module string
|
|
Action string
|
|
ResourceType *string
|
|
ResourceID *int64
|
|
RequestID *string
|
|
BeforeJSON []byte
|
|
AfterJSON []byte
|
|
Result *string
|
|
ErrorMessage *string
|
|
}
|
|
|
|
type GenerationTaskInput struct {
|
|
OperatorID int64
|
|
TenantID int64
|
|
ArticleID *int64
|
|
QuotaReservationID *int64
|
|
TaskType string
|
|
RequestHash *string
|
|
InputParamsJSON []byte
|
|
}
|
|
|
|
type TaskRecordInput struct {
|
|
TenantID int64
|
|
OperatorID int64
|
|
TaskType string
|
|
ResourceType string
|
|
ResourceID int64
|
|
Status string
|
|
ErrorMessage *string
|
|
}
|
|
|
|
type GenerationTaskStatusInput struct {
|
|
ID int64
|
|
TenantID int64
|
|
Status string
|
|
ErrorMessage *string
|
|
StartedAt *time.Time
|
|
CompletedAt *time.Time
|
|
}
|
|
|
|
type AuditRepository interface {
|
|
CreateAuditLog(ctx context.Context, input AuditLogInput) error
|
|
CreateGenerationTask(ctx context.Context, input GenerationTaskInput) (int64, error)
|
|
CreateTaskRecord(ctx context.Context, input TaskRecordInput) (int64, error)
|
|
UpdateTaskRecordStatus(ctx context.Context, input TaskRecordInput) error
|
|
UpdateGenerationTaskStatus(ctx context.Context, input GenerationTaskStatusInput) error
|
|
}
|
|
|
|
type auditRepository struct {
|
|
q generated.Querier
|
|
}
|
|
|
|
func NewAuditRepository(db generated.DBTX) AuditRepository {
|
|
return &auditRepository{q: newQuerier(db)}
|
|
}
|
|
|
|
func (r *auditRepository) CreateAuditLog(ctx context.Context, input AuditLogInput) error {
|
|
return r.q.CreateAuditLog(ctx, generated.CreateAuditLogParams{
|
|
OperatorID: input.OperatorID,
|
|
TenantID: pgInt8(input.TenantID),
|
|
Module: input.Module,
|
|
Action: input.Action,
|
|
ResourceType: pgText(input.ResourceType),
|
|
ResourceID: pgInt8(input.ResourceID),
|
|
RequestID: pgText(input.RequestID),
|
|
BeforeJson: input.BeforeJSON,
|
|
AfterJson: input.AfterJSON,
|
|
Result: pgText(input.Result),
|
|
ErrorMessage: pgText(input.ErrorMessage),
|
|
})
|
|
}
|
|
|
|
func (r *auditRepository) CreateGenerationTask(ctx context.Context, input GenerationTaskInput) (int64, error) {
|
|
var operatorID *int64
|
|
if input.OperatorID > 0 {
|
|
operatorID = &input.OperatorID
|
|
}
|
|
return r.q.CreateGenerationTask(ctx, generated.CreateGenerationTaskParams{
|
|
OperatorID: pgInt8(operatorID),
|
|
TenantID: input.TenantID,
|
|
ArticleID: pgInt8(input.ArticleID),
|
|
QuotaReservationID: pgInt8(input.QuotaReservationID),
|
|
TaskType: input.TaskType,
|
|
RequestHash: pgText(input.RequestHash),
|
|
InputParamsJson: input.InputParamsJSON,
|
|
})
|
|
}
|
|
|
|
func (r *auditRepository) CreateTaskRecord(ctx context.Context, input TaskRecordInput) (int64, error) {
|
|
var operatorID *int64
|
|
if input.OperatorID > 0 {
|
|
operatorID = &input.OperatorID
|
|
}
|
|
return r.q.CreateTaskRecord(ctx, generated.CreateTaskRecordParams{
|
|
TenantID: input.TenantID,
|
|
OperatorID: pgInt8(operatorID),
|
|
TaskType: input.TaskType,
|
|
ResourceType: input.ResourceType,
|
|
ResourceID: input.ResourceID,
|
|
Status: input.Status,
|
|
})
|
|
}
|
|
|
|
func (r *auditRepository) UpdateTaskRecordStatus(ctx context.Context, input TaskRecordInput) error {
|
|
return r.q.UpdateTaskRecordStatus(ctx, generated.UpdateTaskRecordStatusParams{
|
|
Status: input.Status,
|
|
ErrorMessage: pgText(input.ErrorMessage),
|
|
TenantID: input.TenantID,
|
|
TaskType: input.TaskType,
|
|
ResourceType: input.ResourceType,
|
|
ResourceID: input.ResourceID,
|
|
})
|
|
}
|
|
|
|
func (r *auditRepository) UpdateGenerationTaskStatus(ctx context.Context, input GenerationTaskStatusInput) error {
|
|
return r.q.UpdateGenerationTaskStatus(ctx, generated.UpdateGenerationTaskStatusParams{
|
|
Status: input.Status,
|
|
ErrorMessage: pgText(input.ErrorMessage),
|
|
StartedAt: pgTimestamp(input.StartedAt),
|
|
CompletedAt: pgTimestamp(input.CompletedAt),
|
|
ID: input.ID,
|
|
TenantID: input.TenantID,
|
|
})
|
|
}
|