|
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( |
| 60 | + conn: &PgConnection, |
| 61 | + _version_id: i32, |
| 62 | + _action: VersionAction, |
| 63 | + ) -> QueryResult<Vec<VersionOwnerAction>> { |
| 64 | + use version_owner_actions::dsl::{action, version_id}; |
| 65 | + |
| 66 | + version_owner_actions::table |
| 67 | + .filter(version_id.eq(_version_id)) |
| 68 | + .filter(action.eq(_action)) |
| 69 | + .load(conn) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +#[derive(Copy, Clone, Debug, Insertable)] |
| 74 | +#[table_name = "version_owner_actions"] |
| 75 | +pub struct NewVersionOwnerAction { |
21 | 76 | pub version_id: i32,
|
22 | 77 | pub owner_id: i32,
|
23 |
| - pub owner_token_id: i32, |
| 78 | + pub owner_token_id: Option<i32>, |
24 | 79 | pub action: VersionAction,
|
25 |
| - pub time: NaiveDateTime, |
| 80 | +} |
| 81 | + |
| 82 | +impl NewVersionOwnerAction { |
| 83 | + pub fn new( |
| 84 | + version_id: i32, |
| 85 | + owner_id: i32, |
| 86 | + owner_token_id: Option<i32>, |
| 87 | + action: VersionAction, |
| 88 | + ) -> Self { |
| 89 | + Self { |
| 90 | + version_id, |
| 91 | + owner_id, |
| 92 | + owner_token_id, |
| 93 | + action, |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + pub fn save(&self, conn: &PgConnection) -> QueryResult<VersionOwnerAction> { |
| 98 | + diesel::insert_into(version_owner_actions::table) |
| 99 | + .values(self) |
| 100 | + .get_result(conn) |
| 101 | + } |
26 | 102 | }
|
0 commit comments