try to do better
This commit is contained in:
@@ -1,9 +1,18 @@
|
||||
use sea_orm::*;
|
||||
use sea_orm::prelude::Decimal;
|
||||
use chrono::Utc;
|
||||
use chrono::{Utc, Datelike};
|
||||
use std::collections::HashMap;
|
||||
use crate::models::expense::{self, Entity as Expense, Model as ExpenseModel};
|
||||
use crate::models::category::{Entity as Category};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct MonthlyExpenseGroup {
|
||||
pub year: i32,
|
||||
pub month: u32,
|
||||
pub total_amount: Decimal,
|
||||
pub expenses: Vec<ExpenseModel>,
|
||||
}
|
||||
|
||||
pub struct ExpenseService;
|
||||
|
||||
impl ExpenseService {
|
||||
@@ -18,6 +27,7 @@ impl ExpenseService {
|
||||
amount: Set(amount),
|
||||
description: Set(description),
|
||||
created_at: Set(Utc::now().naive_utc()),
|
||||
active: Set(true),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
@@ -38,6 +48,28 @@ impl ExpenseService {
|
||||
) -> Result<Vec<ExpenseModel>, DbErr> {
|
||||
Expense::find()
|
||||
.filter(expense::Column::CategoryId.eq(category_id))
|
||||
.filter(expense::Column::Active.eq(true))
|
||||
.all(db)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn find_all_by_category_id(
|
||||
db: &DatabaseConnection,
|
||||
category_id: i32,
|
||||
) -> Result<Vec<ExpenseModel>, DbErr> {
|
||||
Expense::find()
|
||||
.filter(expense::Column::CategoryId.eq(category_id))
|
||||
.all(db)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn find_inactive_by_category_id(
|
||||
db: &DatabaseConnection,
|
||||
category_id: i32,
|
||||
) -> Result<Vec<ExpenseModel>, DbErr> {
|
||||
Expense::find()
|
||||
.filter(expense::Column::CategoryId.eq(category_id))
|
||||
.filter(expense::Column::Active.eq(false))
|
||||
.all(db)
|
||||
.await
|
||||
}
|
||||
@@ -66,14 +98,15 @@ impl ExpenseService {
|
||||
expense.update(db).await
|
||||
}
|
||||
|
||||
pub async fn delete(db: &DatabaseConnection, id: i32) -> Result<DeleteResult, DbErr> {
|
||||
pub async fn delete(db: &DatabaseConnection, id: i32) -> Result<ExpenseModel, DbErr> {
|
||||
let expense = Expense::find_by_id(id)
|
||||
.one(db)
|
||||
.await?
|
||||
.ok_or(DbErr::RecordNotFound("Expense not found".to_string()))?;
|
||||
|
||||
let expense: expense::ActiveModel = expense.into();
|
||||
expense.delete(db).await
|
||||
let mut expense: expense::ActiveModel = expense.into();
|
||||
expense.active = Set(false);
|
||||
expense.update(db).await
|
||||
}
|
||||
|
||||
pub async fn calculate_remaining_limit(
|
||||
@@ -92,4 +125,60 @@ impl ExpenseService {
|
||||
|
||||
Ok(remaining)
|
||||
}
|
||||
|
||||
pub async fn get_expense_history(
|
||||
db: &DatabaseConnection,
|
||||
category_id: i32,
|
||||
sort_order: Option<String>,
|
||||
show_archive: bool,
|
||||
) -> Result<Vec<MonthlyExpenseGroup>, DbErr> {
|
||||
let expenses = if show_archive {
|
||||
Self::find_inactive_by_category_id(db, category_id).await?
|
||||
} else {
|
||||
Self::find_by_category_id(db, category_id).await?
|
||||
};
|
||||
|
||||
let mut grouped: HashMap<(i32, u32), Vec<ExpenseModel>> = HashMap::new();
|
||||
|
||||
for expense in expenses {
|
||||
let year = expense.created_at.year();
|
||||
let month = expense.created_at.month();
|
||||
grouped.entry((year, month))
|
||||
.or_insert_with(Vec::new)
|
||||
.push(expense);
|
||||
}
|
||||
|
||||
let mut result: Vec<MonthlyExpenseGroup> = grouped
|
||||
.into_iter()
|
||||
.map(|((year, month), expenses)| {
|
||||
let total_amount: Decimal = expenses
|
||||
.iter()
|
||||
.filter(|e| e.active)
|
||||
.map(|e| e.amount)
|
||||
.sum();
|
||||
MonthlyExpenseGroup {
|
||||
year,
|
||||
month,
|
||||
total_amount,
|
||||
expenses,
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
||||
let sort_desc = sort_order
|
||||
.as_deref()
|
||||
.map(|s| s.to_lowercase() == "desc")
|
||||
.unwrap_or(true);
|
||||
|
||||
result.sort_by(|a, b| {
|
||||
let cmp = a.year.cmp(&b.year).then(a.month.cmp(&b.month));
|
||||
if sort_desc {
|
||||
cmp.reverse()
|
||||
} else {
|
||||
cmp
|
||||
}
|
||||
});
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@ impl OAuthService {
|
||||
let redirect_url = std::env::var("GOOGLE_REDIRECT_URL")
|
||||
.unwrap_or_else(|_| "http://localhost:8080/api/auth/google/callback".to_string());
|
||||
|
||||
let client = Self::getClient(client_id, client_secret, redirect_url);
|
||||
let client = Self::get_client(client_id, client_secret, redirect_url);
|
||||
|
||||
let (auth_url, csrf_token) = client
|
||||
.authorize_url(CsrfToken::new_random)
|
||||
@@ -52,7 +52,7 @@ impl OAuthService {
|
||||
let redirect_url = std::env::var("GOOGLE_REDIRECT_URL")
|
||||
.unwrap_or_else(|_| "http://localhost:8080/api/auth/google/callback".to_string());
|
||||
|
||||
let client = Self::getClient(client_id, client_secret, redirect_url);
|
||||
let client = Self::get_client(client_id, client_secret, redirect_url);
|
||||
|
||||
let http_client = oauth2::reqwest::ClientBuilder::new()
|
||||
.build()
|
||||
@@ -67,7 +67,7 @@ impl OAuthService {
|
||||
Ok(token.access_token().secret().clone())
|
||||
}
|
||||
|
||||
fn getClient(client_id: String, client_secret: String, redirect_url: String) -> Client<BasicErrorResponse,
|
||||
fn get_client(client_id: String, client_secret: String, redirect_url: String) -> Client<BasicErrorResponse,
|
||||
BasicTokenResponse,
|
||||
BasicTokenIntrospectionResponse,
|
||||
StandardRevocableToken,
|
||||
|
||||
Reference in New Issue
Block a user