feat(bots): implement bot routes

This commit is contained in:
Paul Makles
2022-02-10 15:54:41 +00:00
parent d4aff072a8
commit a8d5fcec9e
12 changed files with 206 additions and 58 deletions

View File

@@ -1,9 +1,14 @@
use crate::util::regex::RE_USERNAME;
use crate::util::{regex::RE_USERNAME, variables::MAX_BOT_COUNT};
use revolt_quark::Result;
use nanoid::nanoid;
use revolt_quark::{
models::{Bot, User},
Db, Error, Result,
};
use rocket::serde::json::{Json, Value};
use rocket::serde::json::Json;
use serde::{Deserialize, Serialize};
use ulid::Ulid;
use validator::Validate;
#[derive(Validate, Serialize, Deserialize)]
@@ -13,6 +18,26 @@ pub struct Data {
}
#[post("/create", data = "<info>")]
pub async fn create_bot(/* user: User ,*/ info: Json<Data>) -> Result<Value> {
todo!()
pub async fn create_bot(db: &Db, user: User, info: Json<Data>) -> Result<Json<Bot>> {
if user.bot.is_some() {
return Err(Error::IsBot);
}
let info = info.into_inner();
info.validate()
.map_err(|error| Error::FailedValidation { error })?;
if db.get_number_of_bots_by_user(&user.id).await? >= *MAX_BOT_COUNT {
return Err(Error::ReachedMaximumBots);
}
let bot = Bot {
id: Ulid::new().to_string(),
owner: user.id,
token: nanoid!(64),
..Default::default()
};
db.insert_bot(&bot).await?;
Ok(Json(bot))
}