Add color to accommodations
This commit is contained in:
@@ -8,10 +8,12 @@ use actix_web::{web, HttpResponse};
|
||||
|
||||
#[derive(thiserror::Error, Debug)]
|
||||
enum AccommodationListControllerErr {
|
||||
#[error("Malformed name!")]
|
||||
MalformedName,
|
||||
#[error("Malformed description!")]
|
||||
MalformedDescription,
|
||||
#[error("Invalid name length!")]
|
||||
InvalidNameLength,
|
||||
#[error("Invalid description length!")]
|
||||
InvalidDescriptionLength,
|
||||
#[error("Malformed color!")]
|
||||
MalformedColor,
|
||||
}
|
||||
|
||||
#[derive(serde::Deserialize, Clone)]
|
||||
@@ -19,6 +21,7 @@ pub struct AccommodationRequest {
|
||||
pub name: String,
|
||||
pub need_validation: bool,
|
||||
pub description: Option<String>,
|
||||
pub color: Option<String>,
|
||||
pub open_to_reservations: bool,
|
||||
}
|
||||
|
||||
@@ -27,17 +30,24 @@ impl AccommodationRequest {
|
||||
let c = StaticConstraints::default();
|
||||
|
||||
if !c.accommodation_name_len.validate(&self.name) {
|
||||
return Err(AccommodationListControllerErr::MalformedName.into());
|
||||
return Err(AccommodationListControllerErr::InvalidNameLength.into());
|
||||
}
|
||||
accommodation.name = self.name;
|
||||
|
||||
if let Some(d) = &self.description {
|
||||
if !c.accommodation_description_len.validate(d) {
|
||||
return Err(AccommodationListControllerErr::MalformedDescription.into());
|
||||
return Err(AccommodationListControllerErr::InvalidDescriptionLength.into());
|
||||
}
|
||||
}
|
||||
accommodation.description.clone_from(&self.description);
|
||||
|
||||
if let Some(c) = &self.color {
|
||||
if !lazy_regex::regex!("[a-fA-F0-9]{6}").is_match(c) {
|
||||
return Err(AccommodationListControllerErr::MalformedColor.into());
|
||||
}
|
||||
}
|
||||
accommodation.color.clone_from(&self.color);
|
||||
|
||||
accommodation.need_validation = self.need_validation;
|
||||
accommodation.open_to_reservations = self.open_to_reservations;
|
||||
Ok(())
|
||||
|
@@ -459,6 +459,7 @@ pub struct Accommodation {
|
||||
pub name: String,
|
||||
pub need_validation: bool,
|
||||
pub description: Option<String>,
|
||||
pub color: Option<String>,
|
||||
pub open_to_reservations: bool,
|
||||
}
|
||||
|
||||
|
@@ -10,6 +10,8 @@ diesel::table! {
|
||||
name -> Varchar,
|
||||
need_validation -> Bool,
|
||||
description -> Nullable<Text>,
|
||||
#[max_length = 6]
|
||||
color -> Nullable<Varchar>,
|
||||
open_to_reservations -> Bool,
|
||||
}
|
||||
}
|
||||
|
@@ -71,6 +71,7 @@ pub async fn update(accommodation: &mut Accommodation) -> anyhow::Result<()> {
|
||||
accommodations_list::dsl::name.eq(accommodation.name.to_string()),
|
||||
accommodations_list::dsl::need_validation.eq(accommodation.need_validation),
|
||||
accommodations_list::dsl::description.eq(accommodation.description.clone()),
|
||||
accommodations_list::dsl::color.eq(accommodation.color.clone()),
|
||||
accommodations_list::dsl::open_to_reservations.eq(accommodation.open_to_reservations),
|
||||
))
|
||||
.execute(conn)
|
||||
|
Reference in New Issue
Block a user