Browse Source

Created a DTO, so the data is decoupled from the command

main
Julio Biason 3 years ago
parent
commit
f0661d2532
  1. 7
      lib/src/domain/command.rs
  2. 25
      lib/src/domain/project/create.rs
  3. 35
      lib/src/domain/project/dto.rs
  4. 3
      lib/src/domain/project/mod.rs

7
lib/src/domain/command.rs

@ -16,9 +16,12 @@
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
/// Trait/tag for DTOs
pub trait Dto {}
/// Trait that all commands must implement. /// Trait that all commands must implement.
pub trait Command { pub trait Command<T: Dto> {
fn execute(&self) -> Result<(), CommandError>; fn execute(self) -> Result<T, CommandError>;
} }
/// Errors produced by invalid commands /// Errors produced by invalid commands

25
lib/src/domain/project/create_project.rs → lib/src/domain/project/create.rs

@ -19,23 +19,19 @@
use crate::domain::command::Command; use crate::domain::command::Command;
use crate::domain::command::CommandError; use crate::domain::command::CommandError;
struct CreateProject { use super::dto::Project;
code: String,
name: String, struct Create(Project);
}
impl CreateProject { impl Create {
fn new(code: &str, name: &str) -> Self { fn new(project: Project) -> Self {
Self { Self(project)
code: code.into(),
name: name.into(),
}
} }
} }
impl Command for CreateProject { impl Command<Project> for Create {
fn execute(&self) -> Result<(), CommandError> { fn execute(self) -> Result<Project, CommandError> {
Ok(()) Ok(self.0)
} }
} }
@ -45,7 +41,8 @@ mod tests {
#[test] #[test]
fn should_create_project() { fn should_create_project() {
let command = CreateProject::new("project", "some project"); let project = Project::new("project", "some project");
let command = Create::new(project);
assert!(command.execute().is_ok()); assert!(command.execute().is_ok());
} }
} }

35
lib/src/domain/project/dto.rs

@ -0,0 +1,35 @@
/*
TIN - Time tracking application
Copyright (C) 2021 Julio Biason
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use crate::domain::command::Dto;
pub struct Project {
code: String,
name: String,
}
impl Project {
pub fn new(code: &str, name: &str) -> Self {
Self {
code: code.into(),
name: name.into(),
}
}
}
impl Dto for Project {}

3
lib/src/domain/project/mod.rs

@ -1 +1,2 @@
pub mod create_project; pub mod create;
pub mod dto;

Loading…
Cancel
Save