summaryrefslogtreecommitdiffstats
path: root/src/db/models/release_store.rs
blob: ad6c9b296e0ab9a9d891f8a4df5f07ca299ccd76 (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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//
// Copyright (c) 2020-2021 science+computing ag and other contributors
//
// This program and the accompanying materials are made
// available under the terms of the Eclipse Public License 2.0
// which is available at https://www.eclipse.org/legal/epl-2.0/
//
// SPDX-License-Identifier: EPL-2.0
//

use anyhow::Error;
use anyhow::Result;
use diesel::Connection;
use diesel::ExpressionMethods;
use diesel::PgConnection;
use diesel::QueryDsl;
use diesel::RunQueryDsl;

use crate::schema::release_stores;
use crate::schema;

#[derive(Debug, Identifiable, Queryable)]
#[table_name = "release_stores"]
pub struct ReleaseStore {
    pub id: i32,
    pub store_name: String,
}

#[derive(Insertable)]
#[table_name = "release_stores"]
struct NewReleaseStore<'a> {
    pub store_name : &'a str,
}

impl ReleaseStore {
    pub fn create(database_connection: &PgConnection, name: &str) -> Result<ReleaseStore> {
        let new_relstore = NewReleaseStore {
            store_name: name,
        };

        database_connection.transaction::<_, Error, _>(|| {
            diesel::insert_into(schema::release_stores::table)
                .values(&new_relstore)
                .on_conflict_do_nothing()
                .execute(database_connection)?;

            schema::release_stores::table
                .filter(schema::release_stores::store_name.eq(name))
                .first::<ReleaseStore>(database_connection)
                .map_err(Error::from)
        })
    }
}