2019-07-18 13:55:14 +02:00
|
|
|
use actix_web::{error::BlockingError, web, HttpResponse};
|
|
|
|
use diesel::{prelude::*, PgConnection};
|
|
|
|
use futures::Future;
|
2018-12-09 16:55:36 +01:00
|
|
|
|
2019-07-18 13:55:14 +02:00
|
|
|
use crate::email_service::send_invitation;
|
2019-03-29 21:43:03 +01:00
|
|
|
use crate::errors::ServiceError;
|
2019-07-18 13:55:14 +02:00
|
|
|
use crate::models::{Invitation, Pool};
|
2019-03-29 21:43:03 +01:00
|
|
|
|
2018-12-09 16:55:36 +01:00
|
|
|
#[derive(Deserialize)]
|
2019-07-18 13:55:14 +02:00
|
|
|
pub struct InvitationData {
|
2018-12-09 16:55:36 +01:00
|
|
|
pub email: String,
|
|
|
|
}
|
|
|
|
|
2019-07-18 13:55:14 +02:00
|
|
|
pub fn post_invitation(
|
|
|
|
invitation_data: web::Json<InvitationData>,
|
|
|
|
pool: web::Data<Pool>,
|
|
|
|
) -> impl Future<Item = HttpResponse, Error = ServiceError> {
|
|
|
|
// run diesel blocking code
|
2019-07-18 14:03:19 +02:00
|
|
|
web::block(move || create_invitation(invitation_data.into_inner().email, pool)).then(
|
|
|
|
|res| match res {
|
2019-07-18 13:55:14 +02:00
|
|
|
Ok(_) => Ok(HttpResponse::Ok().finish()),
|
|
|
|
Err(err) => match err {
|
|
|
|
BlockingError::Error(service_error) => Err(service_error),
|
|
|
|
BlockingError::Canceled => Err(ServiceError::InternalServerError),
|
|
|
|
},
|
2019-07-18 14:03:19 +02:00
|
|
|
},
|
|
|
|
)
|
2018-12-09 16:55:36 +01:00
|
|
|
}
|
|
|
|
|
2019-07-18 13:55:14 +02:00
|
|
|
fn create_invitation(
|
|
|
|
eml: String,
|
|
|
|
pool: web::Data<Pool>,
|
|
|
|
) -> Result<(), crate::errors::ServiceError> {
|
|
|
|
let invitation = dbg!(query(eml, pool)?);
|
|
|
|
send_invitation(&invitation)
|
|
|
|
}
|
2018-12-09 16:55:36 +01:00
|
|
|
|
2019-07-18 13:55:14 +02:00
|
|
|
/// Diesel query
|
2019-07-18 14:03:19 +02:00
|
|
|
fn query(
|
|
|
|
eml: String,
|
|
|
|
pool: web::Data<Pool>,
|
|
|
|
) -> Result<Invitation, crate::errors::ServiceError> {
|
2019-07-18 13:55:14 +02:00
|
|
|
use crate::schema::invitations::dsl::invitations;
|
2018-12-09 16:55:36 +01:00
|
|
|
|
2019-07-18 14:03:19 +02:00
|
|
|
let new_invitation: Invitation = eml.into();
|
2019-07-18 13:55:14 +02:00
|
|
|
let conn: &PgConnection = &pool.get().unwrap();
|
2018-12-09 16:55:36 +01:00
|
|
|
|
2019-07-18 13:55:14 +02:00
|
|
|
let inserted_invitation = diesel::insert_into(invitations)
|
|
|
|
.values(&new_invitation)
|
|
|
|
.get_result(conn)?;
|
2018-12-09 16:55:36 +01:00
|
|
|
|
2019-07-18 13:55:14 +02:00
|
|
|
Ok(inserted_invitation)
|
2018-12-09 16:55:36 +01:00
|
|
|
}
|