Switch spaces to tabs
This commit is contained in:
parent
07aca5293e
commit
4261141c3a
17 changed files with 560 additions and 557 deletions
|
@ -5,54 +5,54 @@ use serde::{Serialize, Serializer};
|
|||
|
||||
#[derive(Debug)]
|
||||
pub enum DatabaseError {
|
||||
DeleteError,
|
||||
InsertError(diesel::result::Error),
|
||||
InsertGetError,
|
||||
NotFound,
|
||||
Protected,
|
||||
UpdateError(diesel::result::Error),
|
||||
DeleteError,
|
||||
InsertError(diesel::result::Error),
|
||||
InsertGetError,
|
||||
NotFound,
|
||||
Protected,
|
||||
UpdateError(diesel::result::Error),
|
||||
}
|
||||
|
||||
impl DatabaseError {
|
||||
fn get_code(&self) -> StatusCode {
|
||||
match self {
|
||||
DatabaseError::NotFound => StatusCode::NOT_FOUND,
|
||||
DatabaseError::Protected => StatusCode::FORBIDDEN,
|
||||
_ => StatusCode::INTERNAL_SERVER_ERROR,
|
||||
}
|
||||
}
|
||||
fn get_code(&self) -> StatusCode {
|
||||
match self {
|
||||
DatabaseError::NotFound => StatusCode::NOT_FOUND,
|
||||
DatabaseError::Protected => StatusCode::FORBIDDEN,
|
||||
_ => StatusCode::INTERNAL_SERVER_ERROR,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Serialize for DatabaseError {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
let mut s = serializer.serialize_struct("error", 3)?;
|
||||
s.serialize_field("type", "database-error")?;
|
||||
s.serialize_field("code", &self.get_code().as_u16())?;
|
||||
s.serialize_field("description", &String::from(self))?;
|
||||
s.end()
|
||||
}
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
let mut s = serializer.serialize_struct("error", 3)?;
|
||||
s.serialize_field("type", "database-error")?;
|
||||
s.serialize_field("code", &self.get_code().as_u16())?;
|
||||
s.serialize_field("description", &String::from(self))?;
|
||||
s.end()
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&DatabaseError> for String {
|
||||
fn from(err: &DatabaseError) -> Self {
|
||||
match err {
|
||||
DatabaseError::InsertError(_) => String::from("error on inserting into database"),
|
||||
DatabaseError::InsertGetError => {
|
||||
String::from("error on retrieving new entry from database (your entry was saved)")
|
||||
}
|
||||
DatabaseError::NotFound => String::from("model was not found in database"),
|
||||
DatabaseError::DeleteError => String::from("error on deleting from database"),
|
||||
DatabaseError::Protected => String::from("model is protected"),
|
||||
DatabaseError::UpdateError(_) => String::from("error on updating the model"),
|
||||
}
|
||||
}
|
||||
fn from(err: &DatabaseError) -> Self {
|
||||
match err {
|
||||
DatabaseError::InsertError(_) => String::from("error on inserting into database"),
|
||||
DatabaseError::InsertGetError => {
|
||||
String::from("error on retrieving new entry from database (your entry was saved)")
|
||||
}
|
||||
DatabaseError::NotFound => String::from("model was not found in database"),
|
||||
DatabaseError::DeleteError => String::from("error on deleting from database"),
|
||||
DatabaseError::Protected => String::from("model is protected"),
|
||||
DatabaseError::UpdateError(_) => String::from("error on updating the model"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<DatabaseError> for HttpResponse {
|
||||
fn from(err: DatabaseError) -> Self {
|
||||
HttpResponse::build(err.get_code()).json(err)
|
||||
}
|
||||
fn from(err: DatabaseError) -> Self {
|
||||
HttpResponse::build(err.get_code()).json(err)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,64 +12,64 @@ use std::io::Write;
|
|||
#[derive(Debug, Serialize, Deserialize, AsExpression, FromSqlRow, PartialEq, Clone)]
|
||||
#[sql_type = "Binary"]
|
||||
pub struct Period {
|
||||
#[serde(with = "period_format")]
|
||||
pub start: NaiveTime,
|
||||
#[serde(with = "period_format")]
|
||||
pub end: NaiveTime,
|
||||
#[serde(with = "period_format")]
|
||||
pub start: NaiveTime,
|
||||
#[serde(with = "period_format")]
|
||||
pub end: NaiveTime,
|
||||
}
|
||||
|
||||
mod period_format {
|
||||
use chrono::NaiveTime;
|
||||
use serde::{self, Deserialize, Deserializer, Serializer};
|
||||
use chrono::NaiveTime;
|
||||
use serde::{self, Deserialize, Deserializer, Serializer};
|
||||
|
||||
const FORMAT: &str = "%H:%M";
|
||||
const FORMAT: &str = "%H:%M";
|
||||
|
||||
pub fn serialize<S>(time: &NaiveTime, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
let s = format!("{}", time.format(FORMAT));
|
||||
serializer.serialize_str(&s)
|
||||
}
|
||||
pub fn serialize<S>(time: &NaiveTime, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
let s = format!("{}", time.format(FORMAT));
|
||||
serializer.serialize_str(&s)
|
||||
}
|
||||
|
||||
pub fn deserialize<'de, D>(deserializer: D) -> Result<NaiveTime, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
let s = String::deserialize(deserializer)?;
|
||||
NaiveTime::parse_from_str(&s, FORMAT).map_err(serde::de::Error::custom)
|
||||
}
|
||||
pub fn deserialize<'de, D>(deserializer: D) -> Result<NaiveTime, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
let s = String::deserialize(deserializer)?;
|
||||
NaiveTime::parse_from_str(&s, FORMAT).map_err(serde::de::Error::custom)
|
||||
}
|
||||
}
|
||||
|
||||
impl ToSql<Binary, Sqlite> for Periods {
|
||||
fn to_sql<W: Write>(&self, out: &mut Output<W, Sqlite>) -> serialize::Result {
|
||||
for period in self.0.iter() {
|
||||
out.write_all(&[
|
||||
period.start.hour() as u8,
|
||||
period.start.minute() as u8,
|
||||
period.end.hour() as u8,
|
||||
period.end.minute() as u8,
|
||||
])?;
|
||||
}
|
||||
Ok(IsNull::No)
|
||||
}
|
||||
fn to_sql<W: Write>(&self, out: &mut Output<W, Sqlite>) -> serialize::Result {
|
||||
for period in self.0.iter() {
|
||||
out.write_all(&[
|
||||
period.start.hour() as u8,
|
||||
period.start.minute() as u8,
|
||||
period.end.hour() as u8,
|
||||
period.end.minute() as u8,
|
||||
])?;
|
||||
}
|
||||
Ok(IsNull::No)
|
||||
}
|
||||
}
|
||||
|
||||
impl FromSql<Binary, Sqlite> for Periods {
|
||||
fn from_sql(bytes: Option<&<Sqlite as Backend>::RawValue>) -> deserialize::Result<Self> {
|
||||
let blob = bytes.unwrap().read_blob();
|
||||
fn from_sql(bytes: Option<&<Sqlite as Backend>::RawValue>) -> deserialize::Result<Self> {
|
||||
let blob = bytes.unwrap().read_blob();
|
||||
|
||||
let mut vec = Vec::new();
|
||||
for i in (3..blob.len()).step_by(4) {
|
||||
let start_val_h: u32 = blob[i - 3] as u32;
|
||||
let start_val_m: u32 = blob[i - 2] as u32;
|
||||
let end_val_h: u32 = blob[i - 1] as u32;
|
||||
let end_val_m: u32 = blob[i] as u32;
|
||||
vec.push(Period {
|
||||
start: NaiveTime::from_hms(start_val_h, start_val_m, 0),
|
||||
end: NaiveTime::from_hms(end_val_h, end_val_m, 0),
|
||||
});
|
||||
}
|
||||
Ok(Periods(vec))
|
||||
}
|
||||
let mut vec = Vec::new();
|
||||
for i in (3..blob.len()).step_by(4) {
|
||||
let start_val_h: u32 = blob[i - 3] as u32;
|
||||
let start_val_m: u32 = blob[i - 2] as u32;
|
||||
let end_val_h: u32 = blob[i - 1] as u32;
|
||||
let end_val_m: u32 = blob[i] as u32;
|
||||
vec.push(Period {
|
||||
start: NaiveTime::from_hms(start_val_h, start_val_m, 0),
|
||||
end: NaiveTime::from_hms(end_val_h, end_val_m, 0),
|
||||
});
|
||||
}
|
||||
Ok(Periods(vec))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,27 +7,27 @@ use crate::types::EmgauwaUid;
|
|||
|
||||
#[derive(Debug, Serialize, Identifiable, Queryable)]
|
||||
pub struct Relay {
|
||||
#[serde(skip)]
|
||||
pub id: i32,
|
||||
// TODO
|
||||
#[serde(skip)]
|
||||
pub id: i32,
|
||||
// TODO
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Identifiable, Queryable, Clone)]
|
||||
pub struct Schedule {
|
||||
#[serde(skip)]
|
||||
pub id: i32,
|
||||
#[serde(rename(serialize = "id"))]
|
||||
pub uid: EmgauwaUid,
|
||||
pub name: String,
|
||||
pub periods: Periods,
|
||||
#[serde(skip)]
|
||||
pub id: i32,
|
||||
#[serde(rename(serialize = "id"))]
|
||||
pub uid: EmgauwaUid,
|
||||
pub name: String,
|
||||
pub periods: Periods,
|
||||
}
|
||||
|
||||
#[derive(Insertable)]
|
||||
#[table_name = "schedules"]
|
||||
pub struct NewSchedule<'a> {
|
||||
pub uid: &'a EmgauwaUid,
|
||||
pub name: &'a str,
|
||||
pub periods: &'a Periods,
|
||||
pub uid: &'a EmgauwaUid,
|
||||
pub name: &'a str,
|
||||
pub periods: &'a Periods,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, AsExpression, FromSqlRow, PartialEq, Clone)]
|
||||
|
@ -36,14 +36,14 @@ pub struct Periods(pub(crate) Vec<Period>);
|
|||
|
||||
#[derive(Debug, Serialize, Identifiable, Queryable, Clone)]
|
||||
pub struct Tag {
|
||||
pub id: i32,
|
||||
pub tag: String,
|
||||
pub id: i32,
|
||||
pub tag: String,
|
||||
}
|
||||
|
||||
#[derive(Insertable)]
|
||||
#[table_name = "tags"]
|
||||
pub struct NewTag<'a> {
|
||||
pub tag: &'a str,
|
||||
pub tag: &'a str,
|
||||
}
|
||||
|
||||
#[derive(Queryable, Associations, Identifiable)]
|
||||
|
@ -52,16 +52,16 @@ pub struct NewTag<'a> {
|
|||
#[belongs_to(Tag)]
|
||||
#[table_name = "junction_tag"]
|
||||
pub struct JunctionTag {
|
||||
pub id: i32,
|
||||
pub tag_id: i32,
|
||||
pub relay_id: Option<i32>,
|
||||
pub schedule_id: Option<i32>,
|
||||
pub id: i32,
|
||||
pub tag_id: i32,
|
||||
pub relay_id: Option<i32>,
|
||||
pub schedule_id: Option<i32>,
|
||||
}
|
||||
|
||||
#[derive(Insertable)]
|
||||
#[table_name = "junction_tag"]
|
||||
pub struct NewJunctionTag {
|
||||
pub tag_id: i32,
|
||||
pub relay_id: Option<i32>,
|
||||
pub schedule_id: Option<i32>,
|
||||
pub tag_id: i32,
|
||||
pub relay_id: Option<i32>,
|
||||
pub schedule_id: Option<i32>,
|
||||
}
|
||||
|
|
|
@ -13,129 +13,129 @@ use crate::db::tag::{create_junction_tag, create_tag};
|
|||
use crate::db::{get_connection, schema};
|
||||
|
||||
pub fn get_schedule_tags(schedule: &Schedule) -> Vec<String> {
|
||||
let connection = get_connection();
|
||||
JunctionTag::belonging_to(schedule)
|
||||
.inner_join(schema::tags::dsl::tags)
|
||||
.select(schema::tags::tag)
|
||||
.load::<String>(&connection)
|
||||
.expect("Error loading tags")
|
||||
let connection = get_connection();
|
||||
JunctionTag::belonging_to(schedule)
|
||||
.inner_join(schema::tags::dsl::tags)
|
||||
.select(schema::tags::tag)
|
||||
.load::<String>(&connection)
|
||||
.expect("Error loading tags")
|
||||
}
|
||||
|
||||
pub fn get_schedules() -> Vec<Schedule> {
|
||||
let connection = get_connection();
|
||||
schedules
|
||||
.load::<Schedule>(&connection)
|
||||
.expect("Error loading schedules")
|
||||
let connection = get_connection();
|
||||
schedules
|
||||
.load::<Schedule>(&connection)
|
||||
.expect("Error loading schedules")
|
||||
}
|
||||
|
||||
pub fn get_schedule_by_uid(filter_uid: EmgauwaUid) -> Result<Schedule, DatabaseError> {
|
||||
let connection = get_connection();
|
||||
let result = schedules
|
||||
.filter(schema::schedules::uid.eq(filter_uid))
|
||||
.first::<Schedule>(&connection)
|
||||
.or(Err(DatabaseError::NotFound))?;
|
||||
let connection = get_connection();
|
||||
let result = schedules
|
||||
.filter(schema::schedules::uid.eq(filter_uid))
|
||||
.first::<Schedule>(&connection)
|
||||
.or(Err(DatabaseError::NotFound))?;
|
||||
|
||||
Ok(result)
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
pub fn get_schedules_by_tag(tag: &Tag) -> Vec<Schedule> {
|
||||
let connection = get_connection();
|
||||
JunctionTag::belonging_to(tag)
|
||||
.inner_join(schedules)
|
||||
.select(schema::schedules::all_columns)
|
||||
.load::<Schedule>(&connection)
|
||||
.expect("Error loading tags")
|
||||
let connection = get_connection();
|
||||
JunctionTag::belonging_to(tag)
|
||||
.inner_join(schedules)
|
||||
.select(schema::schedules::all_columns)
|
||||
.load::<Schedule>(&connection)
|
||||
.expect("Error loading tags")
|
||||
}
|
||||
|
||||
pub fn delete_schedule_by_uid(filter_uid: EmgauwaUid) -> Result<(), DatabaseError> {
|
||||
let filter_uid = match filter_uid {
|
||||
EmgauwaUid::Off => Err(DatabaseError::Protected),
|
||||
EmgauwaUid::On => Err(DatabaseError::Protected),
|
||||
EmgauwaUid::Any(_) => Ok(filter_uid),
|
||||
}?;
|
||||
let filter_uid = match filter_uid {
|
||||
EmgauwaUid::Off => Err(DatabaseError::Protected),
|
||||
EmgauwaUid::On => Err(DatabaseError::Protected),
|
||||
EmgauwaUid::Any(_) => Ok(filter_uid),
|
||||
}?;
|
||||
|
||||
let connection = get_connection();
|
||||
match diesel::delete(schedules.filter(schema::schedules::uid.eq(filter_uid)))
|
||||
.execute(&connection)
|
||||
{
|
||||
Ok(rows) => {
|
||||
if rows != 0 {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(DatabaseError::DeleteError)
|
||||
}
|
||||
}
|
||||
Err(_) => Err(DatabaseError::DeleteError),
|
||||
}
|
||||
let connection = get_connection();
|
||||
match diesel::delete(schedules.filter(schema::schedules::uid.eq(filter_uid)))
|
||||
.execute(&connection)
|
||||
{
|
||||
Ok(rows) => {
|
||||
if rows != 0 {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(DatabaseError::DeleteError)
|
||||
}
|
||||
}
|
||||
Err(_) => Err(DatabaseError::DeleteError),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn create_schedule(new_name: &str, new_periods: &Periods) -> Result<Schedule, DatabaseError> {
|
||||
let connection = get_connection();
|
||||
let connection = get_connection();
|
||||
|
||||
let new_schedule = NewSchedule {
|
||||
uid: &EmgauwaUid::default(),
|
||||
name: new_name,
|
||||
periods: new_periods,
|
||||
};
|
||||
let new_schedule = NewSchedule {
|
||||
uid: &EmgauwaUid::default(),
|
||||
name: new_name,
|
||||
periods: new_periods,
|
||||
};
|
||||
|
||||
diesel::insert_into(schedules)
|
||||
.values(&new_schedule)
|
||||
.execute(&connection)
|
||||
.map_err(DatabaseError::InsertError)?;
|
||||
diesel::insert_into(schedules)
|
||||
.values(&new_schedule)
|
||||
.execute(&connection)
|
||||
.map_err(DatabaseError::InsertError)?;
|
||||
|
||||
let result = schedules
|
||||
.find(sql("last_insert_rowid()"))
|
||||
.get_result::<Schedule>(&connection)
|
||||
.or(Err(DatabaseError::InsertGetError))?;
|
||||
let result = schedules
|
||||
.find(sql("last_insert_rowid()"))
|
||||
.get_result::<Schedule>(&connection)
|
||||
.or(Err(DatabaseError::InsertGetError))?;
|
||||
|
||||
Ok(result)
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
pub fn update_schedule(
|
||||
schedule: &Schedule,
|
||||
new_name: &str,
|
||||
new_periods: &Periods,
|
||||
schedule: &Schedule,
|
||||
new_name: &str,
|
||||
new_periods: &Periods,
|
||||
) -> Result<Schedule, DatabaseError> {
|
||||
let connection = get_connection();
|
||||
let connection = get_connection();
|
||||
|
||||
let new_periods = match schedule.uid {
|
||||
EmgauwaUid::Off | EmgauwaUid::On => schedule.periods.borrow(),
|
||||
EmgauwaUid::Any(_) => new_periods,
|
||||
};
|
||||
let new_periods = match schedule.uid {
|
||||
EmgauwaUid::Off | EmgauwaUid::On => schedule.periods.borrow(),
|
||||
EmgauwaUid::Any(_) => new_periods,
|
||||
};
|
||||
|
||||
diesel::update(schedule)
|
||||
.set((
|
||||
schema::schedules::name.eq(new_name),
|
||||
schema::schedules::periods.eq(new_periods),
|
||||
))
|
||||
.execute(&connection)
|
||||
.map_err(DatabaseError::UpdateError)?;
|
||||
diesel::update(schedule)
|
||||
.set((
|
||||
schema::schedules::name.eq(new_name),
|
||||
schema::schedules::periods.eq(new_periods),
|
||||
))
|
||||
.execute(&connection)
|
||||
.map_err(DatabaseError::UpdateError)?;
|
||||
|
||||
get_schedule_by_uid(schedule.uid.clone())
|
||||
get_schedule_by_uid(schedule.uid.clone())
|
||||
}
|
||||
|
||||
pub fn set_schedule_tags(schedule: &Schedule, new_tags: &[String]) -> Result<(), DatabaseError> {
|
||||
let connection = get_connection();
|
||||
diesel::delete(junction_tag.filter(schema::junction_tag::schedule_id.eq(schedule.id)))
|
||||
.execute(&connection)
|
||||
.or(Err(DatabaseError::DeleteError))?;
|
||||
let connection = get_connection();
|
||||
diesel::delete(junction_tag.filter(schema::junction_tag::schedule_id.eq(schedule.id)))
|
||||
.execute(&connection)
|
||||
.or(Err(DatabaseError::DeleteError))?;
|
||||
|
||||
let mut database_tags: Vec<Tag> = tags
|
||||
.filter(schema::tags::tag.eq_any(new_tags))
|
||||
.load::<Tag>(&connection)
|
||||
.expect("Error loading tags");
|
||||
let mut database_tags: Vec<Tag> = tags
|
||||
.filter(schema::tags::tag.eq_any(new_tags))
|
||||
.load::<Tag>(&connection)
|
||||
.expect("Error loading tags");
|
||||
|
||||
// create missing tags
|
||||
for new_tag in new_tags {
|
||||
if !database_tags.iter().any(|tab_db| tab_db.tag.eq(new_tag)) {
|
||||
database_tags.push(create_tag(new_tag).expect("Error inserting tag"));
|
||||
}
|
||||
}
|
||||
// create missing tags
|
||||
for new_tag in new_tags {
|
||||
if !database_tags.iter().any(|tab_db| tab_db.tag.eq(new_tag)) {
|
||||
database_tags.push(create_tag(new_tag).expect("Error inserting tag"));
|
||||
}
|
||||
}
|
||||
|
||||
for database_tag in database_tags {
|
||||
create_junction_tag(database_tag, None, Some(schedule))
|
||||
.expect("Error saving junction between tag and schedule");
|
||||
}
|
||||
for database_tag in database_tags {
|
||||
create_junction_tag(database_tag, None, Some(schedule))
|
||||
.expect("Error saving junction between tag and schedule");
|
||||
}
|
||||
|
||||
Ok(())
|
||||
Ok(())
|
||||
}
|
||||
|
|
114
src/db/schema.rs
114
src/db/schema.rs
|
@ -1,74 +1,74 @@
|
|||
table! {
|
||||
controllers (id) {
|
||||
id -> Integer,
|
||||
uid -> Text,
|
||||
name -> Text,
|
||||
ip -> Nullable<Text>,
|
||||
port -> Nullable<Integer>,
|
||||
relay_count -> Nullable<Integer>,
|
||||
active -> Bool,
|
||||
}
|
||||
controllers (id) {
|
||||
id -> Integer,
|
||||
uid -> Text,
|
||||
name -> Text,
|
||||
ip -> Nullable<Text>,
|
||||
port -> Nullable<Integer>,
|
||||
relay_count -> Nullable<Integer>,
|
||||
active -> Bool,
|
||||
}
|
||||
}
|
||||
|
||||
table! {
|
||||
junction_relay_schedule (id) {
|
||||
id -> Integer,
|
||||
weekday -> SmallInt,
|
||||
relay_id -> Nullable<Integer>,
|
||||
schedule_id -> Nullable<Integer>,
|
||||
}
|
||||
junction_relay_schedule (id) {
|
||||
id -> Integer,
|
||||
weekday -> SmallInt,
|
||||
relay_id -> Nullable<Integer>,
|
||||
schedule_id -> Nullable<Integer>,
|
||||
}
|
||||
}
|
||||
|
||||
table! {
|
||||
junction_tag (id) {
|
||||
id -> Integer,
|
||||
tag_id -> Integer,
|
||||
relay_id -> Nullable<Integer>,
|
||||
schedule_id -> Nullable<Integer>,
|
||||
}
|
||||
junction_tag (id) {
|
||||
id -> Integer,
|
||||
tag_id -> Integer,
|
||||
relay_id -> Nullable<Integer>,
|
||||
schedule_id -> Nullable<Integer>,
|
||||
}
|
||||
}
|
||||
|
||||
table! {
|
||||
macro_actions (id) {
|
||||
id -> Integer,
|
||||
macro_id -> Integer,
|
||||
relay_id -> Integer,
|
||||
schedule_id -> Integer,
|
||||
weekday -> SmallInt,
|
||||
}
|
||||
macro_actions (id) {
|
||||
id -> Integer,
|
||||
macro_id -> Integer,
|
||||
relay_id -> Integer,
|
||||
schedule_id -> Integer,
|
||||
weekday -> SmallInt,
|
||||
}
|
||||
}
|
||||
|
||||
table! {
|
||||
macros (id) {
|
||||
id -> Integer,
|
||||
uid -> Text,
|
||||
name -> Text,
|
||||
}
|
||||
macros (id) {
|
||||
id -> Integer,
|
||||
uid -> Text,
|
||||
name -> Text,
|
||||
}
|
||||
}
|
||||
|
||||
table! {
|
||||
relays (id) {
|
||||
id -> Integer,
|
||||
name -> Text,
|
||||
number -> Integer,
|
||||
controller_id -> Integer,
|
||||
}
|
||||
relays (id) {
|
||||
id -> Integer,
|
||||
name -> Text,
|
||||
number -> Integer,
|
||||
controller_id -> Integer,
|
||||
}
|
||||
}
|
||||
|
||||
table! {
|
||||
schedules (id) {
|
||||
id -> Integer,
|
||||
uid -> Binary,
|
||||
name -> Text,
|
||||
periods -> Binary,
|
||||
}
|
||||
schedules (id) {
|
||||
id -> Integer,
|
||||
uid -> Binary,
|
||||
name -> Text,
|
||||
periods -> Binary,
|
||||
}
|
||||
}
|
||||
|
||||
table! {
|
||||
tags (id) {
|
||||
id -> Integer,
|
||||
tag -> Text,
|
||||
}
|
||||
tags (id) {
|
||||
id -> Integer,
|
||||
tag -> Text,
|
||||
}
|
||||
}
|
||||
|
||||
joinable!(junction_relay_schedule -> relays (relay_id));
|
||||
|
@ -82,12 +82,12 @@ joinable!(macro_actions -> schedules (schedule_id));
|
|||
joinable!(relays -> controllers (controller_id));
|
||||
|
||||
allow_tables_to_appear_in_same_query!(
|
||||
controllers,
|
||||
junction_relay_schedule,
|
||||
junction_tag,
|
||||
macro_actions,
|
||||
macros,
|
||||
relays,
|
||||
schedules,
|
||||
tags,
|
||||
controllers,
|
||||
junction_relay_schedule,
|
||||
junction_tag,
|
||||
macro_actions,
|
||||
macros,
|
||||
relays,
|
||||
schedules,
|
||||
tags,
|
||||
);
|
||||
|
|
|
@ -8,56 +8,56 @@ use crate::db::schema::tags::dsl::tags;
|
|||
use crate::db::{get_connection, schema};
|
||||
|
||||
pub fn create_tag(new_tag: &str) -> Result<Tag, DatabaseError> {
|
||||
let connection = get_connection();
|
||||
let connection = get_connection();
|
||||
|
||||
let new_tag = NewTag { tag: new_tag };
|
||||
let new_tag = NewTag { tag: new_tag };
|
||||
|
||||
diesel::insert_into(tags)
|
||||
.values(&new_tag)
|
||||
.execute(&connection)
|
||||
.map_err(DatabaseError::InsertError)?;
|
||||
diesel::insert_into(tags)
|
||||
.values(&new_tag)
|
||||
.execute(&connection)
|
||||
.map_err(DatabaseError::InsertError)?;
|
||||
|
||||
let result = tags
|
||||
.find(sql("last_insert_rowid()"))
|
||||
.get_result::<Tag>(&connection)
|
||||
.or(Err(DatabaseError::InsertGetError))?;
|
||||
let result = tags
|
||||
.find(sql("last_insert_rowid()"))
|
||||
.get_result::<Tag>(&connection)
|
||||
.or(Err(DatabaseError::InsertGetError))?;
|
||||
|
||||
Ok(result)
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
pub fn get_tag(target_tag: &str) -> Result<Tag, DatabaseError> {
|
||||
let connection = get_connection();
|
||||
let connection = get_connection();
|
||||
|
||||
let result = tags
|
||||
.filter(schema::tags::tag.eq(target_tag))
|
||||
.first::<Tag>(&connection)
|
||||
.or(Err(DatabaseError::NotFound))?;
|
||||
let result = tags
|
||||
.filter(schema::tags::tag.eq(target_tag))
|
||||
.first::<Tag>(&connection)
|
||||
.or(Err(DatabaseError::NotFound))?;
|
||||
|
||||
Ok(result)
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
pub fn create_junction_tag(
|
||||
target_tag: Tag,
|
||||
target_relay: Option<&Relay>,
|
||||
target_schedule: Option<&Schedule>,
|
||||
target_tag: Tag,
|
||||
target_relay: Option<&Relay>,
|
||||
target_schedule: Option<&Schedule>,
|
||||
) -> Result<JunctionTag, DatabaseError> {
|
||||
let connection = get_connection();
|
||||
let connection = get_connection();
|
||||
|
||||
let new_junction_tag = NewJunctionTag {
|
||||
relay_id: target_relay.map(|r| r.id),
|
||||
schedule_id: target_schedule.map(|s| s.id),
|
||||
tag_id: target_tag.id,
|
||||
};
|
||||
let new_junction_tag = NewJunctionTag {
|
||||
relay_id: target_relay.map(|r| r.id),
|
||||
schedule_id: target_schedule.map(|s| s.id),
|
||||
tag_id: target_tag.id,
|
||||
};
|
||||
|
||||
diesel::insert_into(junction_tag)
|
||||
.values(&new_junction_tag)
|
||||
.execute(&connection)
|
||||
.map_err(DatabaseError::InsertError)?;
|
||||
diesel::insert_into(junction_tag)
|
||||
.values(&new_junction_tag)
|
||||
.execute(&connection)
|
||||
.map_err(DatabaseError::InsertError)?;
|
||||
|
||||
let result = junction_tag
|
||||
.find(sql("last_insert_rowid()"))
|
||||
.get_result::<JunctionTag>(&connection)
|
||||
.or(Err(DatabaseError::InsertGetError))?;
|
||||
let result = junction_tag
|
||||
.find(sql("last_insert_rowid()"))
|
||||
.get_result::<JunctionTag>(&connection)
|
||||
.or(Err(DatabaseError::InsertGetError))?;
|
||||
|
||||
Ok(result)
|
||||
Ok(result)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue