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

View File

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