Begin add diesel
This commit is contained in:
parent
135c84109f
commit
8b17d896e2
4 changed files with 76 additions and 4 deletions
16
Cargo.lock
generated
16
Cargo.lock
generated
|
@ -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"
|
||||
|
|
|
@ -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"] }
|
||||
diesel = { version = "2.0.2", features = ["sqlite"] }
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = "1.0"
|
|
@ -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<Vec<Self>> {
|
||||
users::table.load(conn)
|
||||
}
|
||||
|
||||
pub fn find_by_id(conn: &SqliteConnection, id: &String) -> QueryResult<Self> {
|
||||
users::table
|
||||
.filter(users::id.eq(id))
|
||||
.first(conn)
|
||||
}
|
||||
|
||||
pub fn create(conn: &SqliteConnection, new_user: &NewUser) -> QueryResult<Self> {
|
||||
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<Self> {
|
||||
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<Self> {
|
||||
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<Self> {
|
||||
|
|
|
@ -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();
|
||||
|
|
Loading…
Reference in a new issue