-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
110 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
DROP TABLE logentries; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
-- Create the logs table | ||
CREATE TABLE logs ( | ||
log_id SERIAL PRIMARY KEY, | ||
resource_type TEXT NOT NULL, | ||
resource_id INTEGER NOT NULL, | ||
user_id INTEGER NOT NULL, | ||
action TEXT NOT NULL, | ||
data TEXT NOT NULL, | ||
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP | ||
); | ||
|
||
-- Insert a test log entry | ||
INSERT INTO logs (resource_type, resource_id, user_id, action, data) VALUES ('test', 1, 1, 'test', 'test'); | ||
|
||
-- Create indexes for common queries | ||
CREATE INDEX logs_resource_type_idx ON logs(resource_type); | ||
CREATE INDEX logs_resource_id_idx ON logs(resource_id); | ||
CREATE INDEX logs_user_id_idx ON logs(user_id); | ||
CREATE INDEX logs_action_idx ON logs(action); | ||
CREATE INDEX logs_created_at_idx ON logs(created_at); | ||
|
||
-- Add foreign key constraint to users table | ||
ALTER TABLE logs | ||
ADD CONSTRAINT logs_user_id_fkey | ||
FOREIGN KEY (user_id) REFERENCES users(user_id) ON DELETE CASCADE; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,2 @@ | ||
use serde::{Deserialize, Serialize}; | ||
use sqlx::prelude::*; | ||
|
||
use crate::database::Database; | ||
|
||
pub mod kind; | ||
pub mod definition; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
use sqlx::{prelude::FromRow, query_as}; | ||
use serde::{Deserialize, Serialize}; | ||
use chrono::{DateTime, Utc}; | ||
use poem_openapi::Object; | ||
|
||
use crate::database::Database; | ||
|
||
/// Represents a log entry | ||
/// When an action is performed on a resource, a log entry is created | ||
/// This resource can then be queried by user, resource_type, (resource_type + resource_id), or action | ||
#[derive(FromRow, Object, Debug, Clone, Serialize, Deserialize)] | ||
pub struct LogEntry { | ||
pub log_id: i32, | ||
pub resource_type: String, | ||
pub resource_id: i32, | ||
pub user_id: i32, | ||
pub action: String, | ||
pub data: String, | ||
pub created_at: DateTime<Utc>, | ||
} | ||
|
||
impl LogEntry { | ||
/// Create a new log entry | ||
/// Let postgres generate the id and created_at | ||
pub async fn new(db: &Database, resource_type: String, resource_id: i32, user_id: i32, action: String, data: String) -> Result<Self, sqlx::Error> { | ||
let log_entry = query_as!( | ||
LogEntry, | ||
"INSERT INTO logs (resource_type, resource_id, user_id, action, data) VALUES ($1, $2, $3, $4, $5) RETURNING *", | ||
resource_type, resource_id, user_id, action, data | ||
).fetch_one(&db.pool).await?; | ||
|
||
Ok(log_entry) | ||
} | ||
|
||
/// Find by log_id | ||
pub async fn find_by_log_id(db: &Database, log_id: i32) -> Result<Self, sqlx::Error> { | ||
let log_entry = query_as!( | ||
LogEntry, | ||
"SELECT * FROM logs WHERE log_id = $1", | ||
log_id | ||
).fetch_one(&db.pool).await?; | ||
|
||
Ok(log_entry) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,4 @@ pub mod products; | |
pub mod sessions; | ||
pub mod tags; | ||
pub mod users; | ||
pub mod log; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.