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::schema;
/*
TODO: both the generation and usage endpoints for invites need the following
* meaningful responses
* authentication
*/
#[get("/generate")]
pub fn generate_invite(conn: DBConn) -> Result<String, String> {
let dt = Utc::now() + Duration::minutes(30);
@ -28,8 +34,20 @@ pub fn generate_invite(conn: DBConn) -> Result<String, String> {
}
#[get("/<hash>")]
pub fn use_invite(hash: i64) -> String {
// check if the invite is somewhere in our invites table
format!("freechat.io/invite/{}", hash)
pub fn use_invite(hash: u64, conn: DBConn) -> Result<String, String> {
// jank but whatever
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> {
let dot = dotenv();
match dot {
Ok(d) => {
println!("Got .env: {}", var("DATABASE_URL").unwrap());
match dotenv() {
Ok(_d) => {
println!("Got .env: {:?}", var("DATABASE_URL").unwrap());
rocket().launch();
},
Err(e) => {

View File

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