| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | use sqlx::Type; |
| 12 | use uuid::Uuid; |
| 13 | |
| 14 | use crate::constant::{ |
| 15 | MAX_DISH_DESCRIPTION_LEN, MAX_DISH_NAME_LEN, MAX_DISH_PRICE_MINOR, MAX_DISH_TAGS, |
| 16 | }; |
| 17 | use crate::domain::menu::MenuId; |
| 18 | use crate::domain::monotonic_id::next_uuid; |
| 19 | use crate::domain::timestamp::Timestamp; |
| 20 | use crate::error::ValidationError; |
| 21 | use crate::validate::{validate_optional, validate_required}; |
| 22 | |
| 23 | |
| 24 | |
| 25 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Type)] |
| 26 | #[sqlx(transparent)] |
| 27 | pub struct MenuDishId(Uuid); |
| 28 | |
| 29 | impl MenuDishId { |
| 30 | |
| 31 | |
| 32 | |
| 33 | pub fn generate() -> Self { |
| 34 | Self(next_uuid()) |
| 35 | } |
| 36 | |
| 37 | |
| 38 | |
| 39 | pub fn uuid(&self) -> Uuid { |
| 40 | self.0 |
| 41 | } |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | pub fn from_key(key: &str) -> Self { |
| 48 | Self(Uuid::parse_str(key).unwrap_or(Uuid::nil())) |
| 49 | } |
| 50 | |
| 51 | |
| 52 | pub fn key(&self) -> String { |
| 53 | self.0.to_string() |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | #[derive(Debug, Clone, PartialEq, Eq, Type)] |
| 58 | #[sqlx(transparent)] |
| 59 | pub struct DishName(String); |
| 60 | |
| 61 | impl DishName { |
| 62 | pub fn try_new(value: &str) -> Result<Self, ValidationError> { |
| 63 | validate_required("name", value, MAX_DISH_NAME_LEN)?; |
| 64 | Ok(Self(value.to_string())) |
| 65 | } |
| 66 | |
| 67 | pub fn as_str(&self) -> &str { |
| 68 | &self.0 |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | #[derive(Debug, Clone, PartialEq, Eq, Type)] |
| 73 | #[sqlx(transparent)] |
| 74 | pub struct DishDescription(String); |
| 75 | |
| 76 | impl DishDescription { |
| 77 | |
| 78 | |
| 79 | pub fn try_new(value: &str) -> Result<Option<Self>, ValidationError> { |
| 80 | validate_optional("description", value, MAX_DISH_DESCRIPTION_LEN)?; |
| 81 | let value = value.trim(); |
| 82 | Ok((!value.is_empty()).then(|| Self(value.to_string()))) |
| 83 | } |
| 84 | |
| 85 | pub fn as_str(&self) -> &str { |
| 86 | &self.0 |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | |
| 91 | |
| 92 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Type)] |
| 93 | #[sqlx(transparent)] |
| 94 | pub struct DishPrice(i64); |
| 95 | |
| 96 | impl DishPrice { |
| 97 | pub fn try_new(value: i64) -> Result<Self, ValidationError> { |
| 98 | if !(0..=MAX_DISH_PRICE_MINOR).contains(&value) { |
| 99 | return Err(ValidationError::Invalid { |
| 100 | field: "price_minor", |
| 101 | reason: "must be between 0 and 1000000 minor units", |
| 102 | }); |
| 103 | } |
| 104 | Ok(Self(value)) |
| 105 | } |
| 106 | |
| 107 | pub fn as_minor(self) -> i64 { |
| 108 | self.0 |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | #[derive(Debug, Clone, PartialEq, Eq, Type)] |
| 118 | #[sqlx(transparent, no_pg_array)] |
| 119 | pub struct DishTags(Vec<String>); |
| 120 | |
| 121 | impl DishTags { |
| 122 | pub fn try_new(values: &[String], allowed: &[String]) -> Result<Self, ValidationError> { |
| 123 | let mut tags: Vec<String> = Vec::new(); |
| 124 | for value in values { |
| 125 | let value = value.trim(); |
| 126 | if !allowed.iter().any(|tag| tag == value) { |
| 127 | return Err(ValidationError::Invalid { |
| 128 | field: "tags", |
| 129 | reason: "not one of the school's dietary tags (see GET /settings)", |
| 130 | }); |
| 131 | } |
| 132 | if !tags.iter().any(|tag| tag == value) { |
| 133 | tags.push(value.to_string()); |
| 134 | } |
| 135 | } |
| 136 | if tags.len() > MAX_DISH_TAGS { |
| 137 | return Err(ValidationError::Invalid { |
| 138 | field: "tags", |
| 139 | reason: "at most 10 tags per dish", |
| 140 | }); |
| 141 | } |
| 142 | Ok(Self(tags)) |
| 143 | } |
| 144 | |
| 145 | pub fn as_slice(&self) -> &[String] { |
| 146 | &self.0 |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | #[derive(Debug, Clone, sqlx::FromRow)] |
| 151 | pub struct MenuDish { |
| 152 | pub(crate) id: MenuDishId, |
| 153 | pub(crate) menu: MenuId, |
| 154 | pub(crate) name: DishName, |
| 155 | pub(crate) description: Option<DishDescription>, |
| 156 | pub(crate) price_minor: DishPrice, |
| 157 | pub(crate) tags: DishTags, |
| 158 | pub(crate) created_at: Timestamp, |
| 159 | } |
| 160 | |
| 161 | impl MenuDish { |
| 162 | pub fn get_id(&self) -> &MenuDishId { |
| 163 | &self.id |
| 164 | } |
| 165 | |
| 166 | pub fn get_menu(&self) -> &MenuId { |
| 167 | &self.menu |
| 168 | } |
| 169 | |
| 170 | pub fn get_name(&self) -> &DishName { |
| 171 | &self.name |
| 172 | } |
| 173 | |
| 174 | pub fn get_description(&self) -> Option<&DishDescription> { |
| 175 | self.description.as_ref() |
| 176 | } |
| 177 | |
| 178 | pub fn get_price_minor(&self) -> DishPrice { |
| 179 | self.price_minor |
| 180 | } |
| 181 | |
| 182 | pub fn get_tags(&self) -> &DishTags { |
| 183 | &self.tags |
| 184 | } |
| 185 | |
| 186 | pub fn get_created_at(&self) -> Timestamp { |
| 187 | self.created_at |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | #[cfg(test)] |
| 192 | mod tests { |
| 193 | use super::*; |
| 194 | |
| 195 | #[test] |
| 196 | fn price_is_bounded_non_negative_minor_units() { |
| 197 | assert_eq!(DishPrice::try_new(0).unwrap().as_minor(), 0); |
| 198 | assert_eq!(DishPrice::try_new(4550).unwrap().as_minor(), 4550); |
| 199 | assert!(DishPrice::try_new(-1).is_err()); |
| 200 | assert!(DishPrice::try_new(MAX_DISH_PRICE_MINOR).is_ok()); |
| 201 | assert!(DishPrice::try_new(MAX_DISH_PRICE_MINOR + 1).is_err()); |
| 202 | } |
| 203 | |
| 204 | #[test] |
| 205 | fn tags_must_come_from_the_school_list_and_dedupe() { |
| 206 | let allowed = vec!["vegan".to_string(), "nut_allergy".to_string()]; |
| 207 | let tags = |
| 208 | DishTags::try_new(&["vegan".to_string(), "vegan".to_string()], &allowed).unwrap(); |
| 209 | assert_eq!(tags.as_slice(), ["vegan"]); |
| 210 | assert!(DishTags::try_new(&["halal".to_string()], &allowed).is_err()); |
| 211 | assert!( |
| 212 | DishTags::try_new(&[], &allowed) |
| 213 | .unwrap() |
| 214 | .as_slice() |
| 215 | .is_empty() |
| 216 | ); |
| 217 | } |
| 218 | } |