F5DMFQAO2IVQXQQ36Z5FRW5WNXKNTY2NAWMRR3VXIOWTPJ5UUDVAC
S6TFYMRGWC4PSRLQ4OAKE2Z3JFEVGF3RZ5Z3MEN5XSA4INPQILOAC
W3M3C7CCWHJWRWHULDWO45D3OFD4NL3V4OTJVIJCYRQG57Z2JTWQC
TWIZ7QV4GCTQK743IKZSOJCIAEX62GZHFIYGOIFCFGIBOGPSY2WAC
DSWQKJRHGLKXUDXKMRXCIQZKEGXYV3H5LPUMGSV4HQ4HYPTI47GQC
FS2NWBVN2SZB2FFPB3JSYT5URTVZEAZWMQ7QW4JUYPNJVDVAJTTQC
3E77DEMDLYBFJEGS2SLQKLQJQIXG2Y4TBDHAG3UKFLMKQ53CFOKQC
use crate::database::Database;
use lazy_static::lazy_static;
use regex::Regex;
use rocket::{
form::{self, Context, Error, Form},
response::{Flash, Redirect},
Route, State,
};
use rocket_dyn_templates::Template;
#[get("/new")]
Template::render("projects/new", &Context::default())
}
fn validate_project_name<'v>(name: &String) -> form::Result<'v, ()> {
lazy_static! {
static ref RE: Regex = Regex::new(r"\A[\w\d]{2,20}\z").unwrap();
}
if !RE.is_match(&name) {
Err(Error::validation(
"only up to 20 letters or digets are to be used as project name",
))?;
}
Ok(())
}
#[derive(FromForm)]
struct NewProject {
#[field(validate = validate_project_name())]
name: String,
}
#[post("/", data = "<project>")]
async fn create(
db: &State<Database>,
project: Form<NewProject>,
) -> Result<Flash<Redirect>, Flash<Redirect>> {
Ok(_) => Ok(Flash::success(Redirect::to("/"), "Project is created")),
Err(_e) => Err(Flash::error(
Redirect::to("/projects/new"),
"Something went wrong",
)),
}
}
pub fn routes() -> Vec<Route> {
routes![new, create]
}
// TODO replace this with user
match projects::create(db, user.id, project.name.clone()).await {
user: User,
use crate::models::projects;
fn new(_user: User) -> Template {
use crate::models::users::User;
use crate::database::Database;
use crate::models::users::User;
use lazy_static::lazy_static;
use regex::Regex;
use rocket::{
form::{self, Context, Error, Form},
response::{Flash, Redirect},
Route, State,
};
use rocket_dyn_templates::Template;
#[get("/projects/new")]
fn new(_user: User) -> Template {
Template::render("projects/new", &Context::default())
}
fn validate_project_name<'v>(name: &String) -> form::Result<'v, ()> {
lazy_static! {
static ref RE: Regex = Regex::new(r"\A[\w\d]{2,20}\z").unwrap();
}
if !RE.is_match(&name) {
Err(Error::validation(
"only up to 20 letters or digets are to be used as project name",
))?;
}
Ok(())
}
#[derive(FromForm)]
struct NewProject {
#[field(validate = validate_project_name())]
name: String,
}
use crate::models::projects;
#[post("/projects", data = "<project>")]
async fn create(
db: &State<Database>,
project: Form<NewProject>,
user: User,
) -> Result<Flash<Redirect>, Flash<Redirect>> {
// TODO replace this with user
match projects::create(db, user.id, project.name.clone()).await {
Ok(_) => Ok(Flash::success(Redirect::to("/"), "Project is created")),
Err(_e) => Err(Flash::error(
Redirect::to("/projects/new"),
"Something went wrong",
)),
}
}
pub fn routes() -> Vec<Route> {
routes![new, create]
}