Skip to content

Commit

Permalink
Merge pull request #32 from raktar-registry/full-name-for-users
Browse files Browse the repository at this point in the history
Full name for users
  • Loading branch information
davidsteiner authored Jul 6, 2023
2 parents 3127d47 + 737fb89 commit 290238a
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 3 deletions.
22 changes: 20 additions & 2 deletions application/graphql/schema.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use anyhow::anyhow;
use async_graphql::{Context, EmptySubscription, Object, Result, Schema};
use async_graphql::{Context, EmptySubscription, Object, Result, Schema, ID};
use semver::Version;
use std::str::FromStr;

use crate::auth::{generate_new_token, AuthenticatedUser};
use crate::graphql::types::{CrateSummary, CrateVersion, DeletedToken, GeneratedToken, Token};
use crate::graphql::types::{
CrateSummary, CrateVersion, DeletedToken, GeneratedToken, Token, User,
};
use crate::repository::DynRepository;

pub struct Query;
Expand Down Expand Up @@ -68,6 +70,22 @@ impl Query {
let token_items = repository.list_auth_tokens(user.id).await?;
Ok(token_items.into_iter().map(From::from).collect())
}

async fn user(&self, ctx: &Context<'_>, id: ID) -> Result<Option<User>> {
let repository = ctx.data::<DynRepository>()?;
let user = repository.get_user_by_id(id.parse::<u32>()?).await?;

Ok(user.map(|u| u.into()))
}

async fn users(&self, ctx: &Context<'_>) -> Result<Vec<User>> {
let repository = ctx.data::<DynRepository>()?;
repository
.get_users()
.await
.map(|users| users.into_iter().map(|u| u.into()).collect())
.map_err(|err| err.into())
}
}

pub struct Mutation;
Expand Down
4 changes: 4 additions & 0 deletions application/graphql/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,17 @@ impl From<CrateSummaryModel> for CrateSummary {
pub struct User {
id: ID,
login: String,
given_name: String,
family_name: String,
}

impl From<UserModel> for User {
fn from(value: UserModel) -> Self {
Self {
id: value.id.into(),
login: value.login,
given_name: value.given_name,
family_name: value.family_name,
}
}
}
Expand Down
1 change: 1 addition & 0 deletions application/repository/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ pub trait Repository {
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 type DynRepository = Arc<dyn Repository + Send + Sync>;
6 changes: 5 additions & 1 deletion application/repository/dynamodb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::models::index::PackageInfo;
use crate::models::metadata::Metadata;
use crate::models::token::TokenItem;
use crate::models::user::{CognitoUserData, User, UserId};
use crate::repository::dynamodb::user::get_user_by_id;
use crate::repository::dynamodb::user::{get_user_by_id, get_users};
use crate::repository::Repository;

static CRATES_PARTITION_KEY: &str = "CRATES";
Expand Down Expand Up @@ -637,4 +637,8 @@ impl Repository for DynamoDBRepository {
async fn get_user_by_id(&self, user_id: UserId) -> AppResult<Option<User>> {
get_user_by_id(&self.db_client, &self.table_name, user_id).await
}

async fn get_users(&self) -> AppResult<Vec<User>> {
get_users(&self.db_client, &self.table_name).await
}
}
19 changes: 19 additions & 0 deletions application/repository/dynamodb/user.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::error::AppResult;
use aws_sdk_dynamodb::types::AttributeValue;
use aws_sdk_dynamodb::Client;
use serde_dynamo::from_items;

use crate::models::user::{User, UserId};

Expand All @@ -26,3 +27,21 @@ pub async fn get_user_by_id(

Ok(user)
}
pub async fn get_users(db_client: &Client, table_name: &str) -> AppResult<Vec<User>> {
let output = db_client
.query()
.table_name(table_name)
.key_condition_expression("pk = :pk and begins_with(sk, :prefix)")
.expression_attribute_values(":pk", AttributeValue::S("USERS".to_string()))
.expression_attribute_values(":prefix", AttributeValue::S("ID#".to_string()))
.send()
.await?;

match output.items() {
None => Ok(vec![]),
Some(items) => {
let users = from_items(items.to_vec())?;
Ok(users)
}
}
}

0 comments on commit 290238a

Please sign in to comment.