use crate::auth::Exercise;
use crate::db::Database;
impl Database {
pub async fn create_exercise(&self, exercise: &Exercise) -> Result<(), String> {
self.conn()
.await?
.execute(
"INSERT INTO exercises (id, name, description, unit, points_per_unit, created_at) VALUES (?1, ?2, ?3, ?4, ?5, ?6)",
turso::params![exercise.id.clone(), exercise.name.clone(), exercise.description.clone(), exercise.unit.clone(), exercise.points_per_unit, exercise.created_at.clone()],
)
.await
.map_err(|e| e.to_string())?;
Ok(())
}
pub async fn get_all_exercises(&self) -> Result<Vec<Exercise>, String> {
let mut rows = self
.conn()
.await?
.query(
"SELECT id, name, description, unit, points_per_unit, created_at FROM exercises ORDER BY name",
(),
)
.await
.map_err(|e| e.to_string())?;
let mut result = Vec::new();
while let Some(row) = rows.next().await.map_err(|e| e.to_string())? {
result.push(Exercise {
id: row.get(0).map_err(|e| e.to_string())?,
name: row.get(1).map_err(|e| e.to_string())?,
description: row.get(2).map_err(|e| e.to_string())?,
unit: row.get(3).map_err(|e| e.to_string())?,
points_per_unit: row.get(4).map_err(|e| e.to_string())?,
created_at: row.get(5).map_err(|e| e.to_string())?,
});
}
Ok(result)
}
pub async fn get_exercise_by_id(&self, id: &str) -> Result<Option<Exercise>, String> {
let mut rows = self
.conn()
.await?
.query(
"SELECT id, name, description, unit, points_per_unit, created_at FROM exercises WHERE id = ?1",
turso::params![id],
)
.await
.map_err(|e| e.to_string())?;
if let Some(row) = rows.next().await.map_err(|e| e.to_string())? {
Ok(Some(Exercise {
id: row.get(0).map_err(|e| e.to_string())?,
name: row.get(1).map_err(|e| e.to_string())?,
description: row.get(2).map_err(|e| e.to_string())?,
unit: row.get(3).map_err(|e| e.to_string())?,
points_per_unit: row.get(4).map_err(|e| e.to_string())?,
created_at: row.get(5).map_err(|e| e.to_string())?,
}))
} else {
Ok(None)
}
}
pub async fn delete_exercise(&self, id: &str) -> Result<(), String> {
self.conn()
.await?
.execute("DELETE FROM exercises WHERE id = ?1", turso::params![id])
.await
.map_err(|e| e.to_string())?;
Ok(())
}
}