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