2022-02-02 16:19:49 +01:00
|
|
|
use actix_web::{web, HttpResponse};
|
2022-09-11 00:33:09 +02:00
|
|
|
use diesel::prelude::*;
|
2020-01-12 13:11:12 +01:00
|
|
|
use serde::Deserialize;
|
2018-12-09 16:55:36 +01:00
|
|
|
|
2022-08-28 19:39:28 +02:00
|
|
|
use crate::{
|
|
|
|
email_service::send_invitation,
|
|
|
|
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-12-07 18:59:24 +01:00
|
|
|
pub async fn post_invitation(
|
2019-07-18 13:55:14 +02:00
|
|
|
invitation_data: web::Json<InvitationData>,
|
|
|
|
pool: web::Data<Pool>,
|
2022-02-02 16:19:49 +01:00
|
|
|
) -> Result<HttpResponse, actix_web::Error> {
|
2019-07-18 13:55:14 +02:00
|
|
|
// run diesel blocking code
|
2022-02-18 03:44:02 +01:00
|
|
|
web::block(move || create_invitation(invitation_data.into_inner().email, pool)).await??;
|
2022-02-02 16:19:49 +01:00
|
|
|
|
|
|
|
Ok(HttpResponse::Ok().finish())
|
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
|
2022-02-18 03:44:02 +01: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
|
|
|
|
2022-09-11 00:33:09 +02:00
|
|
|
let mut conn = pool.get().unwrap();
|
|
|
|
|
|
|
|
let new_invitation = Invitation::from(eml);
|
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)
|
2022-09-11 00:33:09 +02:00
|
|
|
.get_result(&mut 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
|
|
|
}
|