Add showing snapshots by ID.

This commit is contained in:
Bauke 2022-10-09 12:20:49 +02:00
parent de5d256c96
commit 9ec3b1c07d
Signed by: Bauke
GPG Key ID: C1C0F29952BCF558
3 changed files with 35 additions and 10 deletions

View File

@ -90,8 +90,12 @@ pub enum SnapshotSubcommands {
/// Show a snapshot.
Show {
/// The date of the snapshot to show, defaults to today.
#[clap(short, long)]
#[clap(short, long, group = "selection")]
date: Option<NaiveDate>,
/// The ID of the snapshot to show.
#[clap(long, group = "selection")]
id: Option<i64>,
},
}

View File

@ -58,16 +58,24 @@ pub async fn run() -> Result<()> {
}
}
SnapshotSubcommands::Show { date } => {
SnapshotSubcommands::Show { date, id } => {
let date = date.unwrap_or_else(today);
let snapshot = if let Some(snapshot) =
SnapshotModel::get_by_date(&db, date).await?
{
info!("Snapshot {snapshot:?}");
snapshot
} else {
info!("No snapshot exists for {date}");
let snapshot = match id {
Some(id) => SnapshotModel::get_by_id(&db, id).await,
None => SnapshotModel::get_by_date(&db, date).await,
}?;
let snapshot = match (snapshot, id) {
(None, Some(id)) => {
info!("No snapshot exists for id {id}");
return Ok(());
}
(None, None) => {
info!("No snapshot exists for date {date}");
return Ok(());
}
(Some(snapshot), _) => snapshot,
};
for group in GroupDataModel::get_all_by_snapshot(&db, &snapshot).await?

View File

@ -27,6 +27,19 @@ impl SnapshotModel {
Ok(existing)
}
/// Get a snapshot by a given ID.
pub async fn get_by_id(
db: &DatabaseConnection,
id: i64,
) -> Result<Option<Self>> {
let snapshot = SnapshotEntity::find()
.filter(SnapshotColumn::Id.eq(id))
.one(db)
.await?;
Ok(snapshot)
}
/// Get all snapshots.
pub async fn get_all(db: &DatabaseConnection) -> Result<Vec<Self>> {
let snapshots = SnapshotEntity::find().all(db).await?;