Skip to content

Commit

Permalink
adding more search api functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikolai Schimke authored and Nikolai Schimke committed Feb 1, 2024
1 parent 01a673b commit 0b5240c
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/bin/sportshub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ enum Commands {
},
}

#[tokio::main]
#[rocket::main]
async fn main() {
let mut conn = db::helpers::establish_connection().unwrap();

Expand All @@ -50,7 +50,7 @@ async fn main() {
}
Some(Commands::Server { port }) => {
run_migrations(&mut conn).unwrap();
web_server_utils::run(port).await;
web_server_utils::run(port).await.unwrap();
}
None => {
println!("use sportshub -h for help");
Expand Down
37 changes: 37 additions & 0 deletions src/db/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,40 @@ pub fn delete_all_past_streams(conn: &mut SqliteConnection) -> Result<usize, any
.execute(conn)?,
)
}

pub fn get_streams_by_sport(conn: &mut SqliteConnection, search_sport: String) -> Result<Vec<Stream>, anyhow::Error> {
Ok(stream
.filter(schema::stream::sport.eq(search_sport))
.load::<Stream>(conn)?)
}

pub fn get_streams_by_home_team(
conn: &mut SqliteConnection,
search_home_team: String,
) -> Result<Vec<Stream>, anyhow::Error> {
Ok(stream
.filter(schema::stream::home.eq(search_home_team))
.load::<Stream>(conn)?)
}

pub fn get_streams_by_away_team(
conn: &mut SqliteConnection,
search_away_team: String,
) -> Result<Vec<Stream>, anyhow::Error> {
Ok(stream
.filter(schema::stream::away.eq(search_away_team))
.load::<Stream>(conn)?)
}

pub fn get_streams_by_either_team(
conn: &mut SqliteConnection,
search_team: String,
) -> Result<Vec<Stream>, anyhow::Error> {
Ok(stream
.filter(
schema::stream::home
.eq(search_team.clone())
.or(schema::stream::away.eq(search_team)),
)
.load::<Stream>(conn)?)
}
2 changes: 1 addition & 1 deletion src/scrape_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ pub fn check_all_links(browser: &Browser, conn: &mut SqliteConnection, tabs_coun
// for each tab count we create a new tab and a new thread
for tab_num in 0..tabs_count {
// we create a new tab and push it to the tabs vector
let tab = browser.new_tab().unwrap();
let tab = browser.new_tab()?;
tabs.push(tab.clone());

// we get the streams from the chunked streams and turn it to a vec
Expand Down
52 changes: 48 additions & 4 deletions src/web_server_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,57 @@ async fn get_stream_by_id(id: i32) -> Json<Vec<Stream>> {
Json(streams)
}

pub async fn run(port: u16) {
#[get("/sport/<sport>")]
async fn get_streams_by_sport(sport: &str) -> Json<Vec<Stream>> {
let mut conn = db::helpers::establish_connection().unwrap();
let streams = db::helpers::get_streams_by_sport(&mut conn, sport.to_owned()).unwrap();

Json(streams)
}

#[get("/team/home/<team>")]
async fn get_streams_by_home_team(team: &str) -> Json<Vec<Stream>> {
let mut conn = db::helpers::establish_connection().unwrap();
let streams = db::helpers::get_streams_by_home_team(&mut conn, team.to_owned()).unwrap();

Json(streams)
}

#[get("/team/away/<team>")]
async fn get_streams_by_away_team(team: &str) -> Json<Vec<Stream>> {
let mut conn = db::helpers::establish_connection().unwrap();
let streams = db::helpers::get_streams_by_away_team(&mut conn, team.to_owned()).unwrap();

Json(streams)
}

#[get("/team/<team>")]
async fn get_streams_by_either_team(team: &str) -> Json<Vec<Stream>> {
let mut conn = db::helpers::establish_connection().unwrap();
let streams = db::helpers::get_streams_by_either_team(&mut conn, team.to_owned()).unwrap();

Json(streams)
}

pub async fn run(port: u16) -> anyhow::Result<()> {
Rocket::custom(rocket::Config {
port,
..Default::default()
})
.mount("/", routes![get_all_streams, get_active_streams, get_stream_by_id])
.mount(
"/",
routes![
get_all_streams,
get_active_streams,
get_stream_by_id,
get_streams_by_sport,
get_streams_by_home_team,
get_streams_by_away_team,
get_streams_by_either_team,
],
)
.launch()
.await
.unwrap();
.await?;

Ok(())
}

0 comments on commit 0b5240c

Please sign in to comment.