|
| 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; |
| 10 | + |
| 11 | +use crate::models::{ApiToken, Crate, User}; |
| 12 | +use crate::schema::*; |
| 13 | + |
| 14 | +#[derive(Debug, Clone, Copy, PartialEq, FromSqlRow, AsExpression)] |
| 15 | +#[repr(i32)] |
| 16 | +#[sql_type = "Integer"] |
| 17 | +pub enum CrateAction { |
| 18 | + InviteUser = 0, |
| 19 | + RemoveUser = 1, |
| 20 | +} |
| 21 | + |
| 22 | +impl Into<&'static str> for CrateAction { |
| 23 | + fn into(self) -> &'static str { |
| 24 | + match self { |
| 25 | + CrateAction::InviteUser => "invite_user", |
| 26 | + CrateAction::RemoveUser => "remove_user", |
| 27 | + } |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +impl Into<String> for CrateAction { |
| 32 | + fn into(self) -> String { |
| 33 | + let string: &'static str = self.into(); |
| 34 | + |
| 35 | + string.into() |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +impl FromSql<Integer, Pg> for CrateAction { |
| 40 | + fn from_sql(bytes: Option<&[u8]>) -> deserialize::Result<Self> { |
| 41 | + match <i32 as FromSql<Integer, Pg>>::from_sql(bytes)? { |
| 42 | + 0 => Ok(CrateAction::InviteUser), |
| 43 | + 1 => Ok(CrateAction::RemoveUser), |
| 44 | + n => Err(format!("unknown crate action: {}", n).into()), |
| 45 | + } |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +impl ToSql<Integer, Pg> for CrateAction { |
| 50 | + fn to_sql<W: Write>(&self, out: &mut Output<'_, W, Pg>) -> serialize::Result { |
| 51 | + ToSql::<Integer, Pg>::to_sql(&(*self as i32), out) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +#[derive(Debug, Clone, Copy, Queryable, Identifiable, Associations)] |
| 56 | +#[belongs_to(Crate)] |
| 57 | +#[belongs_to(User)] |
| 58 | +#[belongs_to(ApiToken)] |
| 59 | +pub struct CrateOwnerAction { |
| 60 | + pub id: i32, |
| 61 | + pub crate_id: i32, |
| 62 | + pub user_id: i32, |
| 63 | + pub api_token_id: Option<i32>, |
| 64 | + pub action: CrateAction, |
| 65 | + pub time: NaiveDateTime, |
| 66 | +} |
| 67 | + |
| 68 | +impl CrateOwnerAction { |
| 69 | + pub fn all(conn: &PgConnection) -> QueryResult<Vec<Self>> { |
| 70 | + crate_owner_actions::table.load(conn) |
| 71 | + } |
| 72 | + |
| 73 | + pub fn by_crate(conn: &PgConnection, krate: &Crate) -> QueryResult<Vec<(Self, User)>> { |
| 74 | + use crate_owner_actions::dsl::crate_id; |
| 75 | + |
| 76 | + crate_owner_actions::table |
| 77 | + .filter(crate_id.eq(krate.id)) |
| 78 | + .inner_join(users::table) |
| 79 | + .order(crate_owner_actions::dsl::id) |
| 80 | + .load(conn) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +pub fn insert_crate_owner_action( |
| 85 | + conn: &PgConnection, |
| 86 | + crate_id_: i32, |
| 87 | + user_id_: i32, |
| 88 | + api_token_id_: Option<i32>, |
| 89 | + action_: CrateAction, |
| 90 | +) -> QueryResult<CrateOwnerAction> { |
| 91 | + use crate_owner_actions::dsl::{action, api_token_id, crate_id, user_id}; |
| 92 | + |
| 93 | + diesel::insert_into(crate_owner_actions::table) |
| 94 | + .values(( |
| 95 | + crate_id.eq(crate_id_), |
| 96 | + user_id.eq(user_id_), |
| 97 | + api_token_id.eq(api_token_id_), |
| 98 | + action.eq(action_), |
| 99 | + )) |
| 100 | + .get_result(conn) |
| 101 | +} |
0 commit comments