Skip to content

Commit

Permalink
refactor: simplification of expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
ismaelxyz committed May 27, 2024
1 parent 020ac57 commit c8140fe
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 93 deletions.
45 changes: 21 additions & 24 deletions src/db/customer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,15 @@ pub async fn find_customer_by_id(
) -> mongodb::error::Result<Option<Customer>> {
let collection = db.collection::<CustomerDocument>("customer");

let customer_doc = collection.find_one(doc! {"_id":oid }, None).await?;
if customer_doc.is_none() {
let Some(customer_doc) = collection.find_one(doc! {"_id":oid }, None).await? else {
return Ok(None);
}
let unwrapped_doc = customer_doc.unwrap();
};

// transform ObjectId to String
let customer_json = Customer {
id: unwrapped_doc.id.to_string(),
name: unwrapped_doc.name.to_string(),
created_at: unwrapped_doc.created_at.to_string(),
id: customer_doc.id.to_string(),
name: customer_doc.name.to_string(),
created_at: customer_doc.created_at.to_string(),
};

Ok(Some(customer_json))
Expand Down Expand Up @@ -90,23 +89,22 @@ pub async fn update_customer_by_id(

let created_at: DateTime = DateTime::now();

let customer_doc = collection
let Some(customer_doc) = collection
.find_one_and_update(
doc! {"_id":oid },
doc! {"name": input.name.clone(), "createdAt": created_at},
find_one_and_update_options,
)
.await?;

if customer_doc.is_none() {
.await?
else {
return Ok(None);
}
let unwrapped_doc = customer_doc.unwrap();
};

// transform ObjectId to String
let customer_json = Customer {
id: unwrapped_doc.id.to_string(),
name: unwrapped_doc.name.to_string(),
created_at: unwrapped_doc.created_at.to_string(),
id: customer_doc.id.to_string(),
name: customer_doc.name.to_string(),
created_at: customer_doc.created_at.to_string(),
};

Ok(Some(customer_json))
Expand All @@ -119,19 +117,18 @@ pub async fn delete_customer_by_id(
let collection = db.collection::<CustomerDocument>("customer");

// if you just unwrap,, when there is no document it results in 500 error.
let customer_doc = collection
let Some(customer_doc) = collection
.find_one_and_delete(doc! {"_id":oid }, None)
.await?;
if customer_doc.is_none() {
.await?
else {
return Ok(None);
}
};

let unwrapped_doc = customer_doc.unwrap();
// transform ObjectId to String
let customer_json = Customer {
id: unwrapped_doc.id.to_string(),
name: unwrapped_doc.name.to_string(),
created_at: unwrapped_doc.created_at.to_string(),
id: customer_doc.id.to_string(),
name: customer_doc.name.to_string(),
created_at: customer_doc.created_at.to_string(),
};

Ok(Some(customer_json))
Expand Down
109 changes: 40 additions & 69 deletions src/routes/customer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,42 +41,30 @@ pub async fn get_customers(
let page: i64 = page.unwrap_or(1);
match customer::find_customer(db, limit, page).await {
Ok(customer_docs) => Ok(Json(customer_docs)),
Err(error) => {
println!("{:?}", error);

Err(MyError::build(400, Some(error.to_string())))
}
Err(error) => Err(MyError::build(400, Some(error.to_string()))),
}
}

/// get customer document by _id
#[openapi(tag = "Customer")]
#[get("/customer/<id>")]
pub async fn get_customer_by_id(db: &State<Database>, id: &str) -> Result<Json<Customer>, MyError> {
let oid = ObjectId::parse_str(id);

if oid.is_err() {
let Ok(oid) = ObjectId::parse_str(id) else {
return Err(MyError::build(400, Some("Invalid _id format.".to_string())));
}

match customer::find_customer_by_id(db, oid.unwrap()).await {
Ok(customer_doc) => {
if customer_doc.is_none() {
return Err(MyError::build(
400,
Some(format!("Customer not found with _id {}", &id)),
));
}
Ok(Json(customer_doc.unwrap()))
}
Err(error) => {
println!("{:?}", error);
};

Err(MyError::build(
match customer::find_customer_by_id(db, oid).await {
Ok(customer_doc) => match customer_doc {
None => Err(MyError::build(
400,
Some(format!("Customer not found with _id {}", &id)),
))
}
)),
Some(customer_doc) => Ok(Json(customer_doc)),
},
Err(_error) => Err(MyError::build(
400,
Some(format!("Customer not found with _id {}", &id)),
)),
}
}

Expand All @@ -90,12 +78,9 @@ pub async fn post_customer(
// can set with a single error like this.
match customer::insert_customer(db, input).await {
Ok(customer_doc_id) => Ok(Json(customer_doc_id)),
Err(error) => {
println!("{:?}", error);
Err(BadRequest(Json(MessageResponse {
message: "Invalid input".to_string(),
})))
}
Err(_error) => Err(BadRequest(Json(MessageResponse {
message: "Invalid input".to_string(),
}))),
}
}

Expand All @@ -108,29 +93,22 @@ pub async fn patch_customer_by_id(
id: &str,
input: Json<CustomerInput>,
) -> Result<Json<Customer>, MyError> {
let oid = ObjectId::parse_str(id);

if oid.is_err() {
let Ok(oid) = ObjectId::parse_str(id) else {
return Err(MyError::build(400, Some("Invalid id format.".to_string())));
}
};

match customer::update_customer_by_id(db, oid.unwrap(), input).await {
Ok(customer_doc) => {
if customer_doc.is_none() {
return Err(MyError::build(
400,
Some(format!("Customer not found with id {}", &id)),
));
}
Ok(Json(customer_doc.unwrap()))
}
Err(error) => {
println!("{:?}", error);
Err(MyError::build(
match customer::update_customer_by_id(db, oid, input).await {
Ok(customer_doc) => match customer_doc {
Some(customer_doc) => Ok(Json(customer_doc)),
None => Err(MyError::build(
400,
Some(format!("Customer not found with id {}", &id)),
))
}
)),
},
Err(_error) => Err(MyError::build(
400,
Some(format!("Customer not found with id {}", &id)),
)),
}
}

Expand All @@ -142,28 +120,21 @@ pub async fn delete_customer_by_id(
id: &str,
_key: ApiKey,
) -> Result<Json<Customer>, MyError> {
let oid = ObjectId::parse_str(id);

if oid.is_err() {
let Ok(oid) = ObjectId::parse_str(id) else {
return Err(MyError::build(400, Some("Invalid id format.".to_string())));
}
};

match customer::delete_customer_by_id(db, oid.unwrap()).await {
Ok(customer_doc) => {
if customer_doc.is_none() {
return Err(MyError::build(
400,
Some(format!("Customer not found with id {}", &id)),
));
}
Ok(Json(customer_doc.unwrap()))
}
Err(error) => {
println!("{:?}", error);
Err(MyError::build(
match customer::delete_customer_by_id(db, oid).await {
Ok(customer_doc) => match customer_doc {
Some(customer_doc) => Ok(Json(customer_doc)),
None => Err(MyError::build(
400,
Some(format!("Customer not found with _id {}", &id)),
))
}
)),
},
Err(_error) => Err(MyError::build(
400,
Some(format!("Customer not found with _id {}", &id)),
)),
}
}

0 comments on commit c8140fe

Please sign in to comment.