From f8373c0753ce54833d9beaa5e19c9b9825c6175d Mon Sep 17 00:00:00 2001 From: Julio Biason Date: Thu, 2 Sep 2021 12:59:09 -0300 Subject: [PATCH] Working with repositories --- lib/migrations/20210823_create_table.sql | 2 +- lib/src/domain/command.rs | 2 +- lib/src/domain/project/create.rs | 9 ++++-- lib/src/domain/project/dto.rs | 8 +++++ lib/src/domain/project/mod.rs | 19 +++++++++++ lib/src/domain/project/repository.rs | 41 ++++++++++++++++++++++++ 6 files changed, 76 insertions(+), 5 deletions(-) create mode 100644 lib/src/domain/project/repository.rs diff --git a/lib/migrations/20210823_create_table.sql b/lib/migrations/20210823_create_table.sql index 5038398..cfebffe 100644 --- a/lib/migrations/20210823_create_table.sql +++ b/lib/migrations/20210823_create_table.sql @@ -1,6 +1,6 @@ -- Projects CREATE TABLE project ( - id INTEGER NOT NULL PRIMARY KEY, + code TEXT NOT NULL PRIMARY KEY, name TEXT NOT NULL ); CREATE UNIQUE INDEX project_name ON project (name); diff --git a/lib/src/domain/command.rs b/lib/src/domain/command.rs index 6eb30b2..28911e4 100644 --- a/lib/src/domain/command.rs +++ b/lib/src/domain/command.rs @@ -21,7 +21,7 @@ pub trait Dto {} /// Trait that all commands must implement. pub trait Command { - fn execute(self) -> Result; + async fn execute(self) -> Result; } /// Errors produced by invalid commands diff --git a/lib/src/domain/project/create.rs b/lib/src/domain/project/create.rs index 9fc430c..120c5cd 100644 --- a/lib/src/domain/project/create.rs +++ b/lib/src/domain/project/create.rs @@ -16,11 +16,12 @@ along with this program. If not, see . */ +use super::dto::Project; +use super::repository::Repository; +use crate::database; use crate::domain::command::Command; use crate::domain::command::CommandError; -use super::dto::Project; - struct Create(Project); impl Create { @@ -30,7 +31,9 @@ impl Create { } impl Command for Create { - fn execute(self) -> Result { + async fn execute(self) -> Result { + let pool = database::connect().await?; + let repo = Repository::new(&pool); Ok(self.0) } } diff --git a/lib/src/domain/project/dto.rs b/lib/src/domain/project/dto.rs index 216dd57..d53b143 100644 --- a/lib/src/domain/project/dto.rs +++ b/lib/src/domain/project/dto.rs @@ -30,6 +30,14 @@ impl Project { name: name.into(), } } + + pub fn code(&self) -> &str { + &self.code + } + + pub fn name(&self) -> &str { + &self.name + } } impl Dto for Project {} diff --git a/lib/src/domain/project/mod.rs b/lib/src/domain/project/mod.rs index ac52e4e..d91939f 100644 --- a/lib/src/domain/project/mod.rs +++ b/lib/src/domain/project/mod.rs @@ -1,2 +1,21 @@ +/* + 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 . +*/ + pub mod create; pub mod dto; +pub mod repository; diff --git a/lib/src/domain/project/repository.rs b/lib/src/domain/project/repository.rs new file mode 100644 index 0000000..ccee4ce --- /dev/null +++ b/lib/src/domain/project/repository.rs @@ -0,0 +1,41 @@ +/* + 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 sqlx::sqlite::Sqlite; +use sqlx::Pool; + +use super::dto::Project; + +struct Repository<'a> { + pool: &'a Pool, +} + +impl<'a> Repository<'a> { + fn new(pool: &'a Pool) -> Self { + Self { pool } + } + + async fn save(&self, project: Project) -> Result { + sqlx::query(r#"INSERT INTO project (code, name) VALUES (?, ?)"#) + .bind(&project.code()) + .bind(&project.name()) + .execute(self.pool) + .await?; + Ok(project) + } +}