|
1 |
| -use sea_orm::{entity::*, query::*, DbErr}; |
| 1 | +use sea_orm::{entity::*, query::*, DbBackend, DbErr}; |
2 | 2 | use crate::models::applications::{Column, ActiveModel, Entity as Application, Model as ApplicationModel};
|
3 | 3 | use crate::get_database_connection;
|
4 | 4 | use sha1::{Sha1, Digest};
|
| 5 | +use chrono::{DateTime, Utc, TimeZone}; |
5 | 6 |
|
6 | 7 | /**
|
7 | 8 | * Get all applications from the database
|
8 | 9 | *
|
9 | 10 | * # Returns
|
10 | 11 | * @return Result<Vec<ApplicationModel>, sea_orm::DbErr> - The result of the operation
|
11 | 12 | */
|
12 |
| -pub async fn get_applications() ->Result<Vec<ApplicationModel>, sea_orm::DbErr> { |
| 13 | +pub async fn get_applications() -> Result<Vec<ApplicationModel>, sea_orm::DbErr> { |
13 | 14 | let conn = get_database_connection().await?;
|
14 |
| - Application::find() |
15 |
| - .order_by(Column::Owner, Order::Asc) |
16 |
| - .order_by(Column::Repo, Order::Asc) |
17 |
| - .all(&conn).await |
| 15 | + |
| 16 | + //Get all applications from the database. |
| 17 | + //Distinct on is not supported in sea_orm yet, so we have to use raw SQL |
| 18 | + let app_data = JsonValue::find_by_statement(Statement::from_sql_and_values( |
| 19 | + DbBackend::Postgres, |
| 20 | + r#" |
| 21 | + SELECT DISTINCT ON (owner, repo, id) |
| 22 | + a.id, |
| 23 | + a.owner, |
| 24 | + a.repo, |
| 25 | + a.pr_number, |
| 26 | + a.application, |
| 27 | + a.updated_at, |
| 28 | + a.sha, |
| 29 | + a.path |
| 30 | + FROM |
| 31 | + applications a |
| 32 | + ORDER BY |
| 33 | + a.owner, |
| 34 | + a.repo, |
| 35 | + a.id, |
| 36 | + a.pr_number DESC |
| 37 | + "#, |
| 38 | + [], |
| 39 | + )) |
| 40 | + .all(&conn) |
| 41 | + .await?; |
| 42 | + |
| 43 | + //Iterate over the results and convert them to ApplicationModel |
| 44 | + let mut applications: Vec<ApplicationModel> = Vec::new(); |
| 45 | + for app in app_data { |
| 46 | + applications.push(ApplicationModel { |
| 47 | + id: app.get("id").unwrap().as_str().unwrap().to_string(), |
| 48 | + owner: app.get("owner").unwrap().as_str().unwrap().to_string(), |
| 49 | + repo: app.get("repo").unwrap().as_str().unwrap().to_string(), |
| 50 | + pr_number: app.get("pr_number").unwrap().as_i64().unwrap() as i64, |
| 51 | + application: Some(app.get("application").unwrap().as_str().unwrap().to_string()), |
| 52 | + updated_at: Utc.from_utc_datetime(&app.get("updated_at").unwrap().as_str().unwrap().parse::<DateTime<Utc>>().unwrap().naive_utc()), |
| 53 | + sha: Some(app.get("sha").unwrap().as_str().unwrap().to_string()), |
| 54 | + path: Some(app.get("path").unwrap().as_str().unwrap().to_string()), |
| 55 | + }); |
| 56 | + } |
| 57 | + Ok(applications) |
| 58 | + |
18 | 59 | }
|
19 | 60 |
|
20 | 61 | /**
|
|
0 commit comments