| 1 | use uuid::Uuid; |
| 2 | |
| 3 | use crate::constant::MAX_SOLUTION_BODY_LEN; |
| 4 | use crate::domain::monotonic_id::next_uuid; |
| 5 | use crate::domain::note_file::FileContentType; |
| 6 | use crate::domain::pool_question::PoolQuestionId; |
| 7 | use crate::domain::timestamp::Timestamp; |
| 8 | use crate::domain::user::UserId; |
| 9 | use crate::error::ValidationError; |
| 10 | use crate::validate::validate_required; |
| 11 | |
| 12 | |
| 13 | |
| 14 | #[derive(Debug, Clone, Copy, PartialEq, Eq, sqlx::Type)] |
| 15 | #[sqlx(transparent)] |
| 16 | pub struct SolutionId(Uuid); |
| 17 | |
| 18 | impl SolutionId { |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | pub fn generate() -> Self { |
| 24 | Self(next_uuid()) |
| 25 | } |
| 26 | |
| 27 | |
| 28 | |
| 29 | pub fn uuid(&self) -> Uuid { |
| 30 | self.0 |
| 31 | } |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | pub fn from_key(key: &str) -> Self { |
| 38 | Self(Uuid::parse_str(key).unwrap_or(Uuid::nil())) |
| 39 | } |
| 40 | |
| 41 | |
| 42 | pub fn key(&self) -> String { |
| 43 | self.0.to_string() |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | #[derive(Debug, Clone, PartialEq, Eq, sqlx::Type)] |
| 48 | #[sqlx(transparent)] |
| 49 | pub struct SolutionBody(String); |
| 50 | |
| 51 | impl SolutionBody { |
| 52 | pub fn try_new(value: &str) -> Result<Self, ValidationError> { |
| 53 | validate_required("body", value, MAX_SOLUTION_BODY_LEN)?; |
| 54 | Ok(Self(value.to_string())) |
| 55 | } |
| 56 | |
| 57 | pub fn as_str(&self) -> &str { |
| 58 | &self.0 |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | #[derive(Debug, Clone, sqlx::FromRow)] |
| 63 | pub struct Solution { |
| 64 | pub(crate) id: SolutionId, |
| 65 | pub(crate) question: PoolQuestionId, |
| 66 | pub(crate) author: UserId, |
| 67 | pub(crate) body: SolutionBody, |
| 68 | pub(crate) offered_at: Timestamp, |
| 69 | |
| 70 | |
| 71 | pub(crate) image_file: Option<String>, |
| 72 | pub(crate) image_content_type: Option<FileContentType>, |
| 73 | pub(crate) image_size: Option<i64>, |
| 74 | } |
| 75 | |
| 76 | impl Solution { |
| 77 | pub fn new(question: &PoolQuestionId, author: &UserId, body: SolutionBody) -> Self { |
| 78 | Self { |
| 79 | id: SolutionId::generate(), |
| 80 | question: question.clone(), |
| 81 | author: author.clone(), |
| 82 | body, |
| 83 | offered_at: Timestamp::now(), |
| 84 | image_file: None, |
| 85 | image_content_type: None, |
| 86 | image_size: None, |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | pub fn get_id(&self) -> &SolutionId { |
| 91 | &self.id |
| 92 | } |
| 93 | |
| 94 | pub fn get_question(&self) -> &PoolQuestionId { |
| 95 | &self.question |
| 96 | } |
| 97 | |
| 98 | pub fn get_author(&self) -> &UserId { |
| 99 | &self.author |
| 100 | } |
| 101 | |
| 102 | pub fn get_body(&self) -> &SolutionBody { |
| 103 | &self.body |
| 104 | } |
| 105 | |
| 106 | pub fn get_offered_at(&self) -> Timestamp { |
| 107 | self.offered_at |
| 108 | } |
| 109 | |
| 110 | pub fn get_image_file(&self) -> Option<&str> { |
| 111 | self.image_file.as_deref() |
| 112 | } |
| 113 | |
| 114 | pub fn get_image_content_type(&self) -> Option<&FileContentType> { |
| 115 | self.image_content_type.as_ref() |
| 116 | } |
| 117 | |
| 118 | pub fn get_image_size(&self) -> Option<i64> { |
| 119 | self.image_size |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | #[cfg(test)] |
| 124 | mod tests { |
| 125 | use super::*; |
| 126 | |
| 127 | #[test] |
| 128 | fn body_rules() { |
| 129 | assert!(SolutionBody::try_new("").is_err()); |
| 130 | assert!(SolutionBody::try_new(" ").is_err()); |
| 131 | assert!(SolutionBody::try_new(&"x".repeat(10_001)).is_err()); |
| 132 | assert_eq!( |
| 133 | SolutionBody::try_new("Kısmi integrasyon.") |
| 134 | .unwrap() |
| 135 | .as_str(), |
| 136 | "Kısmi integrasyon." |
| 137 | ); |
| 138 | } |
| 139 | } |