use std::error::Error;
use bb8_postgres::bb8::Pool;
use bb8_postgres::PostgresConnectionManager;
use bb8_postgres::tokio_postgres::{GenericClient, NoTls, Row};
const GET_NEW_COMPLETED_TASKS: &str = "SELECT task_id FROM rec23.task_sub_completed";
const DELETE: &str = "DELETE FROM rec23.task_sub_completed WHERE task_id = $1";
pub struct TaskSubComplete{
pub task_id: i32
}
impl TaskSubComplete{
pub fn convert_from_row(row: &Row) -> Self {
Self {
task_id: row.get("task_id")
}
}
pub async fn get_new_completed_tasks(pool: &Pool<PostgresConnectionManager<NoTls>>)->Result<Vec<TaskSubComplete>, Box<dyn Error + Sync + Send>>{
let connection = pool.get().await?;
let client = connection.client();
let rows = client.query(GET_NEW_COMPLETED_TASKS, &[]).await?;
let result = rows.into_iter().map(|row| TaskSubComplete::convert_from_row(&row)).collect();
Ok(result)
}
pub async fn delete(self, pool: &Pool<PostgresConnectionManager<NoTls>>)->Result<(), Box<dyn Error + Sync + Send>>{
let connection = pool.get().await?;
let client = connection.client();
client.execute(DELETE, &[&self.task_id]).await?;
Ok(())
}
}