bitque/jirs-data/src/lib.rs

420 lines
11 KiB
Rust
Raw Normal View History

2020-03-31 22:05:18 +02:00
use std::str::FromStr;
2020-03-28 21:41:16 +01:00
use chrono::NaiveDateTime;
2020-04-01 13:29:43 +02:00
#[cfg(feature = "backend")]
use diesel::*;
2020-03-28 21:41:16 +01:00
use serde::{Deserialize, Serialize};
use uuid::Uuid;
2020-04-01 13:29:43 +02:00
#[cfg(feature = "backend")]
pub use sql::*;
#[cfg(feature = "backend")]
pub mod sql;
#[cfg_attr(feature = "backend", derive(FromSqlRow, AsExpression))]
#[cfg_attr(feature = "backend", sql_type = "IssueTypeType")]
2020-04-07 20:32:21 +02:00
#[derive(Clone, Deserialize, Serialize, Debug, PartialOrd, PartialEq, Hash)]
2020-03-31 14:28:30 +02:00
#[serde(rename_all = "lowercase")]
pub enum IssueType {
Task,
Bug,
Story,
}
2020-04-04 17:42:02 +02:00
impl Default for IssueType {
fn default() -> Self {
IssueType::Task
}
}
2020-04-01 22:41:26 +02:00
impl IssueType {
pub fn to_label(&self) -> &str {
match self {
IssueType::Task => "Task",
IssueType::Bug => "Bug",
IssueType::Story => "Story",
2020-03-31 14:28:30 +02:00
}
}
}
2020-04-02 08:45:43 +02:00
impl Into<u32> for IssueType {
fn into(self) -> u32 {
match self {
IssueType::Task => 1,
IssueType::Bug => 2,
IssueType::Story => 3,
}
}
}
impl Into<IssueType> for u32 {
fn into(self) -> IssueType {
match self {
1 => IssueType::Task,
2 => IssueType::Bug,
3 => IssueType::Story,
_ => IssueType::Task,
}
}
}
2020-04-01 13:29:43 +02:00
impl std::fmt::Display for IssueType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
2020-03-31 14:28:30 +02:00
match self {
2020-04-01 13:29:43 +02:00
IssueType::Task => f.write_str("task"),
IssueType::Bug => f.write_str("bug"),
IssueType::Story => f.write_str("story"),
2020-03-31 14:28:30 +02:00
}
}
}
2020-04-02 16:14:07 +02:00
#[cfg_attr(feature = "backend", derive(FromSqlRow, AsExpression))]
#[cfg_attr(feature = "backend", sql_type = "IssueStatusType")]
2020-04-07 20:32:21 +02:00
#[derive(Clone, Deserialize, Serialize, Debug, PartialOrd, PartialEq, Hash)]
2020-04-02 16:14:07 +02:00
#[serde(rename_all = "lowercase")]
2020-03-31 14:28:30 +02:00
pub enum IssueStatus {
Backlog,
Selected,
InProgress,
Done,
}
2020-04-04 17:42:02 +02:00
impl Default for IssueStatus {
fn default() -> Self {
IssueStatus::Backlog
}
}
2020-03-31 23:49:46 +02:00
impl FromStr for IssueStatus {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"backlog" => Ok(IssueStatus::Backlog),
"selected" => Ok(IssueStatus::Selected),
"in_progress" => Ok(IssueStatus::InProgress),
"done" => Ok(IssueStatus::Done),
_ => Err(format!("Invalid status {:?}", s)),
}
}
}
2020-03-31 14:28:30 +02:00
impl IssueStatus {
pub fn to_label(&self) -> &str {
match self {
IssueStatus::Backlog => "Backlog",
IssueStatus::Selected => "Selected for development",
IssueStatus::InProgress => "In Progress",
IssueStatus::Done => "Done",
}
}
pub fn to_payload(&self) -> &str {
match self {
IssueStatus::Backlog => "backlog",
IssueStatus::Selected => "selected",
IssueStatus::InProgress => "in_progress",
IssueStatus::Done => "done",
}
}
pub fn match_name(&self, name: &str) -> bool {
2020-03-31 23:49:46 +02:00
self.to_payload() == name
2020-03-31 14:28:30 +02:00
}
}
2020-04-01 13:29:43 +02:00
#[cfg_attr(feature = "backend", derive(FromSqlRow, AsExpression))]
#[cfg_attr(feature = "backend", sql_type = "IssuePriorityType")]
2020-04-07 20:32:21 +02:00
#[derive(Clone, Deserialize, Serialize, Debug, PartialOrd, PartialEq, Hash)]
2020-03-31 14:28:30 +02:00
pub enum IssuePriority {
Highest,
High,
Medium,
Low,
Lowest,
}
impl FromStr for IssuePriority {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
2020-03-31 22:05:18 +02:00
match s.to_lowercase().trim() {
"5" | "highest" => Ok(IssuePriority::Highest),
"4" | "high" => Ok(IssuePriority::High),
"3" | "medium" => Ok(IssuePriority::Medium),
"2" | "low" => Ok(IssuePriority::Low),
"1" | "lowest" => Ok(IssuePriority::Lowest),
2020-03-31 14:28:30 +02:00
_ => Err(format!("Unknown priority {}", s)),
}
}
}
2020-04-04 17:42:02 +02:00
impl Default for IssuePriority {
fn default() -> Self {
IssuePriority::Medium
}
}
2020-04-01 13:29:43 +02:00
impl std::fmt::Display for IssuePriority {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
IssuePriority::Highest => f.write_str("highest"),
IssuePriority::High => f.write_str("high"),
IssuePriority::Medium => f.write_str("medium"),
IssuePriority::Low => f.write_str("low"),
IssuePriority::Lowest => f.write_str("lowest"),
}
}
}
2020-04-08 16:14:59 +02:00
impl Into<u32> for IssuePriority {
fn into(self) -> u32 {
2020-03-31 14:28:30 +02:00
match self {
IssuePriority::Highest => 5,
IssuePriority::High => 4,
IssuePriority::Medium => 3,
IssuePriority::Low => 2,
IssuePriority::Lowest => 1,
}
}
}
2020-04-08 16:14:59 +02:00
impl Into<IssuePriority> for u32 {
fn into(self) -> IssuePriority {
match self {
5 => IssuePriority::Highest,
4 => IssuePriority::High,
3 => IssuePriority::Medium,
2 => IssuePriority::Low,
1 => IssuePriority::Lowest,
_ => IssuePriority::Medium,
}
}
}
2020-04-08 20:26:28 +02:00
#[derive(Clone, Serialize, Debug, PartialEq)]
2020-03-28 21:41:16 +01:00
#[serde(rename_all = "camelCase")]
pub struct ErrorResponse {
pub errors: Vec<String>,
}
2020-04-08 20:26:28 +02:00
#[derive(Clone, Serialize, Deserialize, Debug, PartialEq)]
2020-03-28 21:41:16 +01:00
#[serde(rename_all = "camelCase")]
pub struct FullProject {
pub id: i32,
pub name: String,
pub url: String,
pub description: String,
pub category: String,
pub created_at: NaiveDateTime,
pub updated_at: NaiveDateTime,
pub issues: Vec<Issue>,
pub users: Vec<User>,
}
2020-04-08 20:26:28 +02:00
#[derive(Clone, Serialize, Deserialize, Debug, PartialEq)]
2020-03-28 21:41:16 +01:00
#[serde(rename_all = "camelCase")]
pub struct FullIssue {
pub id: i32,
pub title: String,
#[serde(rename = "type")]
2020-04-01 13:29:43 +02:00
pub issue_type: IssueType,
2020-04-02 16:14:07 +02:00
pub status: IssueStatus,
2020-04-01 13:29:43 +02:00
pub priority: IssuePriority,
2020-03-28 21:41:16 +01:00
pub list_position: f64,
pub description: Option<String>,
pub description_text: Option<String>,
pub estimate: Option<i32>,
pub time_spent: Option<i32>,
pub time_remaining: Option<i32>,
pub reporter_id: i32,
pub project_id: i32,
pub created_at: NaiveDateTime,
pub updated_at: NaiveDateTime,
pub user_ids: Vec<i32>,
pub comments: Vec<Comment>,
}
2020-04-02 16:14:07 +02:00
impl Into<Issue> for FullIssue {
fn into(self) -> Issue {
Issue {
id: self.id,
title: self.title,
issue_type: self.issue_type,
status: self.status,
priority: self.priority,
list_position: self.list_position,
description: self.description,
description_text: self.description_text,
estimate: self.estimate,
time_spent: self.time_spent,
time_remaining: self.time_remaining,
reporter_id: self.reporter_id,
project_id: self.project_id,
created_at: self.created_at,
updated_at: self.updated_at,
user_ids: self.user_ids,
}
2020-03-28 21:41:16 +01:00
}
}
2020-04-08 20:26:28 +02:00
#[derive(Clone, Serialize, Deserialize, Debug, PartialEq)]
2020-03-28 21:41:16 +01:00
#[serde(rename_all = "camelCase")]
pub struct Project {
pub id: i32,
pub name: String,
pub url: String,
pub description: String,
pub category: String,
pub created_at: NaiveDateTime,
pub updated_at: NaiveDateTime,
}
2020-04-08 20:26:28 +02:00
#[derive(Clone, Serialize, Deserialize, Debug, PartialEq)]
2020-03-28 21:41:16 +01:00
#[serde(rename_all = "camelCase")]
pub struct Issue {
pub id: i32,
pub title: String,
#[serde(rename = "type")]
2020-04-01 13:29:43 +02:00
pub issue_type: IssueType,
2020-03-31 23:49:46 +02:00
pub status: IssueStatus,
2020-04-01 13:29:43 +02:00
pub priority: IssuePriority,
2020-03-28 21:41:16 +01:00
pub list_position: f64,
pub description: Option<String>,
pub description_text: Option<String>,
pub estimate: Option<i32>,
pub time_spent: Option<i32>,
pub time_remaining: Option<i32>,
pub reporter_id: i32,
pub project_id: i32,
pub created_at: NaiveDateTime,
pub updated_at: NaiveDateTime,
pub user_ids: Vec<i32>,
}
2020-04-08 20:26:28 +02:00
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
2020-03-28 21:41:16 +01:00
#[serde(rename_all = "camelCase")]
pub struct Comment {
pub id: i32,
pub body: String,
pub user_id: i32,
pub issue_id: i32,
pub created_at: NaiveDateTime,
pub updated_at: NaiveDateTime,
pub user: Option<User>,
}
2020-04-08 08:58:02 +02:00
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
2020-03-28 21:41:16 +01:00
#[serde(rename_all = "camelCase")]
pub struct User {
pub id: i32,
pub name: String,
pub email: String,
pub avatar_url: Option<String>,
pub project_id: i32,
pub created_at: NaiveDateTime,
pub updated_at: NaiveDateTime,
}
2020-04-08 20:26:28 +02:00
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
2020-03-28 21:41:16 +01:00
#[serde(rename_all = "camelCase")]
pub struct Token {
pub id: i32,
pub user_id: i32,
pub access_token: Uuid,
pub refresh_token: Uuid,
pub created_at: NaiveDateTime,
pub updated_at: NaiveDateTime,
}
2020-04-08 20:26:28 +02:00
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
2020-03-28 21:41:16 +01:00
#[serde(rename_all = "camelCase")]
pub struct UpdateIssuePayload {
pub title: Option<String>,
#[serde(rename = "type")]
2020-04-01 13:29:43 +02:00
pub issue_type: Option<IssueType>,
2020-04-02 16:14:07 +02:00
pub status: Option<IssueStatus>,
2020-04-01 13:29:43 +02:00
pub priority: Option<IssuePriority>,
2020-03-28 21:41:16 +01:00
pub list_position: Option<f64>,
pub description: Option<Option<String>>,
pub description_text: Option<Option<String>>,
pub estimate: Option<Option<i32>>,
pub time_spent: Option<Option<i32>>,
pub time_remaining: Option<Option<i32>>,
pub project_id: Option<i32>,
pub user_ids: Option<Vec<i32>>,
}
2020-03-29 19:56:55 +02:00
2020-04-08 20:26:28 +02:00
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
2020-03-29 19:56:55 +02:00
#[serde(rename_all = "camelCase")]
pub struct CreateCommentPayload {
pub user_id: Option<i32>,
pub issue_id: i32,
pub body: String,
}
2020-04-08 20:26:28 +02:00
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
2020-03-29 19:56:55 +02:00
#[serde(rename_all = "camelCase")]
pub struct UpdateCommentPayload {
pub body: String,
}
2020-04-08 20:26:28 +02:00
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
2020-03-29 19:56:55 +02:00
#[serde(rename_all = "camelCase")]
pub struct CreateIssuePayload {
pub title: String,
#[serde(rename = "type")]
2020-04-01 13:29:43 +02:00
pub issue_type: IssueType,
2020-04-02 16:14:07 +02:00
pub status: IssueStatus,
2020-04-01 13:29:43 +02:00
pub priority: IssuePriority,
2020-03-29 19:56:55 +02:00
pub description: Option<String>,
pub description_text: Option<String>,
pub estimate: Option<i32>,
pub time_spent: Option<i32>,
pub time_remaining: Option<i32>,
pub project_id: i32,
pub user_ids: Vec<i32>,
2020-04-08 20:26:28 +02:00
pub reporter_id: i32,
2020-03-29 19:56:55 +02:00
}
2020-04-08 20:26:28 +02:00
#[derive(Serialize, Deserialize, Debug, PartialEq)]
2020-03-29 19:56:55 +02:00
#[serde(rename_all = "camelCase")]
pub struct UpdateProjectPayload {
pub name: Option<String>,
pub url: Option<String>,
pub description: Option<String>,
pub category: Option<String>,
}
2020-04-05 15:15:09 +02:00
2020-04-08 20:26:28 +02:00
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
2020-04-05 15:15:09 +02:00
pub enum WsMsg {
Ping,
Pong,
2020-04-07 14:46:56 +02:00
// auth
AuthorizeRequest(Uuid),
AuthorizeLoaded(Result<User, String>),
2020-04-07 14:46:56 +02:00
AuthorizeExpired,
// project page
2020-04-06 08:38:08 +02:00
ProjectRequest,
ProjectLoaded(Project),
2020-04-06 22:59:33 +02:00
ProjectIssuesRequest,
2020-04-06 08:38:08 +02:00
ProjectIssuesLoaded(Vec<Issue>),
2020-04-06 22:59:33 +02:00
ProjectUsersRequest,
ProjectUsersLoaded(Vec<User>),
2020-04-07 14:46:56 +02:00
// issue
2020-04-07 16:02:13 +02:00
IssueUpdateRequest(i32, UpdateIssuePayload),
2020-04-07 14:46:56 +02:00
IssueUpdated(Issue),
IssueDeleteRequest(i32),
IssueDeleted(i32),
IssueCreateRequest(CreateIssuePayload),
IssueCreated(Issue),
2020-04-05 15:15:09 +02:00
}