Start work on notifications from client, cargo fmt

This commit is contained in:
Paul Makles
2020-12-30 11:36:32 +00:00
parent af56f5e2d8
commit f39bc07bb9
34 changed files with 341 additions and 264 deletions

View File

@@ -1,13 +1,13 @@
use mongodb::options::{Collation, FindOneOptions};
use crate::util::result::{Error, Result};
use serde::{Deserialize, Serialize};
use crate::database::entities::User;
use crate::database::get_collection;
use rocket_contrib::json::Json;
use rauth::auth::Session;
use validator::Validate;
use crate::util::result::{Error, Result};
use mongodb::bson::doc;
use mongodb::options::{Collation, FindOneOptions};
use rauth::auth::Session;
use regex::Regex;
use rocket_contrib::json::Json;
use serde::{Deserialize, Serialize};
use validator::Validate;
lazy_static! {
static ref RE_USERNAME: Regex = Regex::new(r"^[a-zA-Z0-9-_]+$").unwrap();
@@ -16,7 +16,7 @@ lazy_static! {
#[derive(Validate, Serialize, Deserialize)]
pub struct Data {
#[validate(length(min = 2, max = 32), regex = "RE_USERNAME")]
username: String
username: String,
}
#[post("/complete", data = "<data>")]
@@ -24,22 +24,27 @@ pub async fn req(session: Session, user: Option<User>, data: Json<Data>) -> Resu
if user.is_some() {
Err(Error::AlreadyOnboarded)?
}
data.validate()
.map_err(|error| Error::FailedValidation { error })?;
let col = get_collection("users");
if col.find_one(
doc! {
"username": &data.username
},
FindOneOptions::builder()
.collation(Collation::builder().locale("en").strength(2).build())
.build()
)
.await
.map_err(|_| Error::DatabaseError { operation: "find_one", with: "user" })?
.is_some() {
if col
.find_one(
doc! {
"username": &data.username
},
FindOneOptions::builder()
.collation(Collation::builder().locale("en").strength(2).build())
.build(),
)
.await
.map_err(|_| Error::DatabaseError {
operation: "find_one",
with: "user",
})?
.is_some()
{
Err(Error::UsernameTaken)?
}
@@ -48,10 +53,13 @@ pub async fn req(session: Session, user: Option<User>, data: Json<Data>) -> Resu
"_id": session.user_id,
"username": &data.username
},
None
None,
)
.await
.map_err(|_| Error::DatabaseError { operation: "insert_one", with: "user" })?;
.map_err(|_| Error::DatabaseError {
operation: "insert_one",
with: "user",
})?;
Ok(())
}

View File

@@ -1,6 +1,6 @@
use rocket_contrib::json::JsonValue;
use crate::database::entities::User;
use rauth::auth::Session;
use rocket_contrib::json::JsonValue;
#[get("/hello")]
pub async fn req(_session: Session, user: Option<User>) -> JsonValue {

View File

@@ -1,11 +1,8 @@
use rocket::Route;
mod hello;
mod complete;
mod hello;
pub fn routes() -> Vec<Route> {
routes! [
hello::req,
complete::req
]
routes![hello::req, complete::req]
}