|
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, |
21 | | - pub version_id: i32, |
22 | | - pub owner_id: i32, |
23 | | - pub owner_token_id: i32, |
| 47 | + pub version_id: Option<i32>, |
| 48 | + pub owner_id: Option<i32>, |
| 49 | + pub owner_token_id: Option<i32>, |
24 | 50 | pub action: VersionAction, |
25 | 51 | pub time: NaiveDateTime, |
26 | 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 | +pub fn insert_version_owner_action( |
| 74 | + conn: &PgConnection, |
| 75 | + _version_id: i32, |
| 76 | + _owner_id: i32, |
| 77 | + _owner_token_id: Option<i32>, |
| 78 | + _action: VersionAction, |
| 79 | +) -> QueryResult<VersionOwnerAction> { |
| 80 | + use version_owner_actions::dsl::{version_id, owner_id, owner_token_id, action}; |
| 81 | + |
| 82 | + diesel::insert_into(version_owner_actions::table) |
| 83 | + .values(( |
| 84 | + version_id.eq(_version_id), |
| 85 | + owner_id.eq(_owner_id), |
| 86 | + owner_token_id.eq(_owner_token_id), |
| 87 | + action.eq(_action))) |
| 88 | + .get_result(conn) |
| 89 | +} |
0 commit comments