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 ) ]
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 Into < i32 > for VersionAction {
24
+ fn into ( self ) -> i32 {
25
+ match self {
26
+ VersionAction :: Publish => 0 ,
27
+ VersionAction :: Yank => 1 ,
28
+ VersionAction :: Unyank => 2 ,
29
+ }
30
+ }
31
+ }
32
+
33
+ impl FromSql < Integer , Pg > for VersionAction {
34
+ fn from_sql ( bytes : Option < & [ u8 ] > ) -> deserialize:: Result < Self > {
35
+ match <i32 as FromSql < Integer , Pg > >:: from_sql ( bytes) ? {
36
+ 0 => Ok ( VersionAction :: Publish ) ,
37
+ 1 => Ok ( VersionAction :: Yank ) ,
38
+ 2 => Ok ( VersionAction :: Unyank ) ,
39
+ n => Err ( format ! ( "unknown version action: {}" , n) . into ( ) ) ,
40
+ }
41
+ }
42
+ }
43
+
44
+ impl ToSql < Integer , Pg > for VersionAction {
45
+ fn to_sql < W : Write > ( & self , out : & mut Output < ' _ , W , Pg > ) -> serialize:: Result {
46
+ let as_i32: i32 = ( * self ) . into ( ) ;
47
+
48
+ ToSql :: < Integer , Pg > :: to_sql ( & as_i32, out)
49
+ }
50
+ }
51
+
14
52
#[ derive( Debug , Clone , Copy , Queryable , Identifiable , Associations ) ]
15
53
#[ belongs_to( Version ) ]
16
54
#[ belongs_to( User , foreign_key = "owner_id" ) ]
@@ -20,7 +58,38 @@ pub struct VersionOwnerAction {
20
58
pub id : i32 ,
21
59
pub version_id : i32 ,
22
60
pub owner_id : i32 ,
23
- pub owner_token_id : i32 ,
61
+ pub owner_token_id : Option < i32 > ,
24
62
pub action : VersionAction ,
25
63
pub time : NaiveDateTime ,
26
64
}
65
+
66
+ #[ derive( Copy , Clone , Debug , Insertable ) ]
67
+ #[ table_name = "version_owner_actions" ]
68
+ pub struct NewVersionOwnerAction {
69
+ pub version_id : i32 ,
70
+ pub owner_id : i32 ,
71
+ pub owner_token_id : Option < i32 > ,
72
+ pub action : VersionAction ,
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
+ }
95
+ }
0 commit comments