forked from jmug/stoatchat
feat(onboard): implement onboarding routes
This commit is contained in:
@@ -1,9 +1,9 @@
|
|||||||
use revolt_quark::{EmptyResponse, Result};
|
use revolt_quark::{EmptyResponse, Error, Result, models::User, Database};
|
||||||
use crate::util::regex::RE_USERNAME;
|
use crate::util::regex::RE_USERNAME;
|
||||||
|
|
||||||
use mongodb::bson::doc;
|
use mongodb::bson::doc;
|
||||||
use rauth::entities::Session;
|
use rauth::entities::Session;
|
||||||
use rocket::serde::json::Json;
|
use rocket::{serde::json::Json, State};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use validator::Validate;
|
use validator::Validate;
|
||||||
|
|
||||||
@@ -14,6 +14,26 @@ pub struct Data {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[post("/complete", data = "<data>")]
|
#[post("/complete", data = "<data>")]
|
||||||
pub async fn req(/*session: Session, user: Option<User>,*/ data: Json<Data>) -> Result<EmptyResponse> {
|
pub async fn req(db: &State<Database>, session: Session, user: Option<User>, data: Json<Data>) -> Result<EmptyResponse> {
|
||||||
todo!()
|
if user.is_some() {
|
||||||
|
return Err(Error::AlreadyOnboarded)
|
||||||
|
}
|
||||||
|
|
||||||
|
let data = data.into_inner();
|
||||||
|
data.validate()
|
||||||
|
.map_err(|error| Error::FailedValidation { error })?;
|
||||||
|
|
||||||
|
if db.is_username_taken(&data.username).await? {
|
||||||
|
return Err(Error::UsernameTaken);
|
||||||
|
}
|
||||||
|
|
||||||
|
let user = User {
|
||||||
|
id: session.user_id,
|
||||||
|
username: data.username,
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
|
||||||
|
db.insert_user(&user)
|
||||||
|
.await
|
||||||
|
.map(|_| EmptyResponse)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,10 @@
|
|||||||
use rauth::entities::Session;
|
use rauth::entities::Session;
|
||||||
|
use revolt_quark::models::User;
|
||||||
use rocket::serde::json::Value;
|
use rocket::serde::json::Value;
|
||||||
|
|
||||||
#[get("/hello")]
|
#[get("/hello")]
|
||||||
pub async fn req(/*_session: Session, user: Option<User>*/) -> Value {
|
pub async fn req(_session: Session, user: Option<User>) -> Value {
|
||||||
todo!()
|
json!({
|
||||||
|
"onboarding": user.is_none()
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user