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