summaryrefslogtreecommitdiff
path: root/db/db.go
diff options
context:
space:
mode:
Diffstat (limited to 'db/db.go')
-rw-r--r--db/db.go24
1 files changed, 24 insertions, 0 deletions
diff --git a/db/db.go b/db/db.go
index 228a655..7409047 100644
--- a/db/db.go
+++ b/db/db.go
@@ -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
+}