-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: simonsan <[email protected]>
- Loading branch information
Showing
8 changed files
with
315 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
use pace_error::{DatabaseStorageErrorKind, PaceOptResult, PaceResult}; | ||
use sea_orm::{EntityTrait, IntoActiveModel}; | ||
|
||
use crate::entity::activities::{Entity as ActivityEntity, Model as ActivityModel}; | ||
use crate::repository::Repository; | ||
|
||
pub struct ActivityRepository<'conn, C> { | ||
connection: &'conn C, | ||
} | ||
|
||
impl<'conn, C> ActivityRepository<'conn, C> { | ||
pub const fn new(connection: &'conn C) -> Self { | ||
Self { connection } | ||
} | ||
} | ||
|
||
impl<'conn> Repository<ActivityModel> for ActivityRepository<'conn, sea_orm::DatabaseConnection> { | ||
async fn read(&self, id: &str) -> PaceOptResult<ActivityModel> { | ||
Ok(ActivityEntity::find_by_id(id) | ||
.one(self.connection) | ||
.await | ||
.map_err(|source| DatabaseStorageErrorKind::RepositoryReadFailed { | ||
source, | ||
item_type: "activity".to_string(), | ||
item_id: id.to_string(), | ||
})?) | ||
} | ||
|
||
async fn read_all(&self) -> PaceOptResult<Vec<ActivityModel>> { | ||
let items = ActivityEntity::find() | ||
.all(self.connection) | ||
.await | ||
.map_err(|source| DatabaseStorageErrorKind::RepositoryReadFailed { | ||
source, | ||
item_type: "activity".to_string(), | ||
item_id: "all".to_string(), | ||
})?; | ||
|
||
if items.is_empty() { | ||
return Ok(None); | ||
} | ||
|
||
Ok(Some(items)) | ||
} | ||
|
||
async fn create(&self, model: &ActivityModel) -> PaceResult<String> { | ||
// TODO: What else should we do with ActiveModel here? | ||
let active_model = model.clone().into_active_model(); | ||
|
||
let id = ActivityEntity::insert(active_model) | ||
.exec(self.connection) | ||
.await | ||
.map_err(|source| DatabaseStorageErrorKind::RepositoryCreateFailed { | ||
source, | ||
item_type: "activity".to_string(), | ||
})? | ||
.last_insert_id; | ||
|
||
Ok(id) | ||
} | ||
|
||
async fn update(&self, id: &str, model: &ActivityModel) -> PaceResult<()> { | ||
unimplemented!() | ||
} | ||
|
||
async fn delete(&self, id: &str) -> PaceOptResult<ActivityModel> { | ||
let item = self.read(id).await?; | ||
|
||
// TODO: Unused result here, what should we do with the rows affected? | ||
_ = ActivityEntity::delete_by_id(id) | ||
.exec(self.connection) | ||
.await | ||
.map_err(|source| DatabaseStorageErrorKind::RepositoryDeleteFailed { | ||
source, | ||
item_type: "activity".to_string(), | ||
item_id: id.to_string(), | ||
})?; | ||
|
||
Ok(item) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,81 @@ | ||
use pace_error::{PaceOptResult, PaceResult}; | ||
use sea_orm::{sea_query::Query, DatabaseConnection}; | ||
use pace_error::{DatabaseStorageErrorKind, PaceOptResult, PaceResult}; | ||
use sea_orm::{EntityTrait, IntoActiveModel}; | ||
|
||
use crate::entity::categories::{Entity as CategoryEntity, Model as CategoryModel}; | ||
use crate::entity::tags::Entity as TagEntity; | ||
use crate::repository::Repository; | ||
|
||
pub struct CategoryRepository { | ||
connection: DatabaseConnection, | ||
pub struct CategoryRepository<'conn, C> { | ||
connection: &'conn C, | ||
} | ||
|
||
impl Repository<CategoryEntity> for CategoryRepository { | ||
fn read(&self, id: &str) -> PaceOptResult<CategoryEntity> { | ||
unimplemented!() | ||
impl<'conn, C> CategoryRepository<'conn, C> { | ||
pub const fn new(connection: &'conn C) -> Self { | ||
Self { connection } | ||
} | ||
} | ||
|
||
fn read_all(&self) -> PaceOptResult<Vec<CategoryEntity>> { | ||
unimplemented!() | ||
impl<'conn> Repository<CategoryModel> for CategoryRepository<'conn, sea_orm::DatabaseConnection> { | ||
async fn read(&self, id: &str) -> PaceOptResult<CategoryModel> { | ||
Ok(CategoryEntity::find_by_id(id) | ||
.one(self.connection) | ||
.await | ||
.map_err(|source| DatabaseStorageErrorKind::RepositoryReadFailed { | ||
source, | ||
item_type: "category".to_string(), | ||
item_id: id.to_string(), | ||
})?) | ||
} | ||
|
||
fn create(&self, entity: &CategoryEntity) -> PaceResult<String> { | ||
unimplemented!() | ||
async fn read_all(&self) -> PaceOptResult<Vec<CategoryModel>> { | ||
let items = CategoryEntity::find() | ||
.all(self.connection) | ||
.await | ||
.map_err(|source| DatabaseStorageErrorKind::RepositoryReadFailed { | ||
source, | ||
item_type: "category".to_string(), | ||
item_id: "all".to_string(), | ||
})?; | ||
|
||
if items.is_empty() { | ||
return Ok(None); | ||
} | ||
|
||
Ok(Some(items)) | ||
} | ||
|
||
fn update(&self, id: &str, entity: &CategoryEntity) -> PaceResult<()> { | ||
unimplemented!() | ||
async fn create(&self, model: &CategoryModel) -> PaceResult<String> { | ||
// TODO: What else should we do with ActiveModel here? | ||
let active_model = model.clone().into_active_model(); | ||
|
||
let id = CategoryEntity::insert(active_model) | ||
.exec(self.connection) | ||
.await | ||
.map_err(|source| DatabaseStorageErrorKind::RepositoryCreateFailed { | ||
source, | ||
item_type: "category".to_string(), | ||
})? | ||
.last_insert_id; | ||
|
||
Ok(id) | ||
} | ||
|
||
fn delete(&self, id: &str) -> PaceResult<()> { | ||
async fn update(&self, id: &str, model: &CategoryModel) -> PaceResult<()> { | ||
unimplemented!() | ||
} | ||
|
||
async fn delete(&self, id: &str) -> PaceOptResult<CategoryModel> { | ||
let item = self.read(id).await?; | ||
|
||
// TODO: Unused result here, what should we do with the rows affected? | ||
_ = CategoryEntity::delete_by_id(id) | ||
.exec(self.connection) | ||
.await | ||
.map_err(|source| DatabaseStorageErrorKind::RepositoryDeleteFailed { | ||
source, | ||
item_type: "category".to_string(), | ||
item_id: id.to_string(), | ||
})?; | ||
|
||
Ok(item) | ||
} | ||
} |
Oops, something went wrong.