diff options
Diffstat (limited to 'db/db.go')
| -rw-r--r-- | db/db.go | 24 |
1 files changed, 24 insertions, 0 deletions
@@ -325,3 +325,27 @@ func (d *DB) HealthCheck(ctx context.Context) error { _, err := d.conn.ExecContext(ctx, "SELECT 1") return err } + +// ApiLogEntry represents an API call log. +type ApiLogEntry struct { + Username string + ArticleName string + StatusCode int + ResponseMs int + Error string + RequestURL string +} + +// LogApiCall stores an API call log in the database. +func (d *DB) LogApiCall(ctx context.Context, entry *ApiLogEntry) error { + _, err := d.conn.ExecContext(ctx, + `INSERT INTO api_logs (username, article_name, status_code, response_time_ms, error, request_url) + VALUES (?, ?, ?, ?, ?, ?)`, + entry.Username, entry.ArticleName, entry.StatusCode, + entry.ResponseMs, entry.Error, entry.RequestURL, + ) + if err != nil { + return fmt.Errorf("log api call: %w", err) + } + return nil +} |
