-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ac5dcb5
commit 84588d8
Showing
6 changed files
with
212 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
trackscape-discord-shared/src/database/clan_mate_collection_log_totals.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
use crate::database::ClanMateCollectionLogTotalsDb; | ||
use anyhow::Error; | ||
use async_trait::async_trait; | ||
use log::info; | ||
use mockall::automock; | ||
use mongodb::bson::{doc, DateTime}; | ||
use mongodb::{bson, Database}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Debug, Serialize, Deserialize, Clone)] | ||
pub struct ClanMateCollectionLogTotalModel { | ||
pub guild_id: u64, | ||
pub player_id: bson::oid::ObjectId, | ||
pub total: i64, | ||
pub created_at: DateTime, | ||
} | ||
|
||
impl ClanMateCollectionLogTotalModel { | ||
pub const COLLECTION_NAME: &'static str = "clan_mate_collection_log_totals"; | ||
|
||
pub fn new(guild_id: u64, player_id: bson::oid::ObjectId, total: i64) -> Self { | ||
Self { | ||
guild_id, | ||
player_id, | ||
total, | ||
created_at: DateTime::now(), | ||
} | ||
} | ||
} | ||
|
||
#[automock] | ||
#[async_trait] | ||
pub trait ClanMateCollectionLogTotals { | ||
fn new_instance(mongodb: Database) -> Self; | ||
|
||
async fn update_or_create( | ||
&self, | ||
guild_id: u64, | ||
player_id: bson::oid::ObjectId, | ||
total: i64, | ||
) -> Result<(), anyhow::Error>; | ||
} | ||
|
||
#[async_trait] | ||
impl ClanMateCollectionLogTotals for ClanMateCollectionLogTotalsDb { | ||
fn new_instance(mongodb: Database) -> Self { | ||
Self { db: mongodb } | ||
} | ||
|
||
async fn update_or_create( | ||
&self, | ||
guild_id: u64, | ||
player_id: bson::oid::ObjectId, | ||
total: i64, | ||
) -> Result<(), Error> { | ||
let collection = self.db.collection::<ClanMateCollectionLogTotalModel>( | ||
ClanMateCollectionLogTotalModel::COLLECTION_NAME, | ||
); | ||
|
||
let filter = doc! { | ||
"guild_id": bson::to_bson(&guild_id).unwrap(), | ||
"player_id": player_id.clone(), | ||
}; | ||
|
||
let update_result = collection | ||
.update_one( | ||
filter, | ||
doc! { | ||
"$set": { | ||
"total": bson::to_bson(&total).unwrap() | ||
} | ||
}, | ||
None, | ||
) | ||
.await?; | ||
info!("Update result: {:?}", update_result); | ||
if update_result.matched_count > 0 { | ||
return Ok(()); | ||
} | ||
let new_total = ClanMateCollectionLogTotalModel::new(guild_id, player_id, total); | ||
let _ = collection | ||
.insert_one(new_total, None) | ||
.await | ||
.expect("Error inserting new collection log total."); | ||
Ok(()) | ||
} | ||
} |
Oops, something went wrong.