watchlist add: event only if inserted; adr 0015 jetstream/relay accuracy; drop vacuous assert
This commit is contained in:
@@ -16,15 +16,23 @@ use super::InMemoryStore;
|
||||
|
||||
#[async_trait]
|
||||
impl WatchlistRepository for InMemoryStore {
|
||||
/// `INSERT OR IGNORE` on UNIQUE(user_id, movie_id): adding twice is a no-op.
|
||||
/// `INSERT OR IGNORE` on UNIQUE(user_id, movie_id): adding twice is a no-op
|
||||
/// and, like the adapter, records events only for the row it inserted.
|
||||
async fn add(&self, entry: &WatchlistEntry, events: &[DomainEvent]) -> Result<(), DomainError> {
|
||||
let key = (entry.user_id.value(), entry.movie_id.value());
|
||||
self.watchlist
|
||||
.write()
|
||||
.unwrap()
|
||||
.entry(key)
|
||||
.or_insert_with(|| entry.clone());
|
||||
self.record_events(events);
|
||||
let inserted = {
|
||||
let mut entries = self.watchlist.write().unwrap();
|
||||
match entries.entry(key) {
|
||||
std::collections::hash_map::Entry::Vacant(slot) => {
|
||||
slot.insert(entry.clone());
|
||||
true
|
||||
}
|
||||
std::collections::hash_map::Entry::Occupied(_) => false,
|
||||
}
|
||||
};
|
||||
if inserted {
|
||||
self.record_events(events);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user