use diesel::prelude::*;
use crate::models::DBVideo;


pub fn get_video(conn: &mut SqliteConnection, video_id: String) -> Result<Option<String>, diesel::result::Error> {
    use crate::schema::videos::dsl::*;
    let result = videos
        .filter(id.eq(video_id))
        .first::<DBVideo>(conn)
        .optional()?;
    match result{
        Some(video) => Ok(Some(video.url)),
        None => Ok(None),
    }
}

pub fn insert_video(conn: &mut SqliteConnection, new_id: &str, new_url: &str) -> Result<usize, diesel::result::Error> {
    use crate::schema::videos::dsl::*;
    diesel::insert_into(videos).values(DBVideo{
        id: new_id.to_string(),
        url: new_url.to_string(),
    }).execute(conn)
}


