1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
use serde::{Deserialize, Serialize};
use uuid::Uuid;

#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub enum PollStatus {
  Pending,
  Active,
  Complete
}

impl std::str::FromStr for PollStatus {
  type Err = anyhow::Error;

  fn from_str(s: &str) -> anyhow::Result<Self> {
    match s {
      "Pending" => Ok(Self::Pending),
      "Active" => Ok(Self::Active),
      "Complete" => Ok(Self::Complete),
      _ => Err(anyhow::anyhow!("Invalid theme [{}]", s))
    }
  }
}

impl std::fmt::Display for PollStatus {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    let s = match self {
      Self::Pending => "Pending",
      Self::Active => "Active",
      Self::Complete => "Complete"
    };
    write!(f, "{}", s)
  }
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Poll {
  id: Uuid,
  idx: u32,
  author_id: Uuid,
  title: String,
  status: PollStatus,
  final_vote: Option<String>
}

impl Poll {
  pub const fn new(id: Uuid, idx: u32, author_id: Uuid, title: String, status: PollStatus) -> Self {
    Self {
      id,
      idx,
      author_id,
      title,
      status,
      final_vote: None
    }
  }

  pub const fn id(&self) -> &Uuid {
    &self.id
  }

  pub const fn idx(&self) -> u32 {
    self.idx
  }

  pub const fn title(&self) -> &String {
    &self.title
  }

  pub fn set_title(&mut self, t: String) {
    self.title = t;
  }

  pub const fn status(&self) -> &PollStatus {
    &self.status
  }

  pub fn set_status(&mut self, s: PollStatus) {
    self.status = s;
  }
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub enum PollActionType {
  UpdateTitle,
  StatusChange,
  CastVote
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct PollAction {
  id: Uuid,
  poll_id: Uuid,
  user_id: Uuid,
  t: PollActionType,
  ctx: std::collections::HashMap<String, String>,
  message: String
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Vote {
  poll_id: Uuid,
  user_id: Uuid,
  choice: String
}

impl Vote {
  pub const fn new(poll_id: Uuid, user_id: Uuid, choice: String) -> Self {
    Self { poll_id, user_id, choice }
  }

  pub const fn poll_id(&self) -> &Uuid {
    &self.poll_id
  }

  pub const fn user_id(&self) -> &Uuid {
    &self.user_id
  }

  pub const fn choice(&self) -> &String {
    &self.choice
  }

  pub fn set_choice(&mut self, c: String) {
    self.choice = c;
  }
}