Also accept flexible card-order form values, move mock seed under db/mock, and regenerate Swagger. Co-authored-by: Cursor <cursoragent@cursor.com>
160 lines
4.5 KiB
Go
160 lines
4.5 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
|
|
logger *logging.Logger
|
|
}
|
|
|
|
func NewRepository(client postgresql.Client, logger *logging.Logger) activitylog.Repository {
|
|
return &repository{
|
|
client: client,
|
|
logger: logger,
|
|
}
|
|
}
|
|
|
|
var activityLogColumns = []string{
|
|
"id", "user_id", "method", "path", "module", "action", "status_code", "request_body", "ip", "created_at",
|
|
}
|
|
|
|
var activityLogColumnsSQL = strings.Join(activityLogColumns, ", ")
|
|
|
|
const activityLogFromClause = ` FROM activity_logs al
|
|
LEFT JOIN users u ON u.id = al.user_id
|
|
LEFT JOIN LATERAL (
|
|
SELECT ru.role_id
|
|
FROM role_user ru
|
|
WHERE ru.user_id = u.id
|
|
ORDER BY ru.id
|
|
LIMIT 1
|
|
) ru ON true
|
|
LEFT JOIN roles r ON r.id = ru.role_id`
|
|
|
|
const activityLogSelectColumns = `al.id, al.user_id, al.method, al.path, al.module, al.action, al.status_code, al.request_body, al.ip, al.created_at, COALESCE(u.username, ''), COALESCE(r.code, '')`
|
|
|
|
func (r *repository) List(ctx context.Context, filter activitylog.ListFilter) (activitylog.ListActivityLogDTO, error) {
|
|
whereClause, args := buildListQuery(filter)
|
|
|
|
var count int64
|
|
if err := r.client.QueryRow(ctx, `SELECT COUNT(*) FROM activity_logs al`+whereClause, args...).Scan(&count); err != nil {
|
|
return activitylog.ListActivityLogDTO{}, err
|
|
}
|
|
|
|
offset := (filter.Page - 1) * filter.Limit
|
|
limitPlaceholder := len(args) + 1
|
|
offsetPlaceholder := len(args) + 2
|
|
query := fmt.Sprintf(
|
|
`SELECT %s%s%s ORDER BY al.id DESC LIMIT $%d OFFSET $%d`,
|
|
activityLogSelectColumns, activityLogFromClause, whereClause, limitPlaceholder, offsetPlaceholder,
|
|
)
|
|
|
|
rows, err := r.client.Query(ctx, query, append(args, filter.Limit, offset)...)
|
|
if err != nil {
|
|
return activitylog.ListActivityLogDTO{}, err
|
|
}
|
|
defer rows.Close()
|
|
|
|
var items []activitylog.ActivityLogDTO
|
|
for rows.Next() {
|
|
item, err := scanActivityLog(rows)
|
|
if err != nil {
|
|
return activitylog.ListActivityLogDTO{}, err
|
|
}
|
|
items = append(items, item)
|
|
}
|
|
if err := rows.Err(); 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("al.user_id = $%d", len(args)))
|
|
}
|
|
|
|
if filter.Module != "" {
|
|
args = append(args, filter.Module)
|
|
conditions = append(conditions, fmt.Sprintf("al.module = $%d", len(args)))
|
|
}
|
|
|
|
if filter.Action != "" {
|
|
args = append(args, filter.Action)
|
|
conditions = append(conditions, fmt.Sprintf("al.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) {
|
|
query := `SELECT ` + activityLogSelectColumns + activityLogFromClause + ` WHERE al.id = $1`
|
|
return scanActivityLog(r.client.QueryRow(ctx, query, id))
|
|
}
|
|
|
|
func (r *repository) Create(ctx context.Context, dto activitylog.CreateActivityLogDTO) (activitylog.ActivityLogDTO, error) {
|
|
query := `
|
|
WITH inserted AS (
|
|
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 + `
|
|
)
|
|
SELECT ` + activityLogSelectColumns + `
|
|
FROM inserted al
|
|
LEFT JOIN users u ON u.id = al.user_id
|
|
LEFT JOIN LATERAL (
|
|
SELECT ru.role_id
|
|
FROM role_user ru
|
|
WHERE ru.user_id = u.id
|
|
ORDER BY ru.id
|
|
LIMIT 1
|
|
) ru ON true
|
|
LEFT JOIN roles r ON r.id = ru.role_id`
|
|
|
|
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
|
|
var username, role string
|
|
|
|
err := row.Scan(
|
|
&item.Id, &item.UserId, &item.Method, &item.Path, &item.Module, &item.Action,
|
|
&item.StatusCode, &requestBody, &item.Ip, &item.CreatedAt,
|
|
&username, &role,
|
|
)
|
|
if err != nil {
|
|
return activitylog.ActivityLogDTO{}, err
|
|
}
|
|
item.RequestBody = requestBody
|
|
if item.UserId != nil {
|
|
item.User = &activitylog.UserInfo{
|
|
Id: *item.UserId,
|
|
Username: username,
|
|
Role: role,
|
|
}
|
|
}
|
|
return item, nil
|
|
}
|