Skip to content

Commit efcddd8

Browse files
committed
return user hospital
1 parent 3e8a1a9 commit efcddd8

4 files changed

Lines changed: 62 additions & 0 deletions

File tree

src/docs.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use crate::handlers::{auth, hospitals, patients, appointments, specialists, admi
2222
patients::delete_account,
2323
patients::update_medical_info,
2424
hospitals::get_hospitals,
25+
hospitals::get_user_hospitals,
2526
hospitals::get_hospital_by_id,
2627
hospitals::create_hospital,
2728
hospitals::update_hospital,

src/handlers/hospitals.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,56 @@ pub async fn get_hospitals(
495495
))
496496
}
497497

498+
/// Get user's hospitals
499+
#[utoipa::path(
500+
get,
501+
path = "/api/hospitals/me",
502+
responses(
503+
(status = 200, body = ApiResponse<Vec<HospitalResponse>>),
504+
(status = 401),
505+
(status = 500)
506+
),
507+
tag = "hospitals",
508+
security(("bearer_auth" = []))
509+
)]
510+
pub async fn get_user_hospitals(
511+
State(state): State<AppState>,
512+
Extension(user): Extension<User>,
513+
) -> Result<ApiResponse<Vec<HospitalResponse>>, AppError> {
514+
let mut conn = state.pool.get()?;
515+
let my_hospitals = hospitals::service::get_user_hospitals(&mut conn, user.id)?;
516+
517+
let mut response = Vec::new();
518+
for hospital in my_hospitals {
519+
let inventory = hospitals::service::get_hospital_inventory(&mut conn, hospital.id)?;
520+
response.push(HospitalResponse {
521+
id: hospital.id,
522+
name: hospital.name,
523+
hospital_type: hospital.hospital_type,
524+
address: hospital.address,
525+
city: hospital.city,
526+
state: hospital.state,
527+
country: hospital.country,
528+
primary_phone: hospital.primary_phone,
529+
emergency_phone: hospital.emergency_phone,
530+
email: hospital.email,
531+
license_number: hospital.license_number,
532+
accreditation_doc_url: hospital.accreditation_doc_url,
533+
license_status: hospital.license_status,
534+
has_blood_bank: hospital.has_blood_bank,
535+
accepting_donors: hospital.accepting_donors,
536+
donating_operating_hours: hospital.donating_operating_hours,
537+
created_at: hospital.created_at,
538+
blood_inventory: inventory,
539+
});
540+
}
541+
542+
Ok(ApiResponse::success_with_message(
543+
"User hospitals retrieved successfully",
544+
response,
545+
))
546+
}
547+
498548
/// Get hospital by ID
499549
#[utoipa::path(
500550
get,

src/hospitals/service.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,16 @@ pub fn get_hospitals(conn: &mut PgConnection) -> Result<Vec<Hospital>, AppError>
291291
.map_err(AppError::from)
292292
}
293293

294+
/// Get user's hospitals
295+
pub fn get_user_hospitals(conn: &mut PgConnection, uid: Uuid) -> Result<Vec<Hospital>, AppError> {
296+
hospitals
297+
.filter(user_id.eq(uid))
298+
.filter(deleted_at.is_null())
299+
.select(Hospital::as_select())
300+
.load(conn)
301+
.map_err(AppError::from)
302+
}
303+
294304
/// Get hospital by ID
295305
pub fn get_hospital_by_id(
296306
conn: &mut PgConnection,

src/routes/hospitals.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ pub fn protected_router() -> Router<AppState> {
1414
Router::new()
1515
.route("/{hospital_id}", delete(hospitals::delete_hospital))
1616
.route("/create", post(hospitals::create_hospital))
17+
.route("/me", get(hospitals::get_user_hospitals))
1718
.route("/update/{hospital_id}", put(hospitals::update_hospital))
1819
.route("/blood-request", get(hospitals::get_blood_requests))
1920
.route("/blood-request", post(hospitals::create_blood_request))

0 commit comments

Comments
 (0)