feat: bit more robust register
parent
04e268a17c
commit
53aeb2339f
|
@ -10,17 +10,26 @@ pub async fn register(
|
||||||
email: String,
|
email: String,
|
||||||
) -> Result<(), Error> {
|
) -> Result<(), Error> {
|
||||||
if let Some(guild_id) = ctx.guild_id() {
|
if let Some(guild_id) = ctx.guild_id() {
|
||||||
|
let discord_id = ctx.author().id.0 as i64;
|
||||||
|
|
||||||
|
{
|
||||||
|
let mut conn = ctx.data().pool.get()?;
|
||||||
|
|
||||||
|
if User::get(&mut conn, guild_id.into(), discord_id)?.is_some() {
|
||||||
|
ctx.say("You've already been registered.").await?;
|
||||||
|
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
let new_user = NewUser {
|
let new_user = NewUser {
|
||||||
discord_id: ctx.author().id.0 as i64,
|
discord_id,
|
||||||
guild_id: guild_id.into(),
|
guild_id: guild_id.into(),
|
||||||
first_name,
|
first_name,
|
||||||
last_name,
|
last_name,
|
||||||
email,
|
email,
|
||||||
};
|
};
|
||||||
|
|
||||||
{
|
new_user.insert(&mut conn)?;
|
||||||
let mut conn = ctx.data().pool.get()?;
|
|
||||||
new_user.insert(&mut conn);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.say("You have been registered.").await?;
|
ctx.say("You have been registered.").await?;
|
||||||
|
|
|
@ -34,10 +34,7 @@ fn initialize_db(conn: &mut SqliteConnection) -> Result<(), DbError> {
|
||||||
pub fn initialize_pool(url: &str) -> Result<Pool<ConnectionManager<SqliteConnection>>, DbError> {
|
pub fn initialize_pool(url: &str) -> Result<Pool<ConnectionManager<SqliteConnection>>, DbError> {
|
||||||
let manager = ConnectionManager::new(url);
|
let manager = ConnectionManager::new(url);
|
||||||
|
|
||||||
let pool = Pool::builder()
|
let pool = Pool::builder().test_on_check_out(true).build(manager)?;
|
||||||
.test_on_check_out(true)
|
|
||||||
.build(manager)
|
|
||||||
.expect("oops");
|
|
||||||
|
|
||||||
let mut conn = pool.get()?;
|
let mut conn = pool.get()?;
|
||||||
initialize_db(&mut conn)?;
|
initialize_db(&mut conn)?;
|
||||||
|
|
|
@ -1,14 +1,12 @@
|
||||||
use super::schema::users::{self, dsl::*};
|
use super::schema::users::{self, dsl::*};
|
||||||
use diesel::dsl::Eq;
|
use diesel::dsl::Eq;
|
||||||
use diesel::dsl::{AsSelect, Select};
|
use diesel::dsl::{AsSelect, Select};
|
||||||
use diesel::expression::AsExpression;
|
|
||||||
use diesel::helper_types::Filter;
|
use diesel::helper_types::Filter;
|
||||||
use diesel::prelude::*;
|
use diesel::prelude::*;
|
||||||
use diesel::sql_types::BigInt;
|
|
||||||
use diesel::sqlite::Sqlite;
|
use diesel::sqlite::Sqlite;
|
||||||
use diesel::sqlite::SqliteConnection;
|
use diesel::sqlite::SqliteConnection;
|
||||||
|
|
||||||
#[derive(Queryable, Selectable)]
|
#[derive(Queryable, Selectable, AsChangeset)]
|
||||||
#[diesel(table_name = users)]
|
#[diesel(table_name = users)]
|
||||||
pub struct User {
|
pub struct User {
|
||||||
pub id: i32,
|
pub id: i32,
|
||||||
|
@ -52,23 +50,30 @@ impl User {
|
||||||
conn: &mut SqliteConnection,
|
conn: &mut SqliteConnection,
|
||||||
guild_id_: i64,
|
guild_id_: i64,
|
||||||
discord_id_: i64,
|
discord_id_: i64,
|
||||||
) -> Result<User, diesel::result::Error> {
|
) -> Result<Option<User>, diesel::result::Error> {
|
||||||
Self::all()
|
Self::all()
|
||||||
.filter(guild_id.eq(guild_id_))
|
.filter(guild_id.eq(guild_id_))
|
||||||
.filter(discord_id.eq(discord_id_))
|
.filter(discord_id.eq(discord_id_))
|
||||||
.first(conn)
|
.first(conn)
|
||||||
|
.optional()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_by_id(conn: &mut SqliteConnection, id_: i32) -> Result<User, diesel::result::Error> {
|
pub fn get_by_id(
|
||||||
Self::all().filter(id.eq(id_)).first(conn)
|
conn: &mut SqliteConnection,
|
||||||
|
id_: i32,
|
||||||
|
) -> Result<Option<User>, diesel::result::Error> {
|
||||||
|
Self::all().find(id_).first(conn).optional()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn update(&self, conn: &mut SqliteConnection) -> Result<usize, diesel::result::Error> {
|
||||||
|
diesel::update(users::table).set(self).execute(conn)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl NewUser {
|
impl NewUser {
|
||||||
pub fn insert(&self, conn: &mut SqliteConnection) -> User {
|
pub fn insert(&self, conn: &mut SqliteConnection) -> Result<User, diesel::result::Error> {
|
||||||
diesel::insert_into(users::table)
|
diesel::insert_into(users::table)
|
||||||
.values(self)
|
.values(self)
|
||||||
.get_result(conn)
|
.get_result(conn)
|
||||||
.expect("fuck")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -84,7 +84,7 @@ async fn main() {
|
||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
|
|
||||||
let pool = db::initialize_pool("affy.db").unwrap();
|
let pool = db::initialize_pool("affy.db").expect("Failed to initialize database.");
|
||||||
|
|
||||||
poise::Framework::builder()
|
poise::Framework::builder()
|
||||||
.token(
|
.token(
|
||||||
|
|
Loading…
Reference in New Issue