blob: ab3019242c8d85b811439fa75b12da0c9b1ec176 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
use anyhow::Error;
use anyhow::Result;
use diesel::PgConnection;
use diesel::prelude::*;
use crate::schema::endpoints::*;
use crate::schema::endpoints;
#[derive(Queryable)]
pub struct Endpoint {
pub id: i32,
pub name: String,
}
#[derive(Insertable)]
#[table_name="endpoints"]
struct NewEndpoint<'a> {
pub name: &'a str,
}
impl Endpoint {
pub fn create_or_fetch(database_connection: &PgConnection, ep_name: &str) -> Result<Endpoint> {
let new_ep = NewEndpoint { name: ep_name };
diesel::insert_into(endpoints::table)
.values(&new_ep)
.on_conflict_do_nothing()
.execute(database_connection)?;
dsl::endpoints
.filter(name.eq(ep_name))
.first::<Endpoint>(database_connection)
.map_err(Error::from)
}
}
|