Skip to content

Commit

Permalink
refactor: mongodb repository (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
kentSarmiento authored Dec 25, 2023
1 parent df5555e commit b18e387
Show file tree
Hide file tree
Showing 15 changed files with 477 additions and 335 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/development.yml
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ jobs:

- name: Generate and fix code coverage
run: |
cargo llvm-cov --ignore-filename-regex "main|repository" --lcov --output-path lcov.info
cargo llvm-cov --ignore-filename-regex "main|inmemory" --lcov --output-path lcov.info
./rust-covfix lcov.info -o lcov.info
env:
RUST_TEST_THREADS: 1
Expand Down
42 changes: 42 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions link-for-later/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ validator = { version = "0.16.1", features = ["derive"] }
[dev-dependencies]
mockall = "0.12.0"
rand = "0.8.5"
rstest = "0.18.2"
tracing-test = "0.2.4"

10 changes: 6 additions & 4 deletions link-for-later/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ pub fn new(db: Database) -> Router {
let users_service = Arc::new(service::users::ServiceProvider {}) as DynUsersService;
let (links_repo, users_repo) = match db {
Database::MongoDb(db) => (
Arc::new(repository::mongodb::LinksDb::new(&db)) as DynLinksRepository,
Arc::new(repository::mongodb::UsersDb::new(&db)) as DynUsersRepository,
Arc::new(repository::mongodb::LinksRepositoryProvider::new(&db)) as DynLinksRepository,
Arc::new(repository::mongodb::UsersRepositoryProvider::new(&db)) as DynUsersRepository,
),
Database::InMemory => (
Arc::new(repository::inmemory::LinksDb::default()) as DynLinksRepository,
Arc::new(repository::inmemory::UsersDb::default()) as DynUsersRepository,
Arc::new(repository::inmemory::LinksRepositoryProvider::default())
as DynLinksRepository,
Arc::new(repository::inmemory::UsersRepositoryProvider::default())
as DynUsersRepository,
),
};

Expand Down
2 changes: 1 addition & 1 deletion link-for-later/src/controller/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::types::AppError;
impl IntoResponse for AppError {
fn into_response(self) -> Response {
let (status, error_message) = match self {
Self::ServerError | Self::DatabaseError => {
Self::ServerError | Self::DatabaseError(_) => {
(StatusCode::INTERNAL_SERVER_ERROR, self.to_string())
}
Self::LinkNotFound => (StatusCode::NOT_FOUND, self.to_string()),
Expand Down
8 changes: 4 additions & 4 deletions link-for-later/src/repository/inmemory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ use crate::types::{
use super::{Links as LinksRepository, Users as UsersRepository};

#[derive(Default)]
pub struct LinksDb {}
pub struct LinksRepositoryProvider {}

static INMEMORY_LINKS_DATA: Lazy<Mutex<Vec<LinkItem>>> = Lazy::new(|| Mutex::new(Vec::new()));
static INMEMORY_LINKS_DATA_COUNTER: Lazy<Mutex<Vec<usize>>> = Lazy::new(|| Mutex::new(Vec::new()));

#[derive(Default)]
pub struct UsersDb {}
pub struct UsersRepositoryProvider {}

static INMEMORY_USERS_DATA: Lazy<Mutex<Vec<UserInfo>>> = Lazy::new(|| Mutex::new(Vec::new()));
static INMEMORY_USERS_DATA_COUNTER: Lazy<Mutex<Vec<usize>>> = Lazy::new(|| Mutex::new(Vec::new()));

#[async_trait]
impl LinksRepository for LinksDb {
impl LinksRepository for LinksRepositoryProvider {
async fn find(&self, query: &LinkQuery) -> Result<Vec<LinkItem>> {
let filtered_links: Vec<LinkItem> = INMEMORY_LINKS_DATA
.lock()
Expand Down Expand Up @@ -84,7 +84,7 @@ impl LinksRepository for LinksDb {
}

#[async_trait]
impl UsersRepository for UsersDb {
impl UsersRepository for UsersRepositoryProvider {
async fn get(&self, query: &UserQuery) -> Result<UserInfo> {
INMEMORY_USERS_DATA
.lock()
Expand Down
Loading

0 comments on commit b18e387

Please sign in to comment.