Begin add diesel
This commit is contained in:
parent
135c84109f
commit
8b17d896e2
4 changed files with 76 additions and 4 deletions
|
@ -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…
Add table
Add a link
Reference in a new issue