[fractal/backend: 2/9] backend: Move room model to their own module



commit 22c44a5ff5fcc263aaa5a6186741129b42343d62
Author: Daniel GarcĂ­a Moreno <danigm wadobo com>
Date:   Thu Dec 13 12:23:03 2018 +0100

    backend: Move room model to their own module

 fractal-backend/src/model/mod.rs                | 74 +++++++++++++++++++++++++
 fractal-backend/src/{model.rs => model/room.rs} | 73 ++----------------------
 2 files changed, 78 insertions(+), 69 deletions(-)
---
diff --git a/fractal-backend/src/model/mod.rs b/fractal-backend/src/model/mod.rs
new file mode 100644
index 00000000..d3137fdd
--- /dev/null
+++ b/fractal-backend/src/model/mod.rs
@@ -0,0 +1,74 @@
+use super::conn;
+use failure::err_msg;
+use failure::Error;
+use rusqlite::Row;
+use rusqlite::NO_PARAMS;
+
+pub mod room;
+pub use self::room::Room;
+
+pub trait Model: Sized {
+    fn store(&self) -> Result<(), Error>;
+    fn get_id(&self) -> &str;
+    fn map_row(row: &Row) -> Self;
+
+    fn create_sql() -> String;
+    fn table_name() -> &'static str;
+    fn fields() -> Vec<&'static str>;
+
+    fn get(id: &str) -> Result<Self, Error> {
+        let fields = Self::fields().join(",");
+        let query = format!("SELECT {} FROM {} WHERE id = ?", fields, Self::table_name());
+        conn(
+            move |c| {
+                let mut stmt = c.prepare(&query)?;
+                let mut iter = stmt.query_map(&[id], Self::map_row)?;
+
+                iter.next()
+                    .ok_or(err_msg("Object not found"))?
+                    .map_err(|e| err_msg(e.to_string()))
+            },
+            Err(err_msg("Connection not init")),
+        )
+    }
+
+    fn all() -> Result<Vec<Self>, Error> {
+        let fields = Self::fields().join(",");
+        let query = format!("SELECT {} FROM {}", fields, Self::table_name());
+        conn(
+            move |c| {
+                let mut stmt = c.prepare(&query)?;
+                let iter = stmt.query_map(NO_PARAMS, Self::map_row)?;
+
+                let array = iter
+                    .filter(|r| r.is_ok())
+                    .map(|r| r.unwrap())
+                    .collect::<Vec<Self>>();
+                Ok(array)
+            },
+            Err(err_msg("Connection not init")),
+        )
+    }
+
+    fn delete(&self) -> Result<usize, Error> {
+        let query = format!("DELETE from {} WHERE id = ?", Self::table_name());
+
+        conn(
+            move |c| {
+                c.execute(&query, &[self.get_id()])
+                    .map_err(|e| err_msg(e.to_string()))
+            },
+            Err(err_msg("Connection not init")),
+        )
+    }
+
+    fn create_table() -> Result<usize, Error> {
+        conn(
+            move |c| {
+                c.execute(&Self::create_sql(), NO_PARAMS)
+                    .map_err(|e| err_msg(e.to_string()))
+            },
+            Err(err_msg("Connection not init")),
+        )
+    }
+}
diff --git a/fractal-backend/src/model.rs b/fractal-backend/src/model/room.rs
similarity index 64%
rename from fractal-backend/src/model.rs
rename to fractal-backend/src/model/room.rs
index 24147856..455a315c 100644
--- a/fractal-backend/src/model.rs
+++ b/fractal-backend/src/model/room.rs
@@ -1,79 +1,14 @@
 use std::collections::HashMap;
 
-use super::conn;
+pub use api::types::Room;
 use failure::err_msg;
 use failure::Error;
+
 use rusqlite::types::ToSql;
 use rusqlite::Row;
-use rusqlite::NO_PARAMS;
-
-pub use api::types::Room;
-
-pub trait Model: Sized {
-    fn store(&self) -> Result<(), Error>;
-    fn get_id(&self) -> &str;
-    fn map_row(row: &Row) -> Self;
-
-    fn create_sql() -> String;
-    fn table_name() -> &'static str;
-    fn fields() -> Vec<&'static str>;
-
-    fn get(id: &str) -> Result<Self, Error> {
-        let fields = Self::fields().join(",");
-        let query = format!("SELECT {} FROM {} WHERE id = ?", fields, Self::table_name());
-        conn(
-            move |c| {
-                let mut stmt = c.prepare(&query)?;
-                let mut iter = stmt.query_map(&[id], Self::map_row)?;
 
-                iter.next()
-                    .ok_or(err_msg("Object not found"))?
-                    .map_err(|e| err_msg(e.to_string()))
-            },
-            Err(err_msg("Connection not init")),
-        )
-    }
-
-    fn all() -> Result<Vec<Self>, Error> {
-        let fields = Self::fields().join(",");
-        let query = format!("SELECT {} FROM {}", fields, Self::table_name());
-        conn(
-            move |c| {
-                let mut stmt = c.prepare(&query)?;
-                let iter = stmt.query_map(NO_PARAMS, Self::map_row)?;
-
-                let array = iter
-                    .filter(|r| r.is_ok())
-                    .map(|r| r.unwrap())
-                    .collect::<Vec<Self>>();
-                Ok(array)
-            },
-            Err(err_msg("Connection not init")),
-        )
-    }
-
-    fn delete(&self) -> Result<usize, Error> {
-        let query = format!("DELETE from {} WHERE id = ?", Self::table_name());
-
-        conn(
-            move |c| {
-                c.execute(&query, &[self.get_id()])
-                    .map_err(|e| err_msg(e.to_string()))
-            },
-            Err(err_msg("Connection not init")),
-        )
-    }
-
-    fn create_table() -> Result<usize, Error> {
-        conn(
-            move |c| {
-                c.execute(&Self::create_sql(), NO_PARAMS)
-                    .map_err(|e| err_msg(e.to_string()))
-            },
-            Err(err_msg("Connection not init")),
-        )
-    }
-}
+use super::conn;
+use super::Model;
 
 impl Model for Room {
     fn table_name() -> &'static str {


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]