Changed ElementCreateRequest to optionally include a ContentUpdateStrategy. Closes #13

This commit is contained in:
Philip (a-0) 2024-02-13 18:31:35 +01:00
parent 6a5fce3070
commit 91142f7bfb
4 changed files with 14 additions and 7 deletions

View file

@ -1,7 +1,7 @@
use reqwest::Method; use reqwest::Method;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::types::{Element, ElementContent, ElementId, PotId}; use crate::types::{ContentUpdateStrategy, Element, ElementContent, ElementId, PotId};
use super::UbisyncRequest; use super::UbisyncRequest;
@ -9,6 +9,7 @@ use super::UbisyncRequest;
pub struct ElementCreateRequest { pub struct ElementCreateRequest {
pub content: ElementContent, pub content: ElementContent,
pub pot: Option<PotId>, pub pot: Option<PotId>,
pub update_strategy: Option<ContentUpdateStrategy>,
} }
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]

View file

@ -17,7 +17,6 @@ use ubisync_lib::{
types::{AppId, ElementId}, types::{AppId, ElementId},
}; };
pub(super) async fn get( pub(super) async fn get(
Path(id): Path<ElementId>, Path(id): Path<ElementId>,
app: Extension<AppId>, app: Extension<AppId>,
@ -51,7 +50,7 @@ pub(super) async fn create(
Ok(_) => { Ok(_) => {
warn!("Pot not found"); warn!("Pot not found");
return StatusCode::INTERNAL_SERVER_ERROR.into_response(); return StatusCode::INTERNAL_SERVER_ERROR.into_response();
}, }
Err(e) => { Err(e) => {
warn!("Element create request did not provide pot id, and no default pot for requesting app was found: {}", e); warn!("Element create request did not provide pot id, and no default pot for requesting app was found: {}", e);
return StatusCode::INTERNAL_SERVER_ERROR.into_response(); return StatusCode::INTERNAL_SERVER_ERROR.into_response();
@ -59,7 +58,11 @@ pub(super) async fn create(
}, },
}; };
let element_id = s.create_element(req.content, pot_id); let element_id = s.create_element(
req.content,
pot_id,
req.update_strategy.unwrap_or(Default::default()),
);
debug!("{:?}", element_id); debug!("{:?}", element_id);
match element_id { match element_id {
Ok(id) => ( Ok(id) => (

View file

@ -57,10 +57,10 @@ impl ApiState {
.map(|app_opt| app_opt.ok_or(Error::msg("Failed to find app")))? .map(|app_opt| app_opt.ok_or(Error::msg("Failed to find app")))?
} }
pub fn create_element(&self, content: ElementContent, pot: PotId) -> anyhow::Result<ElementId> { pub fn create_element(&self, content: ElementContent, pot: PotId, update_strategy: ContentUpdateStrategy) -> anyhow::Result<ElementId> {
let id = ElementId::new(); let id = ElementId::new();
self.db() self.db()
.add_element(id.clone(), content.clone(), ContentUpdateStrategy::Overwrite, None, false, pot.clone())?; .add_element(id.clone(), content.clone(), update_strategy, None, false, pot.clone())?;
debug!("Added element {{{}}}", id.to_string()); debug!("Added element {{{}}}", id.to_string());
self.state.send_to_peers( self.state.send_to_peers(
@ -192,7 +192,7 @@ impl ApiState {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use tracing::Level; use tracing::Level;
use ubisync_lib::types::ElementContent; use ubisync_lib::types::{ContentUpdateStrategy, ElementContent};
use crate::state::State; use crate::state::State;
@ -224,6 +224,7 @@ mod tests {
.create_element( .create_element(
ElementContent::Text("Test-text".to_string()), ElementContent::Text("Test-text".to_string()),
pot_id.clone(), pot_id.clone(),
ContentUpdateStrategy::Overwrite,
) )
.unwrap(); .unwrap();
let el = state.get_element(id, app_id).unwrap(); let el = state.get_element(id, app_id).unwrap();
@ -259,6 +260,7 @@ mod tests {
.create_element( .create_element(
ElementContent::Text("Test-text".to_string()), ElementContent::Text("Test-text".to_string()),
pot_id.clone(), pot_id.clone(),
ContentUpdateStrategy::Overwrite,
) )
.unwrap(); .unwrap();
state state

View file

@ -57,6 +57,7 @@ async fn two_nodes_element_creation() {
ElementCreateRequest { ElementCreateRequest {
content: test_element_content.clone(), content: test_element_content.clone(),
pot: None, pot: None,
update_strategy: None,
}, },
(), (),
) )