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::{AsyncPgConnection, RunQueryDsl};
12 pub async fn read_all(pool: &DbPool) -> Result<Vec<Language>, Error> {
13 let conn = &mut get_conn(pool).await?;
14 Self::read_all_conn(conn).await
17 pub async fn read_all_conn(conn: &mut AsyncPgConnection) -> Result<Vec<Language>, Error> {
18 language.load::<Self>(conn).await
21 pub async fn read_from_id(pool: &DbPool, id_: LanguageId) -> Result<Language, Error> {
22 let conn = &mut get_conn(pool).await?;
23 language.filter(id.eq(id_)).first::<Self>(conn).await
26 /// Attempts to find the given language code and return its ID. If not found, returns none.
27 pub async fn read_id_from_code(
30 ) -> Result<Option<LanguageId>, Error> {
31 if let Some(code_) = code_ {
32 let conn = &mut get_conn(pool).await?;
35 .filter(code.eq(code_))
49 use crate::{source::language::Language, utils::build_db_pool_for_tests};
50 use serial_test::serial;
54 async fn test_languages() {
55 let pool = &build_db_pool_for_tests().await;
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);