Add route /family/list
This commit is contained in:
		@@ -63,3 +63,8 @@ pub async fn join(
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    Ok(HttpResponse::Accepted().finish())
 | 
					    Ok(HttpResponse::Accepted().finish())
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/// Get the list of families of the user
 | 
				
			||||||
 | 
					pub async fn list(token: LoginToken) -> HttpResult {
 | 
				
			||||||
 | 
					    Ok(HttpResponse::Ok().json(families_service::get_user_memberships(token.user_id).await?))
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -92,6 +92,7 @@ async fn main() -> std::io::Result<()> {
 | 
				
			|||||||
                web::post().to(families_controller::create),
 | 
					                web::post().to(families_controller::create),
 | 
				
			||||||
            )
 | 
					            )
 | 
				
			||||||
            .route("/family/join", web::post().to(families_controller::join))
 | 
					            .route("/family/join", web::post().to(families_controller::join))
 | 
				
			||||||
 | 
					            .route("/family/list", web::get().to(families_controller::list))
 | 
				
			||||||
    })
 | 
					    })
 | 
				
			||||||
    .bind(AppConfig::get().listen_address.as_str())?
 | 
					    .bind(AppConfig::get().listen_address.as_str())?
 | 
				
			||||||
    .run()
 | 
					    .run()
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -6,7 +6,7 @@ diesel::table! {
 | 
				
			|||||||
        time_create -> Int8,
 | 
					        time_create -> Int8,
 | 
				
			||||||
        is_admin -> Bool,
 | 
					        is_admin -> Bool,
 | 
				
			||||||
        invitation_code -> Varchar,
 | 
					        invitation_code -> Varchar,
 | 
				
			||||||
        count_members -> Int4,
 | 
					        count_members -> Int8,
 | 
				
			||||||
        count_admins -> Int4,
 | 
					        count_admins -> Int8,
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -94,3 +94,15 @@ pub struct NewMembership {
 | 
				
			|||||||
    pub time_create: i64,
 | 
					    pub time_create: i64,
 | 
				
			||||||
    pub is_admin: bool,
 | 
					    pub is_admin: bool,
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#[derive(Queryable, Debug, serde::Serialize)]
 | 
				
			||||||
 | 
					pub struct FamilyMembership {
 | 
				
			||||||
 | 
					    user_id: i32,
 | 
				
			||||||
 | 
					    family_id: i32,
 | 
				
			||||||
 | 
					    name: String,
 | 
				
			||||||
 | 
					    time_create: i64,
 | 
				
			||||||
 | 
					    is_admin: bool,
 | 
				
			||||||
 | 
					    invitation_code: String,
 | 
				
			||||||
 | 
					    count_members: i64,
 | 
				
			||||||
 | 
					    count_admins: i64,
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,6 +1,9 @@
 | 
				
			|||||||
use crate::connections::db_connection;
 | 
					use crate::connections::db_connection;
 | 
				
			||||||
use crate::constants::FAMILY_INVITATION_CODE_LEN;
 | 
					use crate::constants::FAMILY_INVITATION_CODE_LEN;
 | 
				
			||||||
use crate::models::{Family, FamilyID, Membership, NewFamily, NewMembership, UserID};
 | 
					use crate::manual_schema::families_memberships;
 | 
				
			||||||
 | 
					use crate::models::{
 | 
				
			||||||
 | 
					    Family, FamilyID, FamilyMembership, Membership, NewFamily, NewMembership, UserID,
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
use crate::schema::{families, memberships};
 | 
					use crate::schema::{families, memberships};
 | 
				
			||||||
use crate::utils::string_utils::rand_str;
 | 
					use crate::utils::string_utils::rand_str;
 | 
				
			||||||
use crate::utils::time_utils::time;
 | 
					use crate::utils::time_utils::time;
 | 
				
			||||||
@@ -66,6 +69,15 @@ pub async fn is_member(family_id: FamilyID, user_id: UserID) -> anyhow::Result<b
 | 
				
			|||||||
    .map(|c: i64| c > 0)
 | 
					    .map(|c: i64| c > 0)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/// Get the memberships of a user
 | 
				
			||||||
 | 
					pub async fn get_user_memberships(user_id: UserID) -> anyhow::Result<Vec<FamilyMembership>> {
 | 
				
			||||||
 | 
					    db_connection::execute(|conn| {
 | 
				
			||||||
 | 
					        families_memberships::table
 | 
				
			||||||
 | 
					            .filter(families_memberships::dsl::user_id.eq(user_id.0))
 | 
				
			||||||
 | 
					            .get_results(conn)
 | 
				
			||||||
 | 
					    })
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/// Remove a membership to a family
 | 
					/// Remove a membership to a family
 | 
				
			||||||
pub async fn remove_membership(_family_id: FamilyID, _user_id: UserID) {
 | 
					pub async fn remove_membership(_family_id: FamilyID, _user_id: UserID) {
 | 
				
			||||||
    todo!()
 | 
					    todo!()
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user