invites usage route '/invite/<hash>' now usable

This commit is contained in:
shockrah 2020-02-05 17:45:12 -08:00
parent da0af22ee5
commit d2c45761dd
3 changed files with 26 additions and 8 deletions

View File

@ -6,6 +6,12 @@ use crate::DBConn;
use crate::models::Invite; use crate::models::Invite;
use crate::schema; use crate::schema;
/*
TODO: both the generation and usage endpoints for invites need the following
* meaningful responses
* authentication
*/
#[get("/generate")] #[get("/generate")]
pub fn generate_invite(conn: DBConn) -> Result<String, String> { pub fn generate_invite(conn: DBConn) -> Result<String, String> {
let dt = Utc::now() + Duration::minutes(30); let dt = Utc::now() + Duration::minutes(30);
@ -28,8 +34,20 @@ pub fn generate_invite(conn: DBConn) -> Result<String, String> {
} }
#[get("/<hash>")] #[get("/<hash>")]
pub fn use_invite(hash: i64) -> String { pub fn use_invite(hash: u64, conn: DBConn) -> Result<String, String> {
// check if the invite is somewhere in our invites table // jank but whatever
format!("freechat.io/invite/{}", hash) use schema::invites::dsl::*;
let data: Vec<Invite> = invites.select((id, expires, uses))
.filter(id.eq(hash))
.load(&conn.0)
.unwrap();
if data.is_empty() {
Err("invite does not exist".to_string())
}
else {
let row = diesel::delete(invites.filter(id.eq(hash))).execute(&conn.0);
Ok(format!("Invite used successfully {:?}", row))
}
} }

View File

@ -40,10 +40,9 @@ fn rocket() -> rocket::Rocket {
} }
fn main() -> Result<(), i32> { fn main() -> Result<(), i32> {
let dot = dotenv(); match dotenv() {
match dot { Ok(_d) => {
Ok(d) => { println!("Got .env: {:?}", var("DATABASE_URL").unwrap());
println!("Got .env: {}", var("DATABASE_URL").unwrap());
rocket().launch(); rocket().launch();
}, },
Err(e) => { Err(e) => {

View File

@ -1,5 +1,6 @@
use crate::schema::invites; use crate::schema::invites;
#[derive(Insertable, Serialize, Deserialize, Queryable)] #[derive(Insertable, Serialize, Deserialize, Queryable, Debug)]
#[table_name = "invites"]
pub struct Invite { pub struct Invite {
pub id: u64, pub id: u64,
pub expires: u64, pub expires: u64,