Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Repository split #38

Merged
merged 4 commits into from
Jul 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion application/pre_token_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use serde_json::{to_value, Value};
use tokio::sync::OnceCell;
use tracing::{error, info, Level};

use raktar::repository::{DynamoDBRepository, Repository};
use raktar::repository::{DynamoDBRepository, UserRepository};

#[tokio::main]
async fn main() -> Result<(), Error> {
Expand Down
2 changes: 1 addition & 1 deletion application/repository.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
mod base;
pub mod dynamodb;

pub use base::{DynRepository, Repository};
pub use base::{DynRepository, Repository, UserRepository};
pub use dynamodb::DynamoDBRepository;
55 changes: 8 additions & 47 deletions application/repository/base.rs
Original file line number Diff line number Diff line change
@@ -1,53 +1,14 @@
use std::sync::Arc;
mod krate;
mod token;
mod user;

use semver::Version;
use std::sync::Arc;

use crate::auth::AuthenticatedUser;
use crate::error::AppResult;
use crate::models::crate_summary::CrateSummary;
use crate::models::index::PackageInfo;
use crate::models::metadata::Metadata;
use crate::models::token::TokenItem;
use crate::models::user::{CognitoUserData, User, UserId};
pub use crate::repository::base::krate::CrateRepository;
pub use crate::repository::base::token::TokenRepository;
pub use crate::repository::base::user::UserRepository;

#[async_trait::async_trait]
pub trait Repository {
async fn get_package_info(&self, crate_name: &str) -> AppResult<String>;
async fn store_package_info(
&self,
crate_name: &str,
version: &Version,
package_info: PackageInfo,
metadata: Metadata,
authenticated_user: &AuthenticatedUser,
) -> AppResult<()>;
async fn set_yanked(&self, crate_name: &str, version: &Version, yanked: bool) -> AppResult<()>;
async fn list_owners(&self, crate_name: &str) -> AppResult<Vec<User>>;
async fn add_owners(&self, crate_name: &str, user_ids: Vec<String>) -> AppResult<()>;
async fn get_crate_summary(&self, crate_name: &str) -> AppResult<Option<CrateSummary>>;
async fn get_all_crate_details(
&self,
filter: Option<String>,
limit: usize,
) -> AppResult<Vec<CrateSummary>>;
async fn get_crate_metadata(
&self,
crate_name: &str,
version: &Version,
) -> AppResult<Option<Metadata>>;
async fn list_crate_versions(&self, crate_name: &str) -> AppResult<Vec<Version>>;
async fn store_auth_token(
&self,
token: &[u8],
name: String,
user_id: u32,
) -> AppResult<TokenItem>;
async fn delete_auth_token(&self, user_id: u32, token_id: String) -> AppResult<()>;
async fn list_auth_tokens(&self, user_id: u32) -> AppResult<Vec<TokenItem>>;
async fn get_auth_token(&self, token: &[u8]) -> AppResult<Option<TokenItem>>;
async fn update_or_create_user(&self, user_data: CognitoUserData) -> AppResult<User>;
async fn get_user_by_id(&self, user_id: UserId) -> AppResult<Option<User>>;
async fn get_users(&self) -> AppResult<Vec<User>>;
}
pub trait Repository: CrateRepository + UserRepository + TokenRepository {}

pub type DynRepository = Arc<dyn Repository + Send + Sync>;
35 changes: 35 additions & 0 deletions application/repository/base/krate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use crate::auth::AuthenticatedUser;
use crate::error::AppResult;
use crate::models::crate_summary::CrateSummary;
use crate::models::index::PackageInfo;
use crate::models::metadata::Metadata;
use crate::models::user::User;
use semver::Version;

#[async_trait::async_trait]
pub trait CrateRepository {
async fn get_package_info(&self, crate_name: &str) -> AppResult<String>;
async fn store_package_info(
&self,
crate_name: &str,
version: &Version,
package_info: PackageInfo,
metadata: Metadata,
authenticated_user: &AuthenticatedUser,
) -> AppResult<()>;
async fn set_yanked(&self, crate_name: &str, version: &Version, yanked: bool) -> AppResult<()>;
async fn list_owners(&self, crate_name: &str) -> AppResult<Vec<User>>;
async fn add_owners(&self, crate_name: &str, user_ids: Vec<String>) -> AppResult<()>;
async fn get_crate_summary(&self, crate_name: &str) -> AppResult<Option<CrateSummary>>;
async fn get_all_crate_details(
&self,
filter: Option<String>,
limit: usize,
) -> AppResult<Vec<CrateSummary>>;
async fn get_crate_metadata(
&self,
crate_name: &str,
version: &Version,
) -> AppResult<Option<Metadata>>;
async fn list_crate_versions(&self, crate_name: &str) -> AppResult<Vec<Version>>;
}
15 changes: 15 additions & 0 deletions application/repository/base/token.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use crate::error::AppResult;
use crate::models::token::TokenItem;

#[async_trait::async_trait]
pub trait TokenRepository {
async fn store_auth_token(
&self,
token: &[u8],
name: String,
user_id: u32,
) -> AppResult<TokenItem>;
async fn delete_auth_token(&self, user_id: u32, token_id: String) -> AppResult<()>;
async fn list_auth_tokens(&self, user_id: u32) -> AppResult<Vec<TokenItem>>;
async fn get_auth_token(&self, token: &[u8]) -> AppResult<Option<TokenItem>>;
}
14 changes: 14 additions & 0 deletions application/repository/base/user.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use crate::error::AppResult;
use crate::models::user::{CognitoUserData, User, UserId};

#[async_trait::async_trait]
pub trait UserRepository {
/// Used by the pre-token Lambda to ensure the SSO user is up to date in the repository.
///
/// This either creates a new user, or checks whether the existing user has the
/// latest data (e.g. first name and last name being up to date) and bring the
/// database in line if it's out of sync.
async fn update_or_create_user(&self, user_data: CognitoUserData) -> AppResult<User>;
async fn get_user_by_id(&self, user_id: UserId) -> AppResult<Option<User>>;
async fn get_users(&self) -> AppResult<Vec<User>>;
}
Loading