feat: create scholarship list skeleton
Next up is linking the scholarship IDs that are contained in the link parameters to the form that's displayed upon clicking "Edit".
This commit is contained in:
1 parent
620cc514d3
commit
a0049d4797
8 files changed
+268
-38
No files matched your search
@@ -7,7 +7,8 @@ use aws_sdk_dynamodb::{
|
||||
use leptos::leptos_dom::log;
|
||||
use leptos::prelude::ServerFnError;
|
||||
use leptos::server;
|
||||
use crate::common::ExpandableInfo;
|
||||
use uuid::Uuid;
|
||||
use crate::common::{ExpandableInfo, ValueType};
|
||||
|
||||
#[cfg(feature = "ssr")]
|
||||
use crate::pages::utils::server_utils::create_dynamo_client;
|
||||
@@ -89,3 +90,77 @@ pub async fn get_all_scholarship_info() -> Result<Vec<ExpandableInfo>, ServerFnE
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Gets all scholarships that are associated with the given scholarship provider's ID.
|
||||
#[server(GetProviderScholarships, "/providers/scholarships/get")]
|
||||
pub async fn get_provider_scholarships(provider_id: String) -> Result<Vec<ExpandableInfo>, ServerFnError> {
|
||||
let client = create_dynamo_client().await;
|
||||
|
||||
log!("Getting provider scholarships for provider with ID {:?}", provider_id);
|
||||
|
||||
match client
|
||||
.scan()
|
||||
.table_name("leptos-scholarship-test")
|
||||
.expression_attribute_values(":id", serde_dynamo::to_attribute_value(ValueType::String(Some(provider_id)))?)
|
||||
.filter_expression("provider_id = :id")
|
||||
.send()
|
||||
.await
|
||||
{
|
||||
Ok(output) => {
|
||||
if let Some(items) = output.items {
|
||||
Ok(serde_dynamo::from_items(items)?)
|
||||
} else {
|
||||
Ok(Vec::new())
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
let msg = err.message().unwrap_or("An unknown error occurred");
|
||||
Err(ServerFnError::new(msg))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a new scholarship with a unique ID, and then returns that ID.
|
||||
#[server(RegisterScholarship, "/providers/scholarships/create")]
|
||||
pub async fn register_scholarship(provider_id: String, scholarship_name: String) -> Result<String, ServerFnError> {
|
||||
let client = create_dynamo_client().await;
|
||||
|
||||
log!("Creating scholarship for provider with ID {:?}", provider_id);
|
||||
|
||||
let mut current_uuid = "testing".to_string();
|
||||
let mut item = ExpandableInfo::new(current_uuid.clone());
|
||||
item.data.insert("provider_id".to_string(), ValueType::String(Some(provider_id)));
|
||||
item.data.insert("scholarship_name".to_string(), ValueType::String(Some(scholarship_name)));
|
||||
loop {
|
||||
let ser_item = serde_dynamo::to_item(&item)?;
|
||||
match client
|
||||
.put_item()
|
||||
.table_name("leptos-scholarship-test")
|
||||
.set_item(Some(ser_item))
|
||||
.condition_expression("attribute_not_exists(subject)")
|
||||
.send()
|
||||
.await
|
||||
{
|
||||
Ok(_) => {
|
||||
// Return the uuid that we used.
|
||||
return Ok(current_uuid);
|
||||
}
|
||||
Err(err) => {
|
||||
// Check the error to figure out if it was a key validation error.
|
||||
// If so, retry with new values.
|
||||
match err.code() {
|
||||
Some("ConditionalCheckFailedException") => {
|
||||
log!("Retrying conditional check.");
|
||||
log!("Current error: {:?}", err);
|
||||
current_uuid = Uuid::new_v4().to_string();
|
||||
item.subject = current_uuid.clone();
|
||||
continue
|
||||
}
|
||||
_ => {
|
||||
return Err(ServerFnError::new(err.message().unwrap_or("An unknown error occurred")));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user