Implemented public part and creating reservations.

This commit is contained in:
2024-02-01 09:18:00 +01:00
parent 0d4b4a0b3d
commit 6c7fd2e46f
16 changed files with 930 additions and 103 deletions
+191 -34
View File
@@ -1,14 +1,13 @@
//use chrono::{NaiveDate, NaiveTime, Weekday};
//use rust_decimal::Decimal;
#![allow(unused_variables)]
use std::fmt::Display;
use chrono::{NaiveTime, Weekday};
use std::str::FromStr;
use chrono::{Local, NaiveDate, NaiveTime, Weekday};
use lazy_static::lazy_static;
use regex::Regex;
use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};
//use uuid::Uuid;
use uuid::Uuid;
use validator::{Validate, ValidationError};
use crate::error::AppError;
@@ -329,6 +328,193 @@ impl ResProperty {
}
}
#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Eq, Default)]
pub struct PublicFormData {
pub property: ResProperty,
pub hours: Vec<DayHour>,
pub reservations: Vec<Reservation>
}
fn empty_slots() -> Vec<String> {
vec![]
}
fn validate_date(date: &NaiveDate) -> Result<(), ValidationError> {
if date < &Local::now().date_naive() {
Err(ValidationError::new("date_in_past"))
} else {
Ok(())
}
}
#[derive(Clone, Serialize, Deserialize, Debug, Validate)]
pub struct CrReservation {
#[validate(custom(function = "validate_date", message = "Date can't be in past"))]
date: NaiveDate,
#[validate(length(min = 1,message = "Select at last one time slot"))]
#[serde(default = "empty_slots")]
slots: Vec<String>,
#[validate(length(min = 1,message = "Enter your full name"))]
full_name: String,
#[validate(email(message = "Enter valid email address"))]
email: String,
#[validate(length(min = 1,message = "Enter your phone number"))]
phone: String,
note: String
}
#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
pub struct TmCheck {
pub from: NaiveTime,
pub to: NaiveTime
}
impl FromStr for TmCheck {
type Err = AppError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let times = s.split("-").collect::<Vec<_>>();
if times.len() != 2 {
return Err(AppError::HourParseError);
}
Ok(TmCheck{
from: NaiveTime::from_str(times.get(0).unwrap_or(&""))?,
to: NaiveTime::from_str(times.get(1).unwrap_or(&""))?
})
}
}
impl CrReservation {
pub fn date(&self) -> NaiveDate {
self.date
}
pub fn slots(&self) -> &Vec<String> {
&self.slots
}
pub fn full_name(&self) -> &str {
&self.full_name
}
pub fn email(&self) -> &str {
&self.email
}
pub fn phone(&self) -> &str {
&self.phone
}
pub fn note(&self) -> &str {
&self.note
}
}
#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Eq, Default)]
#[cfg_attr(feature = "ssr", derive(sqlx::FromRow))]
pub struct Customer {
id: i32,
pub full_name: String,
pub email: String,
pub phone: String,
pub discount: i32
}
impl Customer {
pub fn id(&self) -> i32 {
self.id
}
pub fn new(id: i32, full_name: String, email: String, phone: String, discount: i32) -> Self {
Self { id, full_name, email, phone, discount }
}
}
#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Eq, Default)]
#[cfg_attr(feature = "ssr", derive(sqlx::Type))]
#[cfg_attr(feature = "ssr", sqlx(type_name = "reservation_state"))]
pub enum ReservationState {
#[default]
New,
Approved,
Canceled,
}
#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Eq, Default)]
#[cfg_attr(feature = "ssr", derive(sqlx::FromRow))]
pub struct Reservation {
id: i32,
pub from: NaiveTime,
pub to: NaiveTime,
pub property: i32,
pub summary: i32,
}
pub struct Reservations(Vec<Reservation>);
// Transform slots to reservations
impl Reservations {
pub fn new() -> Self {
Reservations(vec![])
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
pub fn add_slot(&mut self, check: &TmCheck, property: i32) {
let mut added = false;
self.0.iter_mut().for_each(|r| {
if r.property == property && r.to == check.from {
r.to = check.to;
added = true;
}
});
if !added {
self.0.push(Reservation {
from: check.from,
to: check.to,
property,
id: 0,
summary: 0,
});
}
}
pub fn reservations(&self) -> &Vec<Reservation> {
&self.0
}
}
impl Reservation {
pub fn new(from: NaiveTime, to: NaiveTime, property: i32) -> Self {
Self { id: 0, from, to, property, summary: 0 }
}
pub fn id(&self) -> i32 {
self.id
}
}
#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Eq, Default)]
#[cfg_attr(feature = "ssr", derive(sqlx::FromRow))]
pub struct ReservationSum {
id: i32,
pub uuid: Uuid,
pub date: NaiveDate,
pub customer: i32,
pub price: Decimal,
pub state: ReservationState,
pub date_create: NaiveDate,
pub edited_by: Option<i32>,
pub note: Option<String>,
}
impl ReservationSum {
pub fn id(&self) -> i32 {
self.id
}
}
/*
pub enum MessageType {
NewReservation,
@@ -344,33 +530,4 @@ pub struct Message {
text: String,
}
pub struct Customer {
id: u128,
full_name: String,
email: String,
phone: String,
discount: u8
}
pub enum ReservationState {
New,
Approved,
Canceled,
}
pub struct Reservation {
id: u128,
from: NaiveTime,
to: NaiveTime,
property: Property,
}
pub struct ReservationSum {
id: u128,
uuid: Uuid,
date: NaiveDate,
items: Vec<Reservation>,
customer: Customer,
price: Decimal,
state: ReservationState,
}*/
*/