Skip to content

Commit

Permalink
twiq: Fix in_memory_user_repository
Browse files Browse the repository at this point in the history
  • Loading branch information
bouzuya committed Oct 6, 2022
1 parent 01022a1 commit 306ed98
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
9 changes: 8 additions & 1 deletion twiq/crates/db/src/in_memory_user_repository.rs
Expand Up @@ -11,14 +11,19 @@ use crate::in_memory_event_store::InMemoryEventStore;
#[derive(Debug, Default)]
pub struct InMemoryUserRepository {
event_store: Arc<Mutex<InMemoryEventStore>>,
user_ids: Arc<Mutex<HashMap<UserId, EventStreamId>>>,
index: Arc<Mutex<HashMap<TwitterUserId, UserId>>>,
}

#[async_trait]
impl UserRepository for InMemoryUserRepository {
async fn find(&self, id: UserId) -> Result<Option<User>> {
let event_store = self.event_store.lock().await;
let event_stream_id = EventStreamId::from(id);
let user_ids = self.user_ids.lock().await;
let event_stream_id = match user_ids.get(&id) {
None => return Ok(None),
Some(event_stream_id) => *event_stream_id,
};
let event_stream = event_store
.find_event_stream(event_stream_id)
.await
Expand Down Expand Up @@ -47,6 +52,7 @@ impl UserRepository for InMemoryUserRepository {

async fn store(&self, before: Option<User>, after: User) -> Result<()> {
let event_store = self.event_store.lock().await;
let mut user_ids = self.user_ids.lock().await;
let mut index = self.index.lock().await;
event_store
.store(
Expand All @@ -55,6 +61,7 @@ impl UserRepository for InMemoryUserRepository {
)
.await
.map_err(|e| Error::Unknown(e.to_string()))?;
user_ids.insert(after.id(), after.event_stream().id());
index.insert(after.twitter_user_id().clone(), after.id());
Ok(())
}
Expand Down
4 changes: 4 additions & 0 deletions twiq/crates/domain/src/aggregate/user.rs
Expand Up @@ -66,6 +66,10 @@ impl User {
Ok(())
}

pub fn event_stream(&self) -> &EventStream {
&self.event_stream
}

pub fn twitter_user_id(&self) -> &TwitterUserId {
&self.twitter_user_id
}
Expand Down

0 comments on commit 306ed98

Please sign in to comment.