diff --git a/src/model/user/fee.rs b/src/model/user/fee.rs new file mode 100644 index 0000000..d4d8f79 --- /dev/null +++ b/src/model/user/fee.rs @@ -0,0 +1,55 @@ +#[derive(Debug, Serialize)] +pub struct Fee { + pub sum_in_cents: i32, + pub parts: Vec<(String, i32)>, + pub name: String, + pub user_ids: String, + pub paid: bool, + pub users: Vec, +} + +impl Default for Fee { + fn default() -> Self { + Self::new() + } +} + +impl Fee { + pub fn new() -> Self { + Self { + sum_in_cents: 0, + name: "".into(), + parts: Vec::new(), + user_ids: "".into(), + users: Vec::new(), + paid: false, + } + } + + pub fn add(&mut self, desc: String, price_in_cents: i32) { + self.sum_in_cents += price_in_cents; + + self.parts.push((desc, price_in_cents)); + } + + pub fn add_person(&mut self, user: &User) { + if !self.name.is_empty() { + self.name.push_str(" + "); + self.user_ids.push('&'); + } + self.name.push_str(&user.name); + + self.user_ids.push_str(&format!("user_ids[]={}", user.id)); + self.users.push(user.clone()); + } + + pub fn paid(&mut self) { + self.paid = true; + } + + pub fn merge(&mut self, fee: Fee) { + for (desc, price_in_cents) in fee.parts { + self.add(desc, price_in_cents); + } + } +} diff --git a/src/model/user.rs b/src/model/user/mod.rs similarity index 97% rename from src/model/user.rs rename to src/model/user/mod.rs index 3c544d7..2bb97e4 100644 --- a/src/model/user.rs +++ b/src/model/user/mod.rs @@ -24,6 +24,8 @@ use crate::{ SCHECKBUCH, STUDENT_OR_PUPIL, UNTERSTUETZEND, }; +mod fee; + #[derive(FromRow, Serialize, Deserialize, Clone, Debug, Eq, Hash, PartialEq)] pub struct User { pub id: i64, @@ -83,62 +85,6 @@ pub enum LoginError { DeserializationError, } -#[derive(Debug, Serialize)] -pub struct Fee { - pub sum_in_cents: i32, - pub parts: Vec<(String, i32)>, - pub name: String, - pub user_ids: String, - pub paid: bool, - pub users: Vec, -} - -impl Default for Fee { - fn default() -> Self { - Self::new() - } -} - -impl Fee { - pub fn new() -> Self { - Self { - sum_in_cents: 0, - name: "".into(), - parts: Vec::new(), - user_ids: "".into(), - users: Vec::new(), - paid: false, - } - } - - pub fn add(&mut self, desc: String, price_in_cents: i32) { - self.sum_in_cents += price_in_cents; - - self.parts.push((desc, price_in_cents)); - } - - pub fn add_person(&mut self, user: &User) { - if !self.name.is_empty() { - self.name.push_str(" + "); - self.user_ids.push('&'); - } - self.name.push_str(&user.name); - - self.user_ids.push_str(&format!("user_ids[]={}", user.id)); - self.users.push(user.clone()); - } - - pub fn paid(&mut self) { - self.paid = true; - } - - pub fn merge(&mut self, fee: Fee) { - for (desc, price_in_cents) in fee.parts { - self.add(desc, price_in_cents); - } - } -} - impl User { pub async fn allowed_to_steer(&self, db: &SqlitePool) -> bool { self.has_role(db, "cox").await || self.has_role(db, "Bootsführer").await