From f0661d2532d27eb025eb1eee30f627cb35e078d0 Mon Sep 17 00:00:00 2001 From: Julio Biason Date: Tue, 31 Aug 2021 12:17:23 -0300 Subject: [PATCH] Created a DTO, so the data is decoupled from the command --- lib/src/domain/command.rs | 7 ++-- .../project/{create_project.rs => create.rs} | 25 ++++++------- lib/src/domain/project/dto.rs | 35 +++++++++++++++++++ lib/src/domain/project/mod.rs | 3 +- 4 files changed, 53 insertions(+), 17 deletions(-) rename lib/src/domain/project/{create_project.rs => create.rs} (71%) create mode 100644 lib/src/domain/project/dto.rs diff --git a/lib/src/domain/command.rs b/lib/src/domain/command.rs index 14e8104..6eb30b2 100644 --- a/lib/src/domain/command.rs +++ b/lib/src/domain/command.rs @@ -16,9 +16,12 @@ along with this program. If not, see . */ +/// Trait/tag for DTOs +pub trait Dto {} + /// Trait that all commands must implement. -pub trait Command { - fn execute(&self) -> Result<(), CommandError>; +pub trait Command { + fn execute(self) -> Result; } /// Errors produced by invalid commands diff --git a/lib/src/domain/project/create_project.rs b/lib/src/domain/project/create.rs similarity index 71% rename from lib/src/domain/project/create_project.rs rename to lib/src/domain/project/create.rs index de18406..9fc430c 100644 --- a/lib/src/domain/project/create_project.rs +++ b/lib/src/domain/project/create.rs @@ -19,23 +19,19 @@ use crate::domain::command::Command; use crate::domain::command::CommandError; -struct CreateProject { - code: String, - name: String, -} +use super::dto::Project; + +struct Create(Project); -impl CreateProject { - fn new(code: &str, name: &str) -> Self { - Self { - code: code.into(), - name: name.into(), - } +impl Create { + fn new(project: Project) -> Self { + Self(project) } } -impl Command for CreateProject { - fn execute(&self) -> Result<(), CommandError> { - Ok(()) +impl Command for Create { + fn execute(self) -> Result { + Ok(self.0) } } @@ -45,7 +41,8 @@ mod tests { #[test] 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()); } } diff --git a/lib/src/domain/project/dto.rs b/lib/src/domain/project/dto.rs new file mode 100644 index 0000000..216dd57 --- /dev/null +++ b/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 . +*/ + +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 {} diff --git a/lib/src/domain/project/mod.rs b/lib/src/domain/project/mod.rs index 37ecbe0..ac52e4e 100644 --- a/lib/src/domain/project/mod.rs +++ b/lib/src/domain/project/mod.rs @@ -1 +1,2 @@ -pub mod create_project; +pub mod create; +pub mod dto;