-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathmod.rs
70 lines (64 loc) · 2.09 KB
/
mod.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
pub mod create;
mod delete;
mod update;
use super::ObjectRef;
use crate::error::Result;
use diesel::PgConnection;
use editoast_derive::EditoastError;
use serde::{Deserialize, Serialize};
use thiserror::Error;
pub use self::delete::DeleteOperation;
pub use create::RailjsonObject;
pub use update::UpdateOperation;
#[derive(Clone, Deserialize, Serialize)]
#[serde(tag = "operation_type", deny_unknown_fields)]
pub enum Operation {
#[serde(rename = "CREATE")]
Create(Box<RailjsonObject>),
#[serde(rename = "UPDATE")]
Update(UpdateOperation),
#[serde(rename = "DELETE")]
Delete(DeleteOperation),
}
#[derive(Clone, Serialize)]
#[serde(tag = "operation_type")]
pub enum OperationResult {
#[serde(rename = "CREATE")]
Create(RailjsonObject),
#[serde(rename = "UPDATE")]
Update(RailjsonObject),
#[serde(rename = "DELETE")]
Delete(ObjectRef),
}
impl Operation {
pub fn apply(&self, infra_id: i64, conn: &mut PgConnection) -> Result<OperationResult> {
match self {
Operation::Delete(deletion) => {
deletion.apply(infra_id, conn)?;
Ok(OperationResult::Delete(deletion.clone().into()))
}
Operation::Create(railjson_object) => {
create::apply_create_operation(railjson_object, infra_id, conn)?;
Ok(OperationResult::Create(*railjson_object.clone()))
}
Operation::Update(update) => {
let obj_railjson = update.apply(infra_id, conn)?;
Ok(OperationResult::Update(obj_railjson))
}
}
}
}
#[derive(Debug, Error, EditoastError)]
#[editoast_error(base_id = "operation")]
enum OperationError {
// To modify
#[error("Object '{obj_id}', could not be found in the infrastructure '{infra_id}'")]
#[editoast_error(status = 404)]
ObjectNotFound { obj_id: String, infra_id: i64 },
#[error("Empty string id is forbidden")]
EmptyId,
#[error("Update operation try to modify object id, which is forbidden")]
ModifyId,
#[error("A Json Patch error occurred: '{}'", .0)]
InvalidPatch(String),
}