summaryrefslogtreecommitdiff
path: root/src/database.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/database.rs')
-rw-r--r--src/database.rs58
1 files changed, 23 insertions, 35 deletions
diff --git a/src/database.rs b/src/database.rs
index 326c1e0..1a3a415 100644
--- a/src/database.rs
+++ b/src/database.rs
@@ -3,10 +3,10 @@ use postgres::{Client, NoTls};
use std::sync::{Arc, Mutex};
lazy_static! {
- static ref CLIENT: Arc<Mutex<Client>> = Arc::new(Mutex::new(Client::connect(
- "host=track_db user=postgres password=example",
- NoTls
- )?));
+ static ref CLIENT: Arc<Mutex<Client>> = Arc::new(Mutex::new(
+ Client::connect("host=track_db user=postgres password=example", NoTls)
+ .expect("failed to connect to database")
+ ));
}
fn initialize_db() -> Result<(), Error> {
@@ -45,8 +45,8 @@ fn initialize_db() -> Result<(), Error> {
}
use rspotify::spotify::model::track::FullTrack;
-fn insert_track(user_id: i32, track: FullTrack) -> Result<(), Error> {
- let mut client = crate::CLIENT.lock()?;
+pub fn insert_track(user_id: i32, track: FullTrack, weight: i32) -> Result<(), Error> {
+ let mut client = CLIENT.lock()?;
if track.id.is_none() {
println!("{:#?}", track);
@@ -56,8 +56,7 @@ fn insert_track(user_id: i32, track: FullTrack) -> Result<(), Error> {
client.execute(
"INSERT INTO track (track_code, name, artist, popularity)
VALUES ($1, $2, $3, $4)
- ON CONFLICT DO NOTHING
- ",
+ ON CONFLICT DO NOTHING",
&[
&(track.id.clone()?),
&track.name,
@@ -71,37 +70,32 @@ fn insert_track(user_id: i32, track: FullTrack) -> Result<(), Error> {
)?[0]
.get(0);
println!("uid: {} tid: {}", user_id, track_id);
- client.batch_execute(
+ client.execute(
"
INSERT INTO user_track_raw (track_id, user_id, count)
VALUES ($1, $2, $3)
ON CONFLICT
ON CONSTRAINT track_user_pkey
DO NOTHING;
- UPDATE user_track SET count = count + 1 WHERE track_id = $1 AND user_id = $2;
",
&[&track_id, &user_id, &0],
)?;
+ client.execute(
+ "UPDATE user_track SET count = count + $3 WHERE track_id = $1 AND user_id = $2;",
+ &[&track_id, &user_id, &weight],
+ )?;
Ok(())
}
-fn insert_user(name: &str) -> Result<(i32, String), Error> {
- let mut client = crate::CLIENT.lock()?;
- client.batch_execute(
- "
- INSERT INTO suser (user_name) VALUES ($1) ON CONFLICT (user_name) DO NOTHING;
- DELETE FROM user_track WHERE user_name = $1;
- ",
- &[&user_id],
+pub fn insert_user(name: &str) -> Result<i32, Error> {
+ let mut client = CLIENT.lock()?;
+ client.execute(
+ "INSERT INTO suser (user_name) VALUES ($1) ON CONFLICT (user_name) DO NOTHING;",
+ &[&name],
)?;
- let user_id: i32 = client
- .query_one(
- "SELECT user_id FROM suser WHERE user_name = $1;",
- &[&user_id],
- )?
- .get(0);
- //reset user_track relation
- Ok((user_id, name))
+ let db_user_id = get_uid(name, &mut *client)?;
+ client.execute("DELETE FROM user_track WHERE user_id = $1;", &[&db_user_id])?;
+ Ok(db_user_id)
}
fn get_uid(name: &str, client: &mut postgres::Client) -> Result<i32, Error> {
@@ -111,13 +105,8 @@ fn get_uid(name: &str, client: &mut postgres::Client) -> Result<i32, Error> {
Ok(x)
}
-lazy_static! {
- static ref CACHE: Arc<Mutex<HashMap<String, SpotifyOAuth>>> =
- Arc::new(Mutex::new(HashMap::new()));
-}
-
pub fn match_users(name1: String, name2: String) -> Result<String, Error> {
- let mut client = crate::CLIENT.lock()?;
+ let mut client = CLIENT.lock()?;
let mut songs = String::new();
for row in client.query(
"
@@ -132,7 +121,7 @@ pub fn match_users(name1: String, name2: String) -> Result<String, Error> {
SELECT track_id, name, artist
FROM track
JOIN (
- SELECT track_id
+ SELECT track_id
FROM user_track
JOIN suser USING (user_id)
JOIN track USING (track_id)
@@ -152,9 +141,8 @@ pub fn match_users(name1: String, name2: String) -> Result<String, Error> {
Ok(songs)
}
-#[get("/user")]
pub fn get_users() -> Result<String, Error> {
- let mut client = crate::CLIENT.lock()?;
+ let mut client = CLIENT.lock()?;
let mut users = String::new();
for row in client.query("SELECT user_name FROM suser", &[])? {
let user: String = row.get(0);