diff --git a/Cargo.lock b/Cargo.lock index 57225cf..cae0f16 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -107,6 +107,8 @@ dependencies = [ "dotenvy", "reqwest", "scraper", + "serde", + "serde_json", ] [[package]] @@ -1146,6 +1148,20 @@ name = "serde" version = "1.0.137" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "61ea8d54c77f8315140a05f4c7237403bf38b72704d031543aa1d16abbf517d1" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.137" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f26faba0c3959972377d3b2d306ee9f71faee9714294e41bb777f83f88578be" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] [[package]] name = "serde_json" diff --git a/Cargo.toml b/Cargo.toml index be4e684..183e749 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,4 +9,6 @@ edition = "2021" reqwest = {version = "0.11", features = ["blocking"]} scraper = "0.14.0" dotenvy = "0.15.1" -diesel = { version = "2.0.2", features = ["sqlite"] } \ No newline at end of file +diesel = { version = "2.0.2", features = ["sqlite"] } +serde = { version = "1.0", features = ["derive"] } +serde_json = "1.0" \ No newline at end of file diff --git a/src/extractor/artists.rs b/src/extractor/artists.rs index d0ef9d5..023f78c 100644 --- a/src/extractor/artists.rs +++ b/src/extractor/artists.rs @@ -1,16 +1,66 @@ +use diesel::{prelude::*}; +use serde::{Deserialize, Serialize}; use scraper::Html; use super::{Extractor, trim_whitespace, parse_date}; -#[derive(Debug, Default)] +use crate::schema::artists; + +#[derive(Debug, Default, Serialize, Deserialize, Queryable, Insertable, AsChangeset)] pub struct Artist { pub artist_name: String, pub artist_url: String, - pub date: String, + pub date: String, pub author: String, pub author_url: String, } +impl Artist { + pub fn new(new_user: Artist) -> Self { + Self { + id: Uuid::new_v4().to_string(), + name: new_user.name, + } + } + + pub fn all(conn: &SqliteConnection) -> QueryResult> { + users::table.load(conn) + } + + pub fn find_by_id(conn: &SqliteConnection, id: &String) -> QueryResult { + users::table + .filter(users::id.eq(id)) + .first(conn) + } + + pub fn create(conn: &SqliteConnection, new_user: &NewUser) -> QueryResult { + let user = User::new(new_user.clone()); + diesel::insert_into(users::table) + .values(&user) + .execute(conn)?; + + Ok(user) + } + + pub fn update(&self, conn: &SqliteConnection, data: &User) -> QueryResult { + use crate::schema::users::dsl::*; + + diesel::update(users.find(self.id.clone())) + .set(data) + .execute(conn)?; + + Ok(data.clone()) + } + + pub fn delete(&self, conn: &SqliteConnection) -> QueryResult { + use crate::schema::users::dsl::*; + + diesel::delete(users.filter(id.eq(self.id.clone()))).execute(conn)?; + + Ok(self.clone()) + } +} + impl Extractor for Artist { type Output = Self; fn extract_all(document: Html) -> Vec { diff --git a/src/main.rs b/src/main.rs index b2a1d5e..ed42783 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,8 @@ -mod extractor; +#[macro_use] +extern crate diesel; // Required for schema.rs + +pub(self) mod extractor; +pub(self) mod schema; fn main() { dotenvy::dotenv().unwrap();