141 lines
3.1 KiB
Rust
141 lines
3.1 KiB
Rust
use super::prelude::*;
|
|
use futures::{StreamExt, TryStreamExt};
|
|
use lan_party_core::user::*;
|
|
use rocket_db_pools::mongodb::options::FindOptions;
|
|
use util::{Ordering, PartyError};
|
|
|
|
api_routes!(
|
|
add_user,
|
|
get_user,
|
|
get_all_users,
|
|
delete_user,
|
|
set_score,
|
|
get_score
|
|
);
|
|
|
|
/// # Create new user with the give name
|
|
///
|
|
/// Returns the created user
|
|
#[openapi(tag = "User")]
|
|
#[post("/", data = "<name>")]
|
|
pub async fn add_user(
|
|
_api_key: ApiKey,
|
|
db: Connection<Db>,
|
|
name: Json<&str>,
|
|
) -> Result<status::Created<Json<User>>, PartyError> {
|
|
let user = User {
|
|
score: 0,
|
|
name: name.to_string(),
|
|
};
|
|
|
|
db.users().insert_one(&user, None).await?;
|
|
|
|
Ok(status::Created::new("/").body(Json(user)))
|
|
}
|
|
|
|
/// # Delete user by name
|
|
#[openapi(tag = "User")]
|
|
#[delete("/<name>")]
|
|
pub async fn delete_user(
|
|
_api_key: ApiKey,
|
|
db: Connection<Db>,
|
|
name: String,
|
|
) -> Result<Status, PartyError> {
|
|
db.users().delete_one(doc! { "name": name }, None).await?;
|
|
|
|
Ok(Status::Ok)
|
|
}
|
|
|
|
/// # Get user by id
|
|
///
|
|
/// Returns a single user by name
|
|
#[openapi(tag = "User")]
|
|
#[get("/<name>")]
|
|
pub async fn get_user(
|
|
_api_key: ApiKey,
|
|
db: Connection<Db>,
|
|
name: String,
|
|
) -> Result<Json<User>, PartyError> {
|
|
let user = db
|
|
.users()
|
|
.find_one(doc! { "name": &name }, None)
|
|
.await?
|
|
.ok_or(PartyError::UserNotFound(name))?;
|
|
Ok(Json(user))
|
|
}
|
|
|
|
/// # Get all users
|
|
///
|
|
/// Returns an array of all users sorted by the given sort field and ordering
|
|
#[openapi(tag = "User")]
|
|
#[get("/?<sort>&<order>")]
|
|
pub async fn get_all_users(
|
|
_api_key: ApiKey,
|
|
db: Connection<Db>,
|
|
sort: Option<UserSort>,
|
|
order: Option<Ordering>,
|
|
) -> Result<Json<Vec<User>>, PartyError> {
|
|
let options = if let Some(sort) = sort {
|
|
let order = order.unwrap_or(Ordering::Asc);
|
|
let mut doc = Document::new();
|
|
doc.insert(
|
|
sort.to_string(),
|
|
match order {
|
|
Ordering::Asc => 1,
|
|
Ordering::Desc => -1,
|
|
},
|
|
);
|
|
FindOptions::builder().sort(doc).build()
|
|
} else {
|
|
FindOptions::builder().build()
|
|
};
|
|
|
|
let users = db
|
|
.users()
|
|
.find(doc! {}, Some(options))
|
|
.await?
|
|
.try_collect()
|
|
.await?;
|
|
|
|
Ok(Json(users))
|
|
}
|
|
|
|
/// # Set score of user by name
|
|
#[openapi(tag = "User")]
|
|
#[post("/<name>/score", data = "<score>")]
|
|
pub async fn set_score(
|
|
_api_key: ApiKey,
|
|
db: Connection<Db>,
|
|
name: String,
|
|
score: Json<i64>,
|
|
) -> Result<Status, PartyError> {
|
|
db.users()
|
|
.update_one(
|
|
doc! { "name": name },
|
|
doc! { "$set": { "score": *score } },
|
|
None,
|
|
)
|
|
.await?;
|
|
|
|
Ok(Status::Ok)
|
|
}
|
|
|
|
/// # Get score of user by name
|
|
///
|
|
/// Returns the score of a single user by name
|
|
#[openapi(tag = "User")]
|
|
#[get("/<name>/score")]
|
|
pub async fn get_score(
|
|
_api_key: ApiKey,
|
|
db: Connection<Db>,
|
|
name: String,
|
|
) -> Result<Json<i64>, PartyError> {
|
|
Ok(Json(
|
|
db.users()
|
|
.find_one(doc! { "name": &name }, None)
|
|
.await?
|
|
.ok_or(PartyError::UserNotFound(name))?
|
|
.score,
|
|
))
|
|
}
|