Browse Source

Working with repositories

main
Julio Biason 3 years ago
parent
commit
f8373c0753
  1. 2
      lib/migrations/20210823_create_table.sql
  2. 2
      lib/src/domain/command.rs
  3. 9
      lib/src/domain/project/create.rs
  4. 8
      lib/src/domain/project/dto.rs
  5. 19
      lib/src/domain/project/mod.rs
  6. 41
      lib/src/domain/project/repository.rs

2
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);

2
lib/src/domain/command.rs

@ -21,7 +21,7 @@ pub trait Dto {}
/// Trait that all commands must implement.
pub trait Command<T: Dto> {
fn execute(self) -> Result<T, CommandError>;
async fn execute(self) -> Result<T, CommandError>;
}
/// Errors produced by invalid commands

9
lib/src/domain/project/create.rs

@ -16,11 +16,12 @@
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
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<Project> for Create {
fn execute(self) -> Result<Project, CommandError> {
async fn execute(self) -> Result<Project, CommandError> {
let pool = database::connect().await?;
let repo = Repository::new(&pool);
Ok(self.0)
}
}

8
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 {}

19
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 <https://www.gnu.org/licenses/>.
*/
pub mod create;
pub mod dto;
pub mod repository;

41
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 <https://www.gnu.org/licenses/>.
*/
use sqlx::sqlite::Sqlite;
use sqlx::Pool;
use super::dto::Project;
struct Repository<'a> {
pool: &'a Pool<Sqlite>,
}
impl<'a> Repository<'a> {
fn new(pool: &'a Pool<Sqlite>) -> Self {
Self { pool }
}
async fn save(&self, project: Project) -> Result<Project, sqlx::Error> {
sqlx::query(r#"INSERT INTO project (code, name) VALUES (?, ?)"#)
.bind(&project.code())
.bind(&project.name())
.execute(self.pool)
.await?;
Ok(project)
}
}
Loading…
Cancel
Save