-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(websites): store navigation items query in cache
- Loading branch information
Showing
7 changed files
with
122 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
mango3-core/src/models/navigation_item/navigation_item_all.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
use cached::proc_macro::io_cached; | ||
use cached::AsyncRedisCache; | ||
use sqlx::query_as; | ||
use sqlx::types::Uuid; | ||
|
||
use crate::constants::PREFIX_NAVIGATION_ITEM_ALL_BY_WEBSITE; | ||
use crate::models::{async_redis_cache, Website}; | ||
use crate::CoreContext; | ||
|
||
use super::{NavigationItem, NavigationItems}; | ||
|
||
impl NavigationItem { | ||
pub async fn all_by_website(core_context: &CoreContext, website: &Website) -> Vec<Self> { | ||
navigation_item_all_by_website(core_context, website) | ||
.await | ||
.map(|items| items.into()) | ||
.unwrap_or_default() | ||
} | ||
} | ||
|
||
#[io_cached( | ||
map_error = r##"|_| sqlx::Error::RowNotFound"##, | ||
convert = r#"{ website.id }"#, | ||
ty = "AsyncRedisCache<Uuid, NavigationItems>", | ||
create = r##" { async_redis_cache(PREFIX_NAVIGATION_ITEM_ALL_BY_WEBSITE).await } "## | ||
)] | ||
pub(crate) async fn navigation_item_all_by_website( | ||
core_context: &CoreContext, | ||
website: &Website, | ||
) -> sqlx::Result<NavigationItems> { | ||
query_as!( | ||
NavigationItem, | ||
"SELECT * FROM navigation_items WHERE website_id = $1 ORDER BY position ASC", | ||
website.id // $1 | ||
) | ||
.fetch_all(&core_context.db_pool) | ||
.await | ||
.map(|items| items.into()) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::test_utils::{insert_test_navigation_item, insert_test_website, setup_core_context}; | ||
|
||
use super::NavigationItem; | ||
|
||
#[tokio::test] | ||
async fn should_get_zero_navigation_items() { | ||
let core_context = setup_core_context().await; | ||
let website = insert_test_website(&core_context, None).await; | ||
|
||
let items = NavigationItem::all_by_website(&core_context, &website).await; | ||
|
||
assert!(items.is_empty()); | ||
} | ||
|
||
#[tokio::test] | ||
async fn should_get_one_navigation_item() { | ||
let core_context = setup_core_context().await; | ||
let website = insert_test_website(&core_context, None).await; | ||
|
||
insert_test_navigation_item(&core_context, Some(&website)).await; | ||
|
||
let items = NavigationItem::all_by_website(&core_context, &website).await; | ||
|
||
assert_eq!(items.len(), 1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters