algohub_server/utils/
solution.rs

1use anyhow::Result;
2use serde::Deserialize;
3use surrealdb::{engine::remote::ws::Client, sql::Thing, Surreal};
4
5use crate::models::solution::{CreateSolution, Solution};
6
7pub async fn create(db: &Surreal<Client>, sol: CreateSolution<'_>) -> Result<Option<Solution>> {
8    Ok(db
9        .create("solution")
10        .content(Into::<Solution>::into(sol))
11        .await?)
12}
13
14pub async fn delete(db: &Surreal<Client>, id: &str) -> Result<Option<Solution>> {
15    Ok(db.delete(("solution", id)).await?)
16}
17
18pub async fn get<M>(db: &Surreal<Client>, id: &str) -> Result<Option<M>>
19where
20    for<'de> M: Deserialize<'de>,
21{
22    Ok(db.select(("solution", id)).await?)
23}
24
25pub async fn list(db: &Surreal<Client>, problem: Thing) -> Result<Vec<Solution>> {
26    Ok(db
27        .query("SELECT * FROM solution WHERE problem = $problem")
28        .bind(("problem", problem))
29        .await?
30        .take(0)?)
31}
32
33pub async fn update(
34    db: &Surreal<Client>,
35    id: &str,
36    solution: CreateSolution<'_>,
37) -> Result<Option<Solution>> {
38    Ok(db
39        .update(("solution", id))
40        .content(Into::<Solution>::into(solution))
41        .await?)
42}