Skip to content

Commit

Permalink
Clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
jabbate19 committed Oct 14, 2024
1 parent a788302 commit 4cca931
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 31 deletions.
20 changes: 13 additions & 7 deletions src/api/v1/event/car/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ async fn create_car(
}));
}

let record = Car::insert_new(event_id, user_id, &car, &mut *tx).await
.unwrap();
let record = Car::insert_new(event_id, user_id, &car, &mut *tx)
.await
.unwrap();

let _ = query!(
r#"
Expand All @@ -78,7 +79,7 @@ async fn create_car(
event_id,
car_id: record.id,
old_riders: Vec::new(),
new_riders: car.riders.clone()
new_riders: car.riders.clone(),
}))
.unwrap();
let mut redis = data.redis.lock().unwrap().clone();
Expand All @@ -105,7 +106,7 @@ async fn get_car(data: web::Data<AppState>, path: web::Path<(i32, i32)>) -> impl
match result {
Ok(Some(car)) => HttpResponse::Ok().json(car),
Ok(None) => HttpResponse::NotFound().body("Car not found"),
Err(_) => HttpResponse::InternalServerError().body("Failed to get Car")
Err(_) => HttpResponse::InternalServerError().body("Failed to get Car"),
}
}

Expand Down Expand Up @@ -149,7 +150,12 @@ async fn update_car(
let (event_id, car_id) = path.into_inner();
let user_id = session.get::<UserInfo>("userinfo").unwrap().unwrap().id;
let mut tx = data.db.begin().await.unwrap();
let other_cars = Car::select_all(event_id, &mut *tx).await.unwrap().into_iter().filter(|car| car.id != car_id).collect();
let other_cars = Car::select_all(event_id, &mut *tx)
.await
.unwrap()
.into_iter()
.filter(|car| car.id != car_id)
.collect();
if let Err(errs) = car.validate(&user_id, other_cars) {
tx.rollback().await.unwrap();
return HttpResponse::BadRequest().json(json!({
Expand Down Expand Up @@ -200,7 +206,7 @@ async fn update_car(
event_id,
car_id,
old_riders: current_riders,
new_riders: car.riders.clone()
new_riders: car.riders.clone(),
}))
.unwrap();
let mut redis = data.redis.lock().unwrap().clone();
Expand Down Expand Up @@ -236,7 +242,7 @@ async fn delete_car(
Err(err) => {
error!("{}", err);
HttpResponse::InternalServerError().body("Failed to delete car")
},
}
}
}

Expand Down
9 changes: 4 additions & 5 deletions src/api/v1/event/car/rider/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use log::error;
use redis_work_queue::{Item, WorkQueue};
use serde_json::json;
use sqlx::query;
use utoipa::openapi::security::Http;
use utoipa::OpenApi;

#[derive(OpenApi)]
Expand Down Expand Up @@ -42,14 +41,14 @@ async fn create_rider(
if car.max_capacity <= car.riders.unwrap().len() as i32 {
return HttpResponse::BadRequest().json(json!({
"error": "Car is full."
}))
}));
}
},
}
Ok(None) => {
return HttpResponse::BadRequest().json(json!({
"error": "Car does not exist."
}))
},
}
Err(err) => {
error!("{}", err);
return HttpResponse::InternalServerError().body("Unable to Join Car");
Expand Down Expand Up @@ -157,7 +156,7 @@ async fn delete_rider(
Err(err) => {
error!("{}", err);
HttpResponse::InternalServerError().body("Failed to delete rider")
},
}
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/api/v1/event/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ async fn create_event(
Err(err) => {
error!("{}", err);
HttpResponse::InternalServerError().body("Failed to create event")
},
}
}
}

Expand Down Expand Up @@ -151,7 +151,7 @@ async fn update_event(
Err(err) => {
error!("{}", err);
HttpResponse::InternalServerError().body("Failed to update event")
},
}
}
}

Expand Down Expand Up @@ -181,7 +181,7 @@ async fn delete_event(
Err(err) => {
error!("{}", err);
HttpResponse::InternalServerError().body("Failed to delete event")
},
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/db/car.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl CarData {
.map(|user| user.id)
.collect();
for rider in self.riders.iter() {
if other_car_members.contains(&rider) {
if other_car_members.contains(rider) {
errs.push(format!(
"{} is already in another car or is a driver.",
rider
Expand Down
2 changes: 1 addition & 1 deletion src/db/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
pub mod event;
pub mod car;
pub mod event;
3 changes: 3 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ enum Commands {

#[tokio::main]
async fn main() -> std::io::Result<()> {
env_logger::init();
dotenv::dotenv().ok();

let cli = Cli::parse();

match &cli.command {
Expand Down
7 changes: 1 addition & 6 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ use anyhow::anyhow;
use base64::prelude::*;
use include_dir::{include_dir, Dir};
use log::info;
use oauth2::basic::BasicClient;
use redis::aio::MultiplexedConnection;
use redis_work_queue::KeyPrefix;
use sqlx::{postgres::PgPoolOptions, PgPool};
use sqlx::postgres::PgPoolOptions;
use std::env;
use std::sync::{Arc, Mutex};

Expand Down Expand Up @@ -43,9 +41,6 @@ async fn serve_index() -> impl Responder {
}

pub async fn main() -> std::io::Result<()> {
env_logger::init();
dotenv::dotenv().ok();

let host = env::var("HOST").unwrap_or("127.0.0.1".to_string());
let host_inner = host.clone();
let port: i32 = match &env::var("PORT").map(|port| port.parse()) {
Expand Down
26 changes: 18 additions & 8 deletions src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use std::{
env,
};

use redis::{aio::MultiplexedConnection, AsyncCommands, RedisResult};
use log::error;
use redis::{aio::MultiplexedConnection, RedisResult};
use redis_work_queue::{Item, KeyPrefix, WorkQueue};
use sqlx::{postgres::PgPoolOptions, query, query_as, Pool, Postgres};
use std::time::Duration;
Expand Down Expand Up @@ -110,7 +111,7 @@ async fn work(job: &Item, db_pool: &Pool<Postgres>, pings: &PingClient) -> Resul
}
pings
.send_join(
&driver.email.trim_end_matches("@csh.rit.edu"),
driver.email.trim_end_matches("@csh.rit.edu"),
&rider.name,
&event_name,
)
Expand All @@ -124,7 +125,7 @@ async fn work(job: &Item, db_pool: &Pool<Postgres>, pings: &PingClient) -> Resul
}
pings
.send_leave(
&driver.email.trim_end_matches("@csh.rit.edu"),
driver.email.trim_end_matches("@csh.rit.edu"),
&rider.name,
&event_name,
)
Expand Down Expand Up @@ -152,11 +153,15 @@ async fn work(job: &Item, db_pool: &Pool<Postgres>, pings: &PingClient) -> Resul
}
pings
.send_remove(
&user.email.trim_end_matches("@csh.rit.edu"),
user.email.trim_end_matches("@csh.rit.edu"),
&driver.name,
&event_name,
)
.await;
.await
.map_err(|err| RedisError {
msg: format!("Failed to send message: {}", err),
should_retry: true,
})?;
}
for added in new_set.difference(&old_set) {
let user = user_map.get(added).unwrap();
Expand All @@ -165,11 +170,15 @@ async fn work(job: &Item, db_pool: &Pool<Postgres>, pings: &PingClient) -> Resul
}
pings
.send_add(
&user.email.trim_end_matches("@csh.rit.edu"),
user.email.trim_end_matches("@csh.rit.edu"),
&driver.name,
&event_name,
)
.await;
.await
.map_err(|err| RedisError {
msg: format!("Failed to send message: {}", err),
should_retry: true,
})?;
}
}
}
Expand All @@ -195,10 +204,11 @@ pub async fn work_loop(
}
// Drop a job that should be retried - it will be returned to the work queue after
// the (5 second) lease expires.
Err(err) if err.should_retry => (),
Err(err) if err.should_retry => error!("{}", err.msg),
// Errors that shouldn't cause a retry should mark the job as complete so it isn't
// tried again.
Err(err) => {
error!("{}", err.msg);
work_queue.complete(&mut db, &job).await?;
}
}
Expand Down

0 comments on commit 4cca931

Please sign in to comment.