Add list feature

This commit is contained in:
2024-03-28 23:51:18 +01:00
parent 40235a3439
commit c5d100488f
3 changed files with 47 additions and 1 deletions

View File

@@ -193,4 +193,32 @@ impl Database {
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(())
}
}

View File

@@ -33,6 +33,14 @@ async fn main() {
Err(e) => eprintln!("{e}"),
}
} 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");
println!("UserID for {username}: {user_id}");
}
@@ -45,7 +53,7 @@ async fn main() {
.expect("Could not read from database");
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;
}