From 69e624f82bd26b9b63784cdb89fb1ac5ee82169e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa?= Date: Thu, 26 Apr 2018 23:21:29 +0200 Subject: [PATCH] Improved collection loading --- src/db/models/collection.rs | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/src/db/models/collection.rs b/src/db/models/collection.rs index f88d356..34854e8 100644 --- a/src/db/models/collection.rs +++ b/src/db/models/collection.rs @@ -66,26 +66,19 @@ impl Collection { .first::(&**conn).ok() } - pub fn find_by_user_uuid(uuid: &str, conn: &DbConn) -> Vec { - match users_collections::table - .filter(users_collections::user_uuid.eq(uuid)) - .select(users_collections::columns::collection_uuid) - .load(&**conn) { - Ok(uuids) => uuids.iter().map(|uuid: &String| { - Collection::find_by_uuid(uuid, &conn).unwrap() - }).collect(), - Err(list) => vec![] - } + pub fn find_by_user_uuid(user_uuid: &str, conn: &DbConn) -> Vec { + users_collections::table.inner_join(collections::table) + .filter(users_collections::user_uuid.eq(user_uuid)) + .select(collections::all_columns) + .load::(&**conn).expect("Error loading user collections") } pub fn find_by_uuid_and_user(uuid: &str, user_uuid: &str, conn: &DbConn) -> Option { - match users_collections::table + users_collections::table.inner_join(collections::table) .filter(users_collections::collection_uuid.eq(uuid)) .filter(users_collections::user_uuid.eq(user_uuid)) - .first::(&**conn).ok() { - None => None, - Some(collection_user) => Collection::find_by_uuid(&collection_user.collection_uuid, &conn) - } + .select(collections::all_columns) + .first::(&**conn).ok() } }