Debian Codegraph @

podcasts-data/src/models/new_source.rs

raw symbols SCIP
1 // new_source.rs
2 //
3 // Copyright 2017 Jordan Petridis <[email protected]>
4 //
5 // This program is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 //
18 // SPDX-License-Identifier: GPL-3.0-or-later
19
20 use diesel::prelude::*;
21 use url::Url;
22
23 use crate::database::connection;
24 use crate::dbqueries;
25 // use models::{Insert, Update};
26 use crate::errors::DataError;
27 use crate::models::Source;
28 use crate::schema::source;
29
30 #[derive(Insertable)]
31 #[diesel(table_name = source)]
32 #[derive(Debug, Clone, Default, Builder, PartialEq)]
33 #[builder(default)]
34 #[builder(derive(Debug))]
35 #[builder(setter(into))]
36 pub(crate) struct NewSource {
37 uri: String,
38 last_modified: Option<String>,
39 http_etag: Option<String>,
40 }
41
42 impl NewSource {
43 pub(crate) fn new(uri: &Url) -> NewSource {
44 NewSource {
45 uri: uri.to_string(),
46 last_modified: None,
47 http_etag: None,
48 }
49 }
50
51 pub(crate) fn insert_or_ignore(&self) -> Result<(), DataError> {
52 use crate::schema::source::dsl::*;
53 let db = connection();
54 let mut con = db.get()?;
55
56 diesel::insert_or_ignore_into(source)
57 .values(self)
58 .execute(&mut con)
59 .map(|_| ())
60 .map_err(From::from)
61 }
62
63 // Look out for when tryinto lands into stable.
64 pub(crate) fn to_source(&self) -> Result<Source, DataError> {
65 self.insert_or_ignore()?;
66 dbqueries::get_source_from_uri(&self.uri)
67 }
68 }