/
channel.rs
74 lines (65 loc) · 2.18 KB
/
channel.rs
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
use super::db_id_format;
use chrono::NaiveDateTime;
use diesel;
use diesel::pg::PgConnection;
use diesel::result::QueryResult;
use diesel::sql_types::{BigInt, Bool, Text};
use diesel::RunQueryDsl;
use server::schema::channel::*;
#[derive(Debug, Serialize, Deserialize, QueryableByName)]
#[table_name = "origin_channels"]
pub struct Channel {
#[serde(with = "db_id_format")]
pub id: i64,
#[serde(with = "db_id_format")]
pub origin_id: i64,
#[serde(with = "db_id_format")]
pub owner_id: i64,
pub name: String,
pub created_at: Option<NaiveDateTime>,
pub updated_at: Option<NaiveDateTime>,
}
pub struct CreateChannel {
pub channel: String,
pub owner_id: i64,
pub origin: String,
}
pub struct ListChannels {
pub origin: String,
pub include_sandbox_channels: bool,
}
pub struct GetChannel {
pub origin: String,
pub channel: String,
}
pub struct DeleteChannel {
pub origin: String,
pub channel: String,
}
impl Channel {
pub fn list(channel: ListChannels, conn: &PgConnection) -> QueryResult<Vec<Channel>> {
diesel::sql_query("select * from get_origin_channels_for_origin_v3($1, $2)")
.bind::<Text, _>(channel.origin)
.bind::<Bool, _>(channel.include_sandbox_channels)
.get_results(conn)
}
pub fn get(channel: GetChannel, conn: &PgConnection) -> QueryResult<Channel> {
diesel::sql_query("select * from get_origin_channel_v1($1, $2)")
.bind::<Text, _>(channel.origin)
.bind::<Text, _>(channel.channel)
.get_result(conn)
}
pub fn create(channel: CreateChannel, conn: &PgConnection) -> QueryResult<Channel> {
diesel::sql_query("select * from insert_origin_channel_v2($1, $2, $3)")
.bind::<Text, _>(channel.origin)
.bind::<BigInt, _>(channel.owner_id)
.bind::<Text, _>(channel.channel)
.get_result(conn)
}
pub fn delete(channel: DeleteChannel, conn: &PgConnection) -> QueryResult<usize> {
diesel::sql_query("select * from delete_origin_channel_v2($1, $2)")
.bind::<Text, _>(channel.channel)
.bind::<Text, _>(channel.origin)
.execute(conn)
}
}