Can create a movement
This commit is contained in:
@ -1,4 +1,5 @@
|
||||
pub mod accounts_service;
|
||||
pub mod files_service;
|
||||
pub mod movements_service;
|
||||
pub mod tokens_service;
|
||||
pub mod users_service;
|
||||
|
89
moneymgr_backend/src/services/movements_service.rs
Normal file
89
moneymgr_backend/src/services/movements_service.rs
Normal file
@ -0,0 +1,89 @@
|
||||
use crate::connections::db_connection::db;
|
||||
use crate::controllers::server_controller::ServerConstraints;
|
||||
use crate::models::accounts::AccountID;
|
||||
use crate::models::files::FileID;
|
||||
use crate::models::movements::{Movement, MovementID, NewMovement};
|
||||
use crate::models::users::UserID;
|
||||
use crate::schema::movements;
|
||||
use crate::services::{accounts_service, files_service};
|
||||
use crate::utils::time_utils::time;
|
||||
use diesel::RunQueryDsl;
|
||||
use diesel::prelude::*;
|
||||
|
||||
#[derive(serde::Deserialize)]
|
||||
pub struct UpdateMovementQuery {
|
||||
pub account_id: AccountID,
|
||||
pub time: u64,
|
||||
pub label: String,
|
||||
pub file_id: Option<FileID>,
|
||||
pub amount: f32,
|
||||
pub checked: bool,
|
||||
}
|
||||
|
||||
impl UpdateMovementQuery {
|
||||
pub async fn check_error(&self, user_id: UserID) -> anyhow::Result<Option<&'static str>> {
|
||||
let constraints = ServerConstraints::default();
|
||||
|
||||
// Check movement label
|
||||
if !constraints.movement_label.check_str(&self.label) {
|
||||
return Ok(Some("Invalid movement label length!"));
|
||||
}
|
||||
|
||||
// Check the account
|
||||
let account = accounts_service::get_by_id(self.account_id).await?;
|
||||
if account.user_id() != user_id {
|
||||
return Ok(Some("The user does not own the account!"));
|
||||
}
|
||||
|
||||
// Check the file
|
||||
if let Some(file_id) = self.file_id {
|
||||
let file = files_service::get_file_with_id(file_id)?;
|
||||
|
||||
if file.user_id() != user_id {
|
||||
return Ok(Some("The user does not own the referenced file!"));
|
||||
}
|
||||
}
|
||||
|
||||
// TODO : check for conflict
|
||||
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
|
||||
/// Create a new movement
|
||||
pub async fn create(query: &UpdateMovementQuery) -> anyhow::Result<Movement> {
|
||||
let new_account = NewMovement {
|
||||
account_id: query.account_id.0,
|
||||
time: query.time as i64,
|
||||
label: &query.label,
|
||||
file_id: query.file_id.map(|f| f.0),
|
||||
amount: query.amount,
|
||||
checked: query.checked,
|
||||
time_create: time() as i64,
|
||||
time_update: time() as i64,
|
||||
};
|
||||
|
||||
let res: Movement = diesel::insert_into(movements::table)
|
||||
.values(&new_account)
|
||||
.get_result(&mut db()?)?;
|
||||
|
||||
update(res.id(), query).await?;
|
||||
|
||||
Ok(res)
|
||||
}
|
||||
|
||||
/// Update a movement
|
||||
pub async fn update(id: MovementID, q: &UpdateMovementQuery) -> anyhow::Result<()> {
|
||||
diesel::update(movements::dsl::movements.filter(movements::dsl::id.eq(id.0)))
|
||||
.set((
|
||||
movements::dsl::time_update.eq(time() as i64),
|
||||
movements::dsl::account_id.eq(q.account_id.0),
|
||||
movements::dsl::time.eq(q.time as i64),
|
||||
movements::dsl::label.eq(&q.label),
|
||||
movements::dsl::file_id.eq(&q.file_id.map(|f| f.0)),
|
||||
movements::dsl::amount.eq(q.amount),
|
||||
))
|
||||
.execute(&mut db()?)?;
|
||||
|
||||
Ok(())
|
||||
}
|
Reference in New Issue
Block a user