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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
use crate::errors::*; use serde::{Serialize, Deserialize}; use diesel::prelude::*; use crate::models::*; use std::net; #[derive(Identifiable, Queryable, Associations, Serialize, Deserialize)] #[belongs_to(Subdomain)] #[belongs_to(IpAddr)] #[table_name="subdomain_ipaddrs"] pub struct SubdomainIpAddr { pub id: i32, pub subdomain_id: i32, pub ip_addr_id: i32, } impl Model for SubdomainIpAddr { type ID = (i32, i32); fn to_string(&self) -> String { unimplemented!("SubdomainIpAddr can not be printed") } fn list(db: &Database) -> Result<Vec<Self>> { use crate::schema::subdomain_ipaddrs::dsl::*; let results = subdomain_ipaddrs.load::<Self>(db.db())?; Ok(results) } fn filter(db: &Database, filter: &Filter) -> Result<Vec<Self>> { use crate::schema::subdomain_ipaddrs::dsl::*; let query = subdomain_ipaddrs.filter(filter.sql()); let results = query.load::<Self>(db.db())?; Ok(results) } fn delete(db: &Database, filter: &Filter) -> Result<usize> { use crate::schema::subdomain_ipaddrs::dsl::*; diesel::delete(subdomain_ipaddrs.filter(filter.sql())) .execute(db.db()) .map_err(Error::from) } fn delete_id(db: &Database, my_id: i32) -> Result<usize> { use crate::schema::subdomain_ipaddrs::dsl::*; diesel::delete(subdomain_ipaddrs.filter(id.eq(my_id))) .execute(db.db()) .map_err(Error::from) } fn id(&self) -> i32 { self.id } fn by_id(db: &Database, my_id: i32) -> Result<Self> { use crate::schema::subdomain_ipaddrs::dsl::*; let subdomain_ipaddr = subdomain_ipaddrs.filter(id.eq(my_id)) .first::<Self>(db.db())?; Ok(subdomain_ipaddr) } fn get(db: &Database, query: &Self::ID) -> Result<Self> { use crate::schema::subdomain_ipaddrs::dsl::*; let (my_subdomain_id, my_ip_addr_id) = query; let subdomain_ipaddr = subdomain_ipaddrs.filter(subdomain_id.eq(my_subdomain_id)) .filter(ip_addr_id.eq(my_ip_addr_id)) .first::<Self>(db.db())?; Ok(subdomain_ipaddr) } fn get_opt(db: &Database, query: &Self::ID) -> Result<Option<Self>> { use crate::schema::subdomain_ipaddrs::dsl::*; let (my_subdomain_id, my_ip_addr_id) = query; let subdomain_ipaddr = subdomain_ipaddrs.filter(subdomain_id.eq(my_subdomain_id)) .filter(ip_addr_id.eq(my_ip_addr_id)) .first::<Self>(db.db()) .optional()?; Ok(subdomain_ipaddr) } } pub struct PrintableSubdomainIpAddr { subdomain: String, ipaddr: net::IpAddr, } impl fmt::Display for PrintableSubdomainIpAddr { fn fmt(&self, w: &mut fmt::Formatter) -> fmt::Result { write!(w, "{:?} -> {}", self.subdomain, self.ipaddr) } } impl Printable<PrintableSubdomainIpAddr> for SubdomainIpAddr { fn printable(&self, db: &Database) -> Result<PrintableSubdomainIpAddr> { let subdomain = Subdomain::by_id(db, self.subdomain_id)?; let ipaddr = IpAddr::by_id(db, self.ip_addr_id)?; Ok(PrintableSubdomainIpAddr { subdomain: subdomain.value, ipaddr: ipaddr.value.parse()?, }) } } #[derive(Debug, Clone, Insertable, Serialize, Deserialize)] #[table_name="subdomain_ipaddrs"] pub struct NewSubdomainIpAddr { pub subdomain_id: i32, pub ip_addr_id: i32, } impl Printable<PrintableSubdomainIpAddr> for NewSubdomainIpAddr { fn printable(&self, db: &Database) -> Result<PrintableSubdomainIpAddr> { let subdomain = Subdomain::by_id(db, self.subdomain_id)?; let ipaddr = IpAddr::by_id(db, self.ip_addr_id)?; Ok(PrintableSubdomainIpAddr { subdomain: subdomain.value, ipaddr: ipaddr.value.parse()?, }) } } pub type InsertSubdomainIpAddr = NewSubdomainIpAddr; impl InsertToNew for InsertSubdomainIpAddr { type Target = NewSubdomainIpAddr; #[inline] fn try_into_new(self) -> Result<NewSubdomainIpAddr> { Ok(self) } }