algohub_server/utils/
asset.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
use std::path::PathBuf;

use anyhow::Result;
use surrealdb::{engine::remote::ws::Client, sql::Thing, Surreal};

use crate::models::{asset::Asset, UserRecordId};

pub async fn create(
    db: &Surreal<Client>,
    owner: UserRecordId,
    name: &str,
    path: PathBuf,
) -> Result<Option<Asset>> {
    Ok(db
        .create("asset")
        .content(Asset {
            id: None,
            name: name.to_string(),
            owner: owner.into(),
            path,
        })
        .await?)
}

pub async fn get_by_id(db: &Surreal<Client>, id: &str) -> Result<Option<Asset>> {
    Ok(db.select(("asset", id)).await?)
}

pub async fn list_by_owner(db: &Surreal<Client>, owner: UserRecordId) -> Result<Vec<Asset>> {
    Ok(db
        .query("SELECT * FROM asset WHERE owner = $owner")
        .bind(("owner", Into::<Thing>::into(owner)))
        .await?
        .take(0)?)
}

pub async fn delete(db: &Surreal<Client>, id: &str) -> Result<Option<Asset>> {
    Ok(db.delete(("asset", id)).await?)
}