Refactored Setter methods for the diesel models.

This commit is contained in:
Jordan Petridis 2017-09-22 13:06:33 +03:00
parent 709a3e555d
commit cc353c120e
No known key found for this signature in database
GPG Key ID: CEABAD9F5683B9A6
2 changed files with 35 additions and 36 deletions

View File

@ -37,8 +37,8 @@ fn insert_source(con: &SqliteConnection, url: &str) -> Result<Source> {
Ok(mut bar) => {
// TODO: Cmp first before replacing
// FIXME: NewSource has None values for etag, and last_mod atm
// bar.set_http_etag(foo.http_etag.map(|x| x.to_string()));
// bar.set_last_modified(foo.last_modified.map(|x| x.to_string()));
// bar.set_http_etag(foo.http_etag);
// bar.set_last_modified(foo.last_modified);
// bar.save_changes::<Source>(con)?;
}
Err(_) => {
@ -61,11 +61,11 @@ fn index_podcast(
match dbqueries::load_podcast(con, &pd.title) {
Ok(mut foo) => {
// TODO: Cmp first before replacing
foo.set_link(pd.link);
foo.set_description(pd.description);
foo.set_image_uri(pd.image_uri.map(|x| x.to_string()));
foo.set_link(&pd.link);
foo.set_description(&pd.description);
foo.set_image_uri(pd.image_uri.as_ref().map(|s| s.as_str()));
foo.save_changes::<Podcast>(con)?;
}
}
Err(_) => {
diesel::insert(&pd).into(schema::podcast::table).execute(
con,
@ -82,10 +82,10 @@ fn index_episode(con: &SqliteConnection, item: &rss::Item, parent: &Podcast) ->
match dbqueries::load_episode(con, &ep.uri.unwrap()) {
Ok(mut foo) => {
// TODO: Cmp first before replacing
foo.set_title(ep.title.map(|x| x.to_string()));
foo.set_description(ep.description.map(|x| x.to_string()));
foo.set_published_date(ep.published_date.map(|x| x.to_string()));
foo.set_guid(ep.guid.map(|x| x.to_string()));
foo.set_title(ep.title);
foo.set_description(ep.description);
foo.set_published_date(ep.published_date);
foo.set_guid(ep.guid);
foo.set_length(ep.length);
foo.set_epoch(ep.length);
foo.save_changes::<Episode>(con)?;

View File

@ -34,48 +34,48 @@ impl Episode {
self.title.as_ref().map(|s| s.as_str())
}
pub fn set_title(&mut self, value: Option<String>) {
self.title = value;
pub fn set_title(&mut self, value: Option<&str>) {
self.title = value.map(|x| x.to_string());
}
pub fn uri(&self) -> Option<&str> {
self.uri.as_ref().map(|s| s.as_str())
}
pub fn set_uri(&mut self, value: Option<String>) {
self.uri = value;
pub fn set_uri(&mut self, value: Option<&str>) {
self.uri = value.map(|x| x.to_string());
}
pub fn local_uri(&self) -> Option<&str> {
self.local_uri.as_ref().map(|s| s.as_str())
}
pub fn set_local_uri(&mut self, value: Option<String>) {
self.local_uri = value;
pub fn set_local_uri(&mut self, value: Option<&str>) {
self.local_uri = value.map(|x| x.to_string());
}
pub fn description(&self) -> Option<&str> {
self.description.as_ref().map(|s| s.as_str())
}
pub fn set_description(&mut self, value: Option<String>) {
self.description = value;
pub fn set_description(&mut self, value: Option<&str>) {
self.description = value.map(|x| x.to_string());
}
pub fn published_date(&self) -> Option<&str> {
self.published_date.as_ref().map(|s| s.as_str())
}
pub fn set_published_date(&mut self, value: Option<String>) {
self.published_date = value;
pub fn set_published_date(&mut self, value: Option<&str>) {
self.published_date = value.map(|x| x.to_string());
}
pub fn guid(&self) -> Option<&str> {
self.guid.as_ref().map(|s| s.as_str())
}
pub fn set_guid(&mut self, value: Option<String>) {
self.guid = value;
pub fn set_guid(&mut self, value: Option<&str>) {
self.guid = value.map(|x| x.to_string());
}
pub fn epoch(&self) -> Option<i32> {
@ -122,24 +122,24 @@ impl Podcast {
&self.link
}
pub fn set_link(&mut self, value: String) {
self.link = value;
pub fn set_link(&mut self, value: &str) {
self.link = value.to_string();
}
pub fn description(&self) -> &str {
&self.description
}
pub fn set_description(&mut self, value: String) {
self.description = value;
pub fn set_description(&mut self, value: &str) {
self.description = value.to_string();
}
pub fn image_uri(&self) -> Option<&str> {
self.image_uri.as_ref().map(|s| s.as_str())
}
pub fn set_image_uri(&mut self, value: Option<String>) {
self.image_uri = value;
pub fn set_image_uri(&mut self, value: Option<&str>) {
self.image_uri = value.map(|x| x.to_string());
}
}
@ -166,16 +166,16 @@ impl<'a> Source {
self.last_modified.as_ref().map(|s| s.as_str())
}
pub fn set_last_modified(&mut self, value: Option<String>) {
self.last_modified = value;
pub fn set_last_modified(&mut self, value: Option<&str>) {
self.last_modified = value.map(|x| x.to_string());
}
pub fn http_etag(&self) -> Option<&str> {
self.http_etag.as_ref().map(|s| s.as_str())
}
pub fn set_http_etag(&mut self, value: Option<String>) {
self.http_etag = value;
pub fn set_http_etag(&mut self, value: Option<&str>) {
self.http_etag = value.map(|x| x.to_string());
}
/// Fetch the xml feed from the source url, update the etag headers,
@ -195,6 +195,7 @@ impl<'a> Source {
Ok(chan)
}
/// Extract Etag and LastModifier from req, and update self and the corresponding db row.
fn update_etag(&mut self, con: &SqliteConnection, req: &reqwest::Response) -> Result<()> {
let headers = req.headers();
@ -260,10 +261,8 @@ pub struct NewPodcast {
pub source_id: i32,
}
impl<'a> NewPodcast {
// pub fn new(parent: &Source) {}
pub fn from_url(uri: &'a str, parent: &Source) -> Result<NewPodcast> {
impl NewPodcast {
pub fn from_url(uri: &str, parent: &Source) -> Result<NewPodcast> {
let chan = Channel::from_url(uri)?;
let foo = ::feedparser::parse_podcast(&chan, parent.id())?;
Ok(foo)