feat: bit more robust register
parent
04e268a17c
commit
53aeb2339f
|
@ -10,17 +10,26 @@ pub async fn register(
|
|||
email: String,
|
||||
) -> Result<(), Error> {
|
||||
if let Some(guild_id) = ctx.guild_id() {
|
||||
let new_user = NewUser {
|
||||
discord_id: ctx.author().id.0 as i64,
|
||||
guild_id: guild_id.into(),
|
||||
first_name,
|
||||
last_name,
|
||||
email,
|
||||
};
|
||||
let discord_id = ctx.author().id.0 as i64;
|
||||
|
||||
{
|
||||
let mut conn = ctx.data().pool.get()?;
|
||||
new_user.insert(&mut conn);
|
||||
|
||||
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 {
|
||||
discord_id,
|
||||
guild_id: guild_id.into(),
|
||||
first_name,
|
||||
last_name,
|
||||
email,
|
||||
};
|
||||
|
||||
new_user.insert(&mut conn)?;
|
||||
}
|
||||
|
||||
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> {
|
||||
let manager = ConnectionManager::new(url);
|
||||
|
||||
let pool = Pool::builder()
|
||||
.test_on_check_out(true)
|
||||
.build(manager)
|
||||
.expect("oops");
|
||||
let pool = Pool::builder().test_on_check_out(true).build(manager)?;
|
||||
|
||||
let mut conn = pool.get()?;
|
||||
initialize_db(&mut conn)?;
|
||||
|
|
|
@ -1,14 +1,12 @@
|
|||
use super::schema::users::{self, dsl::*};
|
||||
use diesel::dsl::Eq;
|
||||
use diesel::dsl::{AsSelect, Select};
|
||||
use diesel::expression::AsExpression;
|
||||
use diesel::helper_types::Filter;
|
||||
use diesel::prelude::*;
|
||||
use diesel::sql_types::BigInt;
|
||||
use diesel::sqlite::Sqlite;
|
||||
use diesel::sqlite::SqliteConnection;
|
||||
|
||||
#[derive(Queryable, Selectable)]
|
||||
#[derive(Queryable, Selectable, AsChangeset)]
|
||||
#[diesel(table_name = users)]
|
||||
pub struct User {
|
||||
pub id: i32,
|
||||
|
@ -52,23 +50,30 @@ impl User {
|
|||
conn: &mut SqliteConnection,
|
||||
guild_id_: i64,
|
||||
discord_id_: i64,
|
||||
) -> Result<User, diesel::result::Error> {
|
||||
) -> Result<Option<User>, diesel::result::Error> {
|
||||
Self::all()
|
||||
.filter(guild_id.eq(guild_id_))
|
||||
.filter(discord_id.eq(discord_id_))
|
||||
.first(conn)
|
||||
.optional()
|
||||
}
|
||||
|
||||
pub fn get_by_id(conn: &mut SqliteConnection, id_: i32) -> Result<User, diesel::result::Error> {
|
||||
Self::all().filter(id.eq(id_)).first(conn)
|
||||
pub fn get_by_id(
|
||||
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 {
|
||||
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)
|
||||
.values(self)
|
||||
.get_result(conn)
|
||||
.expect("fuck")
|
||||
}
|
||||
}
|
||||
|
|
|
@ -84,7 +84,7 @@ async fn main() {
|
|||
..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()
|
||||
.token(
|
||||
|
|
Loading…
Reference in New Issue