Add function and prefer .iter()

This commit is contained in:
Tobias Reisinger 2022-07-17 20:19:36 +02:00
parent 53c6fcd917
commit e3adc35221
12 changed files with 117 additions and 67 deletions

View file

@ -6,8 +6,8 @@ use dotenv::dotenv;
pub mod errors; pub mod errors;
pub mod models; pub mod models;
pub mod schema;
pub mod schedules; pub mod schedules;
pub mod schema;
pub mod tag; pub mod tag;
mod model_utils; mod model_utils;

View file

@ -1,5 +1,5 @@
use actix_web::HttpResponse;
use actix_web::http::StatusCode; use actix_web::http::StatusCode;
use actix_web::HttpResponse;
use serde::ser::SerializeStruct; use serde::ser::SerializeStruct;
use serde::{Serialize, Serializer}; use serde::{Serialize, Serializer};
@ -18,7 +18,7 @@ impl DatabaseError {
match self { match self {
DatabaseError::NotFound => StatusCode::NOT_FOUND, DatabaseError::NotFound => StatusCode::NOT_FOUND,
DatabaseError::Protected => StatusCode::FORBIDDEN, DatabaseError::Protected => StatusCode::FORBIDDEN,
_ => StatusCode::INTERNAL_SERVER_ERROR _ => StatusCode::INTERNAL_SERVER_ERROR,
} }
} }
} }

View file

@ -1,3 +1,4 @@
use crate::db::models::Periods;
use chrono::{NaiveTime, Timelike}; use chrono::{NaiveTime, Timelike};
use diesel::backend::Backend; use diesel::backend::Backend;
use diesel::deserialize::FromSql; use diesel::deserialize::FromSql;
@ -7,7 +8,6 @@ use diesel::sqlite::Sqlite;
use diesel::{deserialize, serialize}; use diesel::{deserialize, serialize};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::io::Write; use std::io::Write;
use crate::db::models::Periods;
#[derive(Debug, Serialize, Deserialize, AsExpression, FromSqlRow, PartialEq, Clone)] #[derive(Debug, Serialize, Deserialize, AsExpression, FromSqlRow, PartialEq, Clone)]
#[sql_type = "Binary"] #[sql_type = "Binary"]

View file

@ -1,6 +1,6 @@
use crate::db::model_utils::Period;
use diesel::sql_types::Binary; use diesel::sql_types::Binary;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::db::model_utils::Period;
use super::schema::*; use super::schema::*;
use crate::types::EmgauwaUid; use crate::types::EmgauwaUid;
@ -12,7 +12,7 @@ pub struct Relay {
// TODO // TODO
} }
#[derive(Debug, Serialize, Identifiable, Queryable)] #[derive(Debug, Serialize, Identifiable, Queryable, Clone)]
pub struct Schedule { pub struct Schedule {
#[serde(skip)] #[serde(skip)]
pub id: i32, pub id: i32,

View file

@ -1,16 +1,16 @@
use std::borrow::Borrow;
use diesel::dsl::sql; use diesel::dsl::sql;
use diesel::prelude::*; use diesel::prelude::*;
use std::borrow::Borrow;
use crate::types::EmgauwaUid; use crate::types::EmgauwaUid;
use crate::db::errors::DatabaseError; use crate::db::errors::DatabaseError;
use crate::db::{get_connection, schema};
use crate::db::models::*; use crate::db::models::*;
use crate::db::schema::tags::dsl::tags;
use crate::db::schema::junction_tag::dsl::junction_tag; use crate::db::schema::junction_tag::dsl::junction_tag;
use crate::db::schema::schedules::dsl::schedules; use crate::db::schema::schedules::dsl::schedules;
use crate::db::schema::tags::dsl::tags;
use crate::db::tag::{create_junction_tag, create_tag}; use crate::db::tag::{create_junction_tag, create_tag};
use crate::db::{get_connection, schema};
pub fn get_schedule_tags(schedule: &Schedule) -> Vec<String> { pub fn get_schedule_tags(schedule: &Schedule) -> Vec<String> {
let connection = get_connection(); let connection = get_connection();
@ -51,11 +51,13 @@ pub fn delete_schedule_by_uid(filter_uid: EmgauwaUid) -> Result<(), DatabaseErro
let filter_uid = match filter_uid { let filter_uid = match filter_uid {
EmgauwaUid::Off => Err(DatabaseError::Protected), EmgauwaUid::Off => Err(DatabaseError::Protected),
EmgauwaUid::On => Err(DatabaseError::Protected), EmgauwaUid::On => Err(DatabaseError::Protected),
EmgauwaUid::Any(_) => Ok(filter_uid) EmgauwaUid::Any(_) => Ok(filter_uid),
}?; }?;
let connection = get_connection(); let connection = get_connection();
match diesel::delete(schedules.filter(schema::schedules::uid.eq(filter_uid))).execute(&connection) { match diesel::delete(schedules.filter(schema::schedules::uid.eq(filter_uid)))
.execute(&connection)
{
Ok(rows) => { Ok(rows) => {
if rows != 0 { if rows != 0 {
Ok(()) Ok(())
@ -89,7 +91,11 @@ pub fn create_schedule(new_name: &str, new_periods: &Periods) -> Result<Schedule
Ok(result) Ok(result)
} }
pub fn update_schedule(schedule: &Schedule, new_name: &str, new_periods: &Periods) -> Result<Schedule, DatabaseError> { pub fn update_schedule(
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 { let new_periods = match schedule.uid {
@ -114,18 +120,20 @@ pub fn set_schedule_tags(schedule: &Schedule, new_tags: &[String]) -> Result<(),
.execute(&connection) .execute(&connection)
.or(Err(DatabaseError::DeleteError))?; .or(Err(DatabaseError::DeleteError))?;
let mut database_tags: Vec<Tag> = tags.filter(schema::tags::tag.eq_any(new_tags)) let mut database_tags: Vec<Tag> = tags
.filter(schema::tags::tag.eq_any(new_tags))
.load::<Tag>(&connection) .load::<Tag>(&connection)
.expect("Error loading tags"); .expect("Error loading tags");
let mut database_tags_iter = database_tags.clone().into_iter().map(|tag_db| tag_db.tag); let database_tags_str: Vec<String> = database_tags
.iter()
.map(|tag_db| tag_db.tag.clone())
.collect();
// create missing tags // create missing tags
for new_tag in new_tags { for new_tag in new_tags {
if !database_tags_iter.any(|t| t.eq(new_tag)) { if !database_tags_str.contains(new_tag) {
database_tags.push( database_tags.push(create_tag(new_tag).expect("Error inserting tag"));
create_tag(new_tag).expect("Error inserting tag")
);
} }
} }

View file

@ -1,20 +1,16 @@
use diesel::dsl::sql; use diesel::dsl::sql;
use diesel::prelude::*; use diesel::prelude::*;
use crate::db::errors::DatabaseError; use crate::db::errors::DatabaseError;
use crate::db::{get_connection, schema};
use crate::db::models::*; use crate::db::models::*;
use crate::db::schema::tags::dsl::tags;
use crate::db::schema::junction_tag::dsl::junction_tag; use crate::db::schema::junction_tag::dsl::junction_tag;
use crate::db::schema::tags::dsl::tags;
use crate::db::{get_connection, schema};
pub fn create_tag(new_tag: &str) -> Result<Tag, DatabaseError> { pub fn create_tag(new_tag: &str) -> Result<Tag, DatabaseError> {
let connection = get_connection(); let connection = get_connection();
let new_tag = NewTag { let new_tag = NewTag { tag: new_tag };
tag: new_tag,
};
diesel::insert_into(tags) diesel::insert_into(tags)
.values(&new_tag) .values(&new_tag)
@ -40,13 +36,17 @@ pub fn get_tag(target_tag: &str) -> Result<Tag, DatabaseError> {
Ok(result) Ok(result)
} }
pub fn create_junction_tag(target_tag: Tag, target_relay: Option<&Relay>, target_schedule: Option<&Schedule>) -> Result<JunctionTag, DatabaseError> { pub fn create_junction_tag(
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 { let new_junction_tag = NewJunctionTag {
relay_id: target_relay.map(|r| r.id), relay_id: target_relay.map(|r| r.id),
schedule_id: target_schedule.map(|s| s.id), schedule_id: target_schedule.map(|s| s.id),
tag_id: target_tag.id tag_id: target_tag.id,
}; };
diesel::insert_into(junction_tag) diesel::insert_into(junction_tag)

View file

@ -6,7 +6,7 @@ use serde::{Serialize, Serializer};
#[derive(Debug)] #[derive(Debug)]
pub enum HandlerError { pub enum HandlerError {
BadUid, BadUid,
ProtectedSchedule ProtectedSchedule,
} }
impl HandlerError { impl HandlerError {
@ -16,7 +16,6 @@ impl HandlerError {
HandlerError::ProtectedSchedule => StatusCode::FORBIDDEN, HandlerError::ProtectedSchedule => StatusCode::FORBIDDEN,
} }
} }
} }
impl Serialize for HandlerError { impl Serialize for HandlerError {

View file

@ -21,7 +21,7 @@ impl Serialize for EmgauwaJsonPayLoadError {
"description", "description",
&match self { &match self {
EmgauwaJsonPayLoadError::Error(err) => format!("{}", err), EmgauwaJsonPayLoadError::Error(err) => format!("{}", err),
} },
)?; )?;
s.end() s.end()
} }

View file

@ -1,14 +1,16 @@
use crate::db::errors::DatabaseError;
use actix_web::{delete, get, post, put, web, HttpResponse, Responder};
use serde::{Deserialize, Serialize};
use std::borrow::Borrow; use std::borrow::Borrow;
use std::convert::TryFrom; use std::convert::TryFrom;
use actix_web::{HttpResponse, Responder, web, get, post, put, delete};
use serde::{Serialize, Deserialize};
use crate::db::models::Periods; use crate::db::models::{Periods, Schedule};
use crate::db::schedules::*; use crate::db::schedules::*;
use crate::db::tag::get_tag; use crate::db::tag::get_tag;
use crate::handlers::errors::HandlerError; use crate::handlers::errors::HandlerError;
use crate::return_models::ReturnSchedule; use crate::return_models::ReturnSchedule;
use crate::types::EmgauwaUid; use crate::types::EmgauwaUid;
use crate::utils::vec_has_error;
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
pub struct RequestSchedule { pub struct RequestSchedule {
@ -20,27 +22,27 @@ pub struct RequestSchedule {
#[get("/api/v1/schedules")] #[get("/api/v1/schedules")]
pub async fn index() -> impl Responder { pub async fn index() -> impl Responder {
let schedules = get_schedules(); let schedules = get_schedules();
let return_schedules: Vec<ReturnSchedule> = schedules.into_iter().map(ReturnSchedule::from).collect(); let return_schedules: Vec<ReturnSchedule> =
schedules.iter().map(ReturnSchedule::from).collect();
HttpResponse::Ok().json(return_schedules) HttpResponse::Ok().json(return_schedules)
} }
#[get("/api/v1/schedules/tag/{tag}")] #[get("/api/v1/schedules/tag/{tag}")]
pub async fn tagged(web::Path((tag,)): web::Path<(String,)>) -> impl Responder { pub async fn tagged(web::Path((tag,)): web::Path<(String,)>) -> impl Responder {
let tag_db = get_tag(&tag); let tag_db = get_tag(&tag);
if tag_db.is_err() { if tag_db.is_err() {
return HttpResponse::from(tag_db.unwrap_err()) return HttpResponse::from(tag_db.unwrap_err());
} }
let tag_db = tag_db.unwrap(); let tag_db = tag_db.unwrap();
let schedules = get_schedules_by_tag(&tag_db); let schedules = get_schedules_by_tag(&tag_db);
let return_schedules: Vec<ReturnSchedule> = schedules.into_iter().map(ReturnSchedule::from).collect(); let return_schedules: Vec<ReturnSchedule> =
schedules.iter().map(ReturnSchedule::from).collect();
HttpResponse::Ok().json(return_schedules) HttpResponse::Ok().json(return_schedules)
} }
#[get("/api/v1/schedules/{schedule_id}")] #[get("/api/v1/schedules/{schedule_id}")]
pub async fn show(web::Path((schedule_uid,)): web::Path<(String,)>) -> impl Responder { pub async fn show(web::Path((schedule_uid,)): web::Path<(String,)>) -> impl Responder {
let emgauwa_uid = EmgauwaUid::try_from(schedule_uid.as_str()).or(Err(HandlerError::BadUid)); let emgauwa_uid = EmgauwaUid::try_from(schedule_uid.as_str()).or(Err(HandlerError::BadUid));
match emgauwa_uid { match emgauwa_uid {
@ -50,8 +52,8 @@ pub async fn show(web::Path((schedule_uid,)): web::Path<(String,)>) -> impl Resp
Ok(ok) => HttpResponse::Ok().json(ReturnSchedule::from(ok)), Ok(ok) => HttpResponse::Ok().json(ReturnSchedule::from(ok)),
Err(err) => HttpResponse::from(err), Err(err) => HttpResponse::from(err),
} }
}, }
Err(err) => HttpResponse::from(err) Err(err) => HttpResponse::from(err),
} }
} }
@ -60,7 +62,7 @@ pub async fn add(data: web::Json<RequestSchedule>) -> impl Responder {
let new_schedule = create_schedule(&data.name, &data.periods); let new_schedule = create_schedule(&data.name, &data.periods);
if new_schedule.is_err() { if new_schedule.is_err() {
return HttpResponse::from(new_schedule.unwrap_err()) return HttpResponse::from(new_schedule.unwrap_err());
} }
let new_schedule = new_schedule.unwrap(); let new_schedule = new_schedule.unwrap();
@ -72,9 +74,43 @@ pub async fn add(data: web::Json<RequestSchedule>) -> impl Responder {
HttpResponse::Created().json(ReturnSchedule::from(new_schedule)) HttpResponse::Created().json(ReturnSchedule::from(new_schedule))
} }
#[put("/api/v1/schedules/{schedule_id}")] #[post("/api/v1/schedules/list")]
pub async fn update(web::Path((schedule_uid,)): web::Path<(String,)>, data: web::Json<RequestSchedule>) -> impl Responder { pub async fn add_list(data: web::Json<Vec<RequestSchedule>>) -> impl Responder {
let result: Vec<Result<Schedule, DatabaseError>> = data
.as_slice()
.iter()
.map(|request_schedule| {
let new_schedule = create_schedule(&request_schedule.name, &request_schedule.periods)?;
set_schedule_tags(&new_schedule, request_schedule.tags.as_slice())?;
Ok(new_schedule)
})
.collect();
match vec_has_error(&result) {
true => HttpResponse::from(
result
.into_iter()
.find(|r| r.is_err())
.unwrap()
.unwrap_err(),
),
false => {
let return_schedules: Vec<ReturnSchedule> = result
.iter()
.map(|s| ReturnSchedule::from(s.as_ref().unwrap()))
.collect();
HttpResponse::Created().json(return_schedules)
}
}
}
#[put("/api/v1/schedules/{schedule_id}")]
pub async fn update(
web::Path((schedule_uid,)): web::Path<(String,)>,
data: web::Json<RequestSchedule>,
) -> impl Responder {
let emgauwa_uid = EmgauwaUid::try_from(schedule_uid.as_str()).or(Err(HandlerError::BadUid)); let emgauwa_uid = EmgauwaUid::try_from(schedule_uid.as_str()).or(Err(HandlerError::BadUid));
if emgauwa_uid.is_err() { if emgauwa_uid.is_err() {
return HttpResponse::from(emgauwa_uid.unwrap_err()); return HttpResponse::from(emgauwa_uid.unwrap_err());
@ -109,13 +145,11 @@ pub async fn delete(web::Path((schedule_uid,)): web::Path<(String,)>) -> impl Re
Ok(uid) => match uid { Ok(uid) => match uid {
EmgauwaUid::Off => HttpResponse::from(HandlerError::ProtectedSchedule), EmgauwaUid::Off => HttpResponse::from(HandlerError::ProtectedSchedule),
EmgauwaUid::On => HttpResponse::from(HandlerError::ProtectedSchedule), EmgauwaUid::On => HttpResponse::from(HandlerError::ProtectedSchedule),
EmgauwaUid::Any(_) => { EmgauwaUid::Any(_) => match delete_schedule_by_uid(uid) {
match delete_schedule_by_uid(uid) {
Ok(_) => HttpResponse::Ok().json("schedule got deleted"), Ok(_) => HttpResponse::Ok().json("schedule got deleted"),
Err(err) => HttpResponse::from(err) Err(err) => HttpResponse::from(err),
}
}
}, },
Err(err) => HttpResponse::from(err) },
Err(err) => HttpResponse::from(err),
} }
} }

View file

@ -2,11 +2,11 @@
extern crate diesel; extern crate diesel;
#[macro_use] #[macro_use]
extern crate diesel_migrations; extern crate diesel_migrations;
extern crate dotenv;
extern crate core; extern crate core;
extern crate dotenv;
use actix_web::{middleware, web, App, HttpServer};
use actix_web::middleware::normalize::TrailingSlash; use actix_web::middleware::normalize::TrailingSlash;
use actix_web::{middleware, web, App, HttpServer};
use env_logger::{Builder, Env}; use env_logger::{Builder, Env};
use wiringpi::pin::Value::High; use wiringpi::pin::Value::High;
@ -14,6 +14,7 @@ mod db;
mod handlers; mod handlers;
mod return_models; mod return_models;
mod types; mod types;
mod utils;
#[actix_web::main] #[actix_web::main]
async fn main() -> std::io::Result<()> { async fn main() -> std::io::Result<()> {
@ -29,10 +30,11 @@ async fn main() -> std::io::Result<()> {
HttpServer::new(|| { HttpServer::new(|| {
App::new() App::new()
.wrap(middleware::DefaultHeaders::new() .wrap(
middleware::DefaultHeaders::new()
.header("Access-Control-Allow-Origin", "*") .header("Access-Control-Allow-Origin", "*")
.header("Access-Control-Allow-Headers", "*") .header("Access-Control-Allow-Headers", "*")
.header("Access-Control-Allow-Methods", "*") .header("Access-Control-Allow-Methods", "*"),
) )
.wrap(middleware::Logger::default()) .wrap(middleware::Logger::default())
.wrap(middleware::NormalizePath::new(TrailingSlash::Trim)) .wrap(middleware::NormalizePath::new(TrailingSlash::Trim))
@ -41,6 +43,7 @@ async fn main() -> std::io::Result<()> {
.service(handlers::v1::schedules::tagged) .service(handlers::v1::schedules::tagged)
.service(handlers::v1::schedules::show) .service(handlers::v1::schedules::show)
.service(handlers::v1::schedules::add) .service(handlers::v1::schedules::add)
.service(handlers::v1::schedules::add_list)
.service(handlers::v1::schedules::update) .service(handlers::v1::schedules::update)
.service(handlers::v1::schedules::delete) .service(handlers::v1::schedules::delete)
}) })

View file

@ -1,4 +1,4 @@
use serde::{Serialize}; use serde::Serialize;
use crate::db::models::Schedule; use crate::db::models::Schedule;
use crate::db::schedules::get_schedule_tags; use crate::db::schedules::get_schedule_tags;
@ -13,9 +13,12 @@ pub struct ReturnSchedule {
impl From<Schedule> for ReturnSchedule { impl From<Schedule> for ReturnSchedule {
fn from(schedule: Schedule) -> Self { fn from(schedule: Schedule) -> Self {
let tags: Vec<String> = get_schedule_tags(&schedule); let tags: Vec<String> = get_schedule_tags(&schedule);
ReturnSchedule { ReturnSchedule { schedule, tags }
schedule, }
tags, }
}
impl From<&Schedule> for ReturnSchedule {
fn from(schedule: &Schedule) -> Self {
ReturnSchedule::from(schedule.clone())
} }
} }

3
src/utils.rs Normal file
View file

@ -0,0 +1,3 @@
pub fn vec_has_error<T, E>(target: &[Result<T, E>]) -> bool {
target.iter().any(|t| t.is_err())
}