Update visited date when event is fetched

This commit is contained in:
Ben Grant 2023-05-15 16:48:00 +10:00
parent 9157308398
commit aa3b323cb6
3 changed files with 24 additions and 9 deletions

View file

@ -135,11 +135,20 @@ impl Adaptor for DatastoreAdaptor {
async fn get_event(&self, id: String) -> Result<Option<Event>, Self::Error> { async fn get_event(&self, id: String) -> Result<Option<Event>, Self::Error> {
let mut client = self.client.lock().await; let mut client = self.client.lock().await;
// TODO: mark as visited let key = Key::new(EVENT_KIND).id(id.clone());
let existing_event = client.get::<Value, _>(key.clone()).await?;
Ok(client // Mark as visited if it exists
.get::<Value, _>(Key::new(EVENT_KIND).id(id.clone())) if let Some(mut event) = existing_event
.await? .clone()
.map(HashMap::<String, Value>::from_value)
.transpose()?
{
event.insert(String::from("visited"), Utc::now().timestamp().into_value());
client.put((key, event)).await?;
}
Ok(existing_event
.map(|value| parse_into_event(id, value)) .map(|value| parse_into_event(id, value))
.transpose()?) .transpose()?)
} }

View file

@ -91,11 +91,16 @@ impl Adaptor for SqlAdaptor {
} }
async fn get_event(&self, id: String) -> Result<Option<Event>, Self::Error> { async fn get_event(&self, id: String) -> Result<Option<Event>, Self::Error> {
// TODO: mark as visited let existing_event = event::Entity::find_by_id(id).one(&self.db).await?;
Ok(event::Entity::find_by_id(id)
.one(&self.db) // Mark as visited
.await? if let Some(event) = existing_event.clone() {
.map(|model| model.into())) let mut event: event::ActiveModel = event.into();
event.visited_at = Set(Utc::now().naive_utc());
event.save(&self.db).await?;
}
Ok(existing_event.map(|model| model.into()))
} }
async fn create_event(&self, event: Event) -> Result<Event, Self::Error> { async fn create_event(&self, event: Event) -> Result<Event, Self::Error> {

View file

@ -21,6 +21,7 @@ pub trait Adaptor: Send + Sync {
async fn get_people(&self, event_id: String) -> Result<Option<Vec<Person>>, Self::Error>; async fn get_people(&self, event_id: String) -> Result<Option<Vec<Person>>, Self::Error>;
async fn upsert_person(&self, event_id: String, person: Person) -> Result<Person, Self::Error>; async fn upsert_person(&self, event_id: String, person: Person) -> Result<Person, Self::Error>;
/// Get an event and update visited date to current time
async fn get_event(&self, id: String) -> Result<Option<Event>, Self::Error>; async fn get_event(&self, id: String) -> Result<Option<Event>, Self::Error>;
async fn create_event(&self, event: Event) -> Result<Event, Self::Error>; async fn create_event(&self, event: Event) -> Result<Event, Self::Error>;