|
1 | 1 | use chrono::NaiveDateTime; |
| 2 | +use diesel::prelude::*; |
| 3 | +use diesel::{ |
| 4 | + deserialize::{self, FromSql}, |
| 5 | + pg::Pg, |
| 6 | + serialize::{self, Output, ToSql}, |
| 7 | + sql_types::Integer, |
| 8 | +}; |
| 9 | +use std::io::Write; |
2 | 10 |
|
3 | 11 | use crate::models::{ApiToken, User, Version}; |
4 | 12 | use crate::schema::*; |
5 | 13 |
|
6 | | -#[derive(Debug, Clone, Copy)] |
7 | | -#[repr(u32)] |
| 14 | +#[derive(Debug, Clone, Copy, PartialEq, FromSqlRow, AsExpression)] |
| 15 | +#[repr(i32)] |
| 16 | +#[sql_type = "Integer"] |
8 | 17 | pub enum VersionAction { |
9 | 18 | Publish = 0, |
10 | 19 | Yank = 1, |
11 | 20 | Unyank = 2, |
12 | 21 | } |
13 | 22 |
|
| 23 | +impl FromSql<Integer, Pg> for VersionAction { |
| 24 | + fn from_sql(bytes: Option<&[u8]>) -> deserialize::Result<Self> { |
| 25 | + match <i32 as FromSql<Integer, Pg>>::from_sql(bytes)? { |
| 26 | + 0 => Ok(VersionAction::Publish), |
| 27 | + 1 => Ok(VersionAction::Yank), |
| 28 | + 2 => Ok(VersionAction::Unyank), |
| 29 | + n => Err(format!("unknown version action: {}", n).into()), |
| 30 | + } |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +impl ToSql<Integer, Pg> for VersionAction { |
| 35 | + fn to_sql<W: Write>(&self, out: &mut Output<'_, W, Pg>) -> serialize::Result { |
| 36 | + ToSql::<Integer, Pg>::to_sql(&(*self as i32), out) |
| 37 | + } |
| 38 | +} |
| 39 | + |
14 | 40 | #[derive(Debug, Clone, Copy, Queryable, Identifiable, Associations)] |
15 | 41 | #[belongs_to(Version)] |
16 | 42 | #[belongs_to(User, foreign_key = "owner_id")] |
17 | 43 | #[belongs_to(ApiToken, foreign_key = "owner_token_id")] |
18 | 44 | #[table_name = "version_owner_actions"] |
19 | 45 | pub struct VersionOwnerAction { |
20 | 46 | pub id: i32, |
| 47 | + pub version_id: Option<i32>, |
| 48 | + pub owner_id: Option<i32>, |
| 49 | + pub owner_token_id: Option<i32>, |
| 50 | + pub action: VersionAction, |
| 51 | + pub time: NaiveDateTime, |
| 52 | +} |
| 53 | + |
| 54 | +impl VersionOwnerAction { |
| 55 | + pub fn all(conn: &PgConnection) -> QueryResult<Vec<VersionOwnerAction>> { |
| 56 | + version_owner_actions::table.load(conn) |
| 57 | + } |
| 58 | + |
| 59 | + pub fn by_version_id_and_action(conn: &PgConnection, _version_id: i32, _action: VersionAction) -> QueryResult<Vec<VersionOwnerAction>> { |
| 60 | + use version_owner_actions::dsl::{version_id, action}; |
| 61 | + |
| 62 | + version_owner_actions::table.filter(version_id.eq(_version_id)).filter(action.eq(_action)).load(conn) |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +#[derive(Copy, Clone, Debug, Insertable)] |
| 67 | +#[table_name = "version_owner_actions"] |
| 68 | +pub struct NewVersionOwnerAction { |
21 | 69 | pub version_id: i32, |
22 | 70 | pub owner_id: i32, |
23 | | - pub owner_token_id: i32, |
| 71 | + pub owner_token_id: Option<i32>, |
24 | 72 | pub action: VersionAction, |
25 | | - pub time: NaiveDateTime, |
| 73 | +} |
| 74 | + |
| 75 | +impl NewVersionOwnerAction { |
| 76 | + pub fn new( |
| 77 | + version_id: i32, |
| 78 | + owner_id: i32, |
| 79 | + owner_token_id: Option<i32>, |
| 80 | + action: VersionAction, |
| 81 | + ) -> Self { |
| 82 | + Self { |
| 83 | + version_id, |
| 84 | + owner_id, |
| 85 | + owner_token_id, |
| 86 | + action, |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + pub fn save(&self, conn: &PgConnection) -> QueryResult<VersionOwnerAction> { |
| 91 | + diesel::insert_into(version_owner_actions::table) |
| 92 | + .values(self) |
| 93 | + .get_result(conn) |
| 94 | + } |
26 | 95 | } |
0 commit comments