diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/serve.rs | 53 |
1 files changed, 36 insertions, 17 deletions
diff --git a/src/serve.rs b/src/serve.rs index bc13b86..9d12a03 100644 --- a/src/serve.rs +++ b/src/serve.rs @@ -13,9 +13,14 @@ lazy_static! { } #[get("/callback/<name>/<url>")] -pub fn get_tracks(name: String, url: String) -> String { - let mut guard = CACHE.lock().unwrap(); - let mut oauth = guard.remove(&name).unwrap(); +pub fn get_tracks(name: String, url: String) -> Result<(), status::Custom<String>> { + let mut guard = CACHE.lock().or(Err(status::Custom( + Status::InternalServerError, + String::new(), + )))?; + let mut oauth = guard + .remove(&name) + .ok_or(status::Custom(Status::NotFound, String::new()))?; println!("auth: {:?} url: {}", oauth, url); let token_info = process_token(&mut oauth, &mut ("?code=".to_owned() + url.as_ref())); let client_credential = SpotifyClientCredentials::default() @@ -26,17 +31,21 @@ pub fn get_tracks(name: String, url: String) -> String { .client_credentials_manager(client_credential) .build(); let user_id = spotify.current_user().unwrap().id; - let mut client = crate::CLIENT.lock().unwrap(); + let mut client = crate::CLIENT.lock().or(Err(status::Custom( + Status::InternalServerError, + String::new(), + )))?; client .execute( "INSERT INTO suser (user_name) VALUES ($1) ON CONFLICT (user_name) DO NOTHING;", &[&user_id], ) .unwrap(); - let uid = get_uid(user_id.as_ref(), &mut client); + let uid = get_uid(user_id.as_ref(), &mut client) + .ok_or(status::NotFound(format!("username {} not found", user_id)))?; client - .execute("DELETE FROM user_track WHERE user_id = $1;", &[&uid]) - .unwrap(); + .execute("DELETE FROM user_track WHERE user_id = $1;", &[&uid]) + .unwrap(); drop(client); let chunk_size = 50; let mut playlist_index = 0; @@ -67,7 +76,9 @@ pub fn get_tracks(name: String, url: String) -> String { } for track in tracks.items { //println!("{:?}", track.track.name); - let mut client = crate::CLIENT.lock().unwrap(); + let mut client = crate::CLIENT.lock().or(Err( + status::Custom(Status::InternalServerError, String::new()), + ))?; if track.track.id.is_none() { println!("{:#?}", track); @@ -135,7 +146,7 @@ pub fn get_tracks(name: String, url: String) -> String { }, } } - name + Ok(()) } #[get("/token/<name>")] @@ -160,18 +171,26 @@ pub fn token(name: String) -> Result<Redirect, status::Custom<String>> { } } -fn get_uid(name: &str, client: &mut postgres::Client) -> i32 { - client - .query("SELECT user_id FROM suser where user_name = $1;", &[&name]) - .unwrap()[0] - .get(0) +fn get_uid(name: &str, client: &mut postgres::Client) -> Option<i32> { + match client.query("SELECT user_id FROM suser where user_name = $1;", &[&name]) { + _ => None, + Ok(rows) => match rows.len() { + 0 => None, + x => { + let x: i32 = rows[0].get(0); + Some(x) + } + }, + } } #[get("/match/<name1>/<name2>")] -pub fn match_users(name1: String, name2: String) -> Result<String, status::Custom<String>> { +pub fn match_users(name1: String, name2: String) -> Result<String, status::NotFound<String>> { let mut client = crate::CLIENT.lock().unwrap(); - let uid1 = get_uid(name1.as_ref(), &mut client); - let uid2 = get_uid(name2.as_ref(), &mut client); + let uid1 = get_uid(name1.as_ref(), &mut client) + .ok_or(status::NotFound(format!("username {} not found", name1)))?; + let uid2 = get_uid(name2.as_ref(), &mut client) + .ok_or(status::NotFound(format!("username {} not found", name2)))?; let mut songs = String::new(); for row in client .query( |