2022-10-06 12:54:25 +00:00
|
|
|
//! All logic for group datas.
|
|
|
|
|
2022-10-07 13:27:43 +00:00
|
|
|
use {
|
|
|
|
color_eyre::Result,
|
|
|
|
sea_orm::{prelude::*, QueryOrder, QuerySelect},
|
|
|
|
};
|
2022-10-06 12:54:25 +00:00
|
|
|
|
2022-10-07 13:27:43 +00:00
|
|
|
pub use crate::{
|
|
|
|
entities::group_data::{
|
|
|
|
ActiveModel as GroupDataActiveModel, Column as GroupDataColumn,
|
|
|
|
Entity as GroupDataEntity, Model as GroupDataModel,
|
|
|
|
},
|
|
|
|
snapshots::SnapshotModel,
|
|
|
|
};
|
2022-10-06 12:54:25 +00:00
|
|
|
|
2022-10-07 13:27:43 +00:00
|
|
|
impl GroupDataModel {
|
|
|
|
/// Get all group datas from a given snapshot.
|
|
|
|
pub async fn get_all_by_snapshot(
|
|
|
|
db: &DatabaseConnection,
|
|
|
|
snapshot: &SnapshotModel,
|
|
|
|
) -> Result<Vec<Self>> {
|
|
|
|
let groups = snapshot.find_related(GroupDataEntity).all(db).await?;
|
|
|
|
Ok(groups)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the group with the highest subscriber count from a given snapshot.
|
|
|
|
pub async fn get_highest_subscribers(
|
|
|
|
db: &DatabaseConnection,
|
|
|
|
snapshot: &SnapshotModel,
|
|
|
|
) -> Result<Option<Self>> {
|
|
|
|
let group = snapshot
|
|
|
|
.find_related(GroupDataEntity)
|
|
|
|
.order_by_desc(GroupDataColumn::Subscribers)
|
|
|
|
.one(db)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
Ok(group)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the N most recently saved group datas from a given group name.
|
|
|
|
pub async fn get_n_most_recent(
|
|
|
|
db: &DatabaseConnection,
|
|
|
|
amount: u64,
|
|
|
|
name: &str,
|
|
|
|
) -> Result<Vec<Self>> {
|
|
|
|
let groups = GroupDataEntity::find()
|
2022-10-08 19:34:43 +00:00
|
|
|
.order_by_asc(GroupDataColumn::SnapshotId)
|
2022-10-07 13:27:43 +00:00
|
|
|
.filter(GroupDataColumn::Name.eq(name))
|
|
|
|
.limit(amount)
|
|
|
|
.all(db)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
Ok(groups)
|
|
|
|
}
|
2022-10-06 12:54:25 +00:00
|
|
|
}
|