Add list feature
This commit is contained in:
10
Cargo.toml
10
Cargo.toml
@@ -13,3 +13,13 @@ ratatui = "0.26.1"
|
|||||||
sqlx = { version = "0.7.4", features = ["sqlite", "runtime-tokio"] }
|
sqlx = { version = "0.7.4", features = ["sqlite", "runtime-tokio"] }
|
||||||
tokio = { version = "1.36.0", features = ["rt", "macros", "rt-multi-thread"] }
|
tokio = { version = "1.36.0", features = ["rt", "macros", "rt-multi-thread"] }
|
||||||
tui-textarea = "0.4.0"
|
tui-textarea = "0.4.0"
|
||||||
|
|
||||||
|
[profile.optimize]
|
||||||
|
inherits = "release"
|
||||||
|
opt-level = "z"
|
||||||
|
debug = false
|
||||||
|
strip = true
|
||||||
|
lto = true
|
||||||
|
codegen-units = 1
|
||||||
|
incremental = false
|
||||||
|
panic = "abort"
|
||||||
|
@@ -193,4 +193,32 @@ impl Database {
|
|||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn get_user_most_ratings(&self) -> Result<()> {
|
||||||
|
let mut conn = self.pool.acquire().await?;
|
||||||
|
|
||||||
|
let records = sqlx::query(
|
||||||
|
r#"
|
||||||
|
select users.name, count(rating) as c, avg(rating)
|
||||||
|
from ratings
|
||||||
|
join users on users.id = user_id
|
||||||
|
group by user_id
|
||||||
|
order by c desc;
|
||||||
|
"#,
|
||||||
|
)
|
||||||
|
.fetch_all(&mut *conn)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
//let id = record.try_get("id")?;
|
||||||
|
|
||||||
|
for r in records {
|
||||||
|
let name: &str = r.try_get("name")?;
|
||||||
|
let count: i64 = r.try_get("c")?;
|
||||||
|
let avg: f64 = r.try_get("avg(rating)")?;
|
||||||
|
|
||||||
|
println!("Name: {name}, Ratings: {count}, Average: {avg}");
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
10
src/main.rs
10
src/main.rs
@@ -33,6 +33,14 @@ async fn main() {
|
|||||||
Err(e) => eprintln!("{e}"),
|
Err(e) => eprintln!("{e}"),
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
// Check if the parameter is -l
|
||||||
|
if username == "-l" {
|
||||||
|
db.get_user_most_ratings()
|
||||||
|
.await
|
||||||
|
.expect("Could not get ratings");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
let user_id = db.add_user(&username).await.expect("Could not add user");
|
let user_id = db.add_user(&username).await.expect("Could not add user");
|
||||||
println!("UserID for {username}: {user_id}");
|
println!("UserID for {username}: {user_id}");
|
||||||
}
|
}
|
||||||
@@ -45,7 +53,7 @@ async fn main() {
|
|||||||
.expect("Could not read from database");
|
.expect("Could not read from database");
|
||||||
|
|
||||||
if is_empty {
|
if is_empty {
|
||||||
println!("Add an user first. This can be achieved by using rate_music [name] [user_id]");
|
println!("Add a new user first. This can be achieved by using rate_music [name] [user_id]");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user