2 diesel::ExpressionMethods,
4 schema::language::dsl::{code, id, language},
5 source::language::Language,
6 utils::{get_conn, DbPool},
8 use diesel::{result::Error, QueryDsl};
9 use diesel_async::RunQueryDsl;
12 pub async fn read_all(pool: &mut DbPool<'_>) -> Result<Vec<Language>, Error> {
13 let conn = &mut get_conn(pool).await?;
14 language.load::<Self>(conn).await
17 pub async fn read_from_id(pool: &mut DbPool<'_>, id_: LanguageId) -> Result<Language, Error> {
18 let conn = &mut get_conn(pool).await?;
19 language.filter(id.eq(id_)).first::<Self>(conn).await
22 /// Attempts to find the given language code and return its ID. If not found, returns none.
23 pub async fn read_id_from_code(
24 pool: &mut DbPool<'_>,
26 ) -> Result<Option<LanguageId>, Error> {
27 if let Some(code_) = code_ {
28 let conn = &mut get_conn(pool).await?;
31 .filter(code.eq(code_))
45 #![allow(clippy::unwrap_used)]
46 #![allow(clippy::indexing_slicing)]
48 use crate::{source::language::Language, utils::build_db_pool_for_tests};
49 use serial_test::serial;
53 async fn test_languages() {
54 let pool = &build_db_pool_for_tests().await;
55 let pool = &mut pool.into();
57 let all = Language::read_all(pool).await.unwrap();
59 assert_eq!(184, all.len());
60 assert_eq!("ak", all[5].code);
61 assert_eq!("lv", all[99].code);
62 assert_eq!("yi", all[179].code);