98 lines
2.8 KiB
Go
98 lines
2.8 KiB
Go
package db
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"online-back/internal/identity/activitylog"
|
|
"online-back/pkg/client/postgresql"
|
|
"online-back/pkg/logging"
|
|
"online-back/pkg/repository/genericrepo"
|
|
)
|
|
|
|
type repository struct {
|
|
client postgresql.Client
|
|
generic *genericrepo.Repository[activitylog.ActivityLogDTO]
|
|
logger *logging.Logger
|
|
}
|
|
|
|
func NewRepository(client postgresql.Client, logger *logging.Logger) activitylog.Repository {
|
|
return &repository{
|
|
client: client,
|
|
generic: genericrepo.New(client, "activity_logs", activityLogColumnsSQL, scanActivityLog),
|
|
logger: logger,
|
|
}
|
|
}
|
|
|
|
var activityLogColumns = []string{
|
|
"id", "user_id", "method", "path", "module", "action", "status_code", "request_body", "ip", "created_at",
|
|
}
|
|
|
|
var activityLogColumnsSQL = strings.Join(activityLogColumns, ", ")
|
|
|
|
func (r *repository) List(ctx context.Context, filter activitylog.ListFilter) (activitylog.ListActivityLogDTO, error) {
|
|
whereClause, args := buildListQuery(filter)
|
|
|
|
items, count, err := r.generic.List(ctx, whereClause, args, filter.Page, filter.Limit, "id DESC")
|
|
if err != nil {
|
|
return activitylog.ListActivityLogDTO{}, err
|
|
}
|
|
|
|
return activitylog.ListActivityLogDTO{Count: count, Logs: items}, nil
|
|
}
|
|
|
|
func buildListQuery(filter activitylog.ListFilter) (whereClause string, args []interface{}) {
|
|
var conditions []string
|
|
|
|
if filter.UserId != nil {
|
|
args = append(args, *filter.UserId)
|
|
conditions = append(conditions, fmt.Sprintf("user_id = $%d", len(args)))
|
|
}
|
|
|
|
if filter.Module != "" {
|
|
args = append(args, filter.Module)
|
|
conditions = append(conditions, fmt.Sprintf("module = $%d", len(args)))
|
|
}
|
|
|
|
if filter.Action != "" {
|
|
args = append(args, filter.Action)
|
|
conditions = append(conditions, fmt.Sprintf("action = $%d", len(args)))
|
|
}
|
|
|
|
if len(conditions) == 0 {
|
|
return "", args
|
|
}
|
|
|
|
return " WHERE " + strings.Join(conditions, " AND "), args
|
|
}
|
|
|
|
func (r *repository) GetByID(ctx context.Context, id int64) (activitylog.ActivityLogDTO, error) {
|
|
return r.generic.GetByID(ctx, id)
|
|
}
|
|
|
|
func (r *repository) Create(ctx context.Context, dto activitylog.CreateActivityLogDTO) (activitylog.ActivityLogDTO, error) {
|
|
query := `INSERT INTO activity_logs (user_id, method, path, module, action, status_code, request_body, ip)
|
|
VALUES ($1,$2,$3,$4,$5,$6,$7,$8) RETURNING ` + activityLogColumnsSQL
|
|
|
|
row := r.client.QueryRow(ctx, query,
|
|
dto.UserId, dto.Method, dto.Path, dto.Module, dto.Action, dto.StatusCode, dto.RequestBody, dto.Ip,
|
|
)
|
|
return scanActivityLog(row)
|
|
}
|
|
|
|
func scanActivityLog(row genericrepo.Scanner) (activitylog.ActivityLogDTO, error) {
|
|
var item activitylog.ActivityLogDTO
|
|
var requestBody []byte
|
|
|
|
err := row.Scan(
|
|
&item.Id, &item.UserId, &item.Method, &item.Path, &item.Module, &item.Action,
|
|
&item.StatusCode, &requestBody, &item.Ip, &item.CreatedAt,
|
|
)
|
|
if err != nil {
|
|
return activitylog.ActivityLogDTO{}, err
|
|
}
|
|
item.RequestBody = requestBody
|
|
return item, nil
|
|
}
|