1
1
use super :: error:: { EntityApiErrorKind , Error } ;
2
- use crate :: uuid_parse_str;
3
- use entity:: actions:: { self , ActiveModel , Entity , Model } ;
2
+ use entity:: actions:: { ActiveModel , Entity , Model } ;
4
3
use entity:: { status:: Status , Id } ;
5
4
use sea_orm:: {
6
5
entity:: prelude:: * ,
7
6
ActiveValue :: { Set , Unchanged } ,
8
7
DatabaseConnection , TryIntoModel ,
9
8
} ;
10
- use std:: collections:: HashMap ;
11
9
12
10
use log:: * ;
13
11
@@ -106,68 +104,16 @@ pub async fn update_status(
106
104
pub async fn delete_by_id ( db : & DatabaseConnection , id : Id ) -> Result < ( ) , Error > {
107
105
let result = find_by_id ( db, id) . await ?;
108
106
109
- match result {
110
- Some ( action_model) => {
111
- debug ! ( "Existing Action model to be deleted: {:?}" , action_model) ;
112
-
113
- action_model. delete ( db) . await ?;
114
- Ok ( ( ) )
115
- }
116
- None => Err ( Error {
117
- source : None ,
118
- error_kind : EntityApiErrorKind :: RecordNotFound ,
119
- } ) ,
120
- }
121
- }
122
-
123
- pub async fn find_by_id ( db : & DatabaseConnection , id : Id ) -> Result < Option < Model > , Error > {
124
- match Entity :: find_by_id ( id) . one ( db) . await {
125
- Ok ( Some ( action) ) => {
126
- debug ! ( "Action found: {:?}" , action) ;
107
+ result. delete ( db) . await ?;
127
108
128
- Ok ( Some ( action) )
129
- }
130
- Ok ( None ) => {
131
- error ! ( "Action with id {} not found" , id) ;
132
-
133
- Err ( Error {
134
- source : None ,
135
- error_kind : EntityApiErrorKind :: RecordNotFound ,
136
- } )
137
- }
138
- Err ( err) => {
139
- error ! ( "Action with id {} not found and returned error {}" , id, err) ;
140
- Err ( Error {
141
- source : None ,
142
- error_kind : EntityApiErrorKind :: RecordNotFound ,
143
- } )
144
- }
145
- }
109
+ Ok ( ( ) )
146
110
}
147
111
148
- pub async fn find_by (
149
- db : & DatabaseConnection ,
150
- query_params : HashMap < String , String > ,
151
- ) -> Result < Vec < Model > , Error > {
152
- let mut query = Entity :: find ( ) ;
153
-
154
- for ( key, value) in query_params {
155
- match key. as_str ( ) {
156
- "coaching_session_id" => {
157
- let coaching_session_id = uuid_parse_str ( & value) ?;
158
-
159
- query = query. filter ( actions:: Column :: CoachingSessionId . eq ( coaching_session_id) ) ;
160
- }
161
- _ => {
162
- return Err ( Error {
163
- source : None ,
164
- error_kind : EntityApiErrorKind :: InvalidQueryTerm ,
165
- } ) ;
166
- }
167
- }
168
- }
169
-
170
- Ok ( query. all ( db) . await ?)
112
+ pub async fn find_by_id ( db : & DatabaseConnection , id : Id ) -> Result < Model , Error > {
113
+ Entity :: find_by_id ( id) . one ( db) . await ?. ok_or_else ( || Error {
114
+ source : None ,
115
+ error_kind : EntityApiErrorKind :: RecordNotFound ,
116
+ } )
171
117
}
172
118
173
119
#[ cfg( test) ]
@@ -178,7 +124,7 @@ pub async fn find_by(
178
124
mod tests {
179
125
use super :: * ;
180
126
use entity:: { actions:: Model , Id } ;
181
- use sea_orm:: { DatabaseBackend , MockDatabase , Transaction } ;
127
+ use sea_orm:: { DatabaseBackend , MockDatabase } ;
182
128
183
129
#[ tokio:: test]
184
130
async fn create_returns_a_new_action_model ( ) -> Result < ( ) , Error > {
@@ -286,29 +232,4 @@ mod tests {
286
232
287
233
Ok ( ( ) )
288
234
}
289
-
290
- #[ tokio:: test]
291
- async fn find_by_returns_all_actions_associated_with_coaching_session ( ) -> Result < ( ) , Error > {
292
- let db = MockDatabase :: new ( DatabaseBackend :: Postgres ) . into_connection ( ) ;
293
- let mut query_params = HashMap :: new ( ) ;
294
- let coaching_session_id = Id :: new_v4 ( ) ;
295
-
296
- query_params. insert (
297
- "coaching_session_id" . to_owned ( ) ,
298
- coaching_session_id. to_string ( ) ,
299
- ) ;
300
-
301
- let _ = find_by ( & db, query_params) . await ;
302
-
303
- assert_eq ! (
304
- db. into_transaction_log( ) ,
305
- [ Transaction :: from_sql_and_values(
306
- DatabaseBackend :: Postgres ,
307
- r#"SELECT "actions"."id", "actions"."coaching_session_id", "actions"."user_id", "actions"."body", "actions"."due_by", CAST("actions"."status" AS text), "actions"."status_changed_at", "actions"."created_at", "actions"."updated_at" FROM "refactor_platform"."actions" WHERE "actions"."coaching_session_id" = $1"# ,
308
- [ coaching_session_id. into( ) ]
309
- ) ]
310
- ) ;
311
-
312
- Ok ( ( ) )
313
- }
314
235
}
0 commit comments