| [a4ca79c] | 1 | use sqlx::PgPool;
|
|---|
| 2 | use tauri::State;
|
|---|
| 3 | use crate::models::product::Product;
|
|---|
| 4 | use crate::commands::auth::ActiveSessions;
|
|---|
| 5 |
|
|---|
| 6 | #[tauri::command]
|
|---|
| 7 | pub async fn get_products(
|
|---|
| 8 | token: String,
|
|---|
| 9 | pool: State<'_, PgPool>,
|
|---|
| 10 | sessions: State<'_, ActiveSessions>,
|
|---|
| 11 | ) -> Result<Vec<Product>, String> {
|
|---|
| 12 | {
|
|---|
| 13 | let lock = sessions.0.lock().map_err(|_| "Системска грешка со сесијата".to_string())?;
|
|---|
| 14 | if !lock.contains_key(&token) {
|
|---|
| 15 | return Err("Неавторизиран пристап. Невалидна сесија.".to_string());
|
|---|
| 16 | }
|
|---|
| 17 | }
|
|---|
| 18 |
|
|---|
| 19 | let products = sqlx::query_as::<_, Product>(
|
|---|
| 20 | "
|
|---|
| 21 | SELECT product_id, category_id, name, description, price
|
|---|
| 22 | FROM product
|
|---|
| 23 | WHERE active = true
|
|---|
| 24 | "
|
|---|
| 25 | )
|
|---|
| 26 | .fetch_all(pool.inner())
|
|---|
| 27 | .await
|
|---|
| 28 | .map_err(|e| e.to_string())?;
|
|---|
| 29 |
|
|---|
| 30 | Ok(products)
|
|---|
| 31 | }
|
|---|
| 32 |
|
|---|
| 33 | #[tauri::command]
|
|---|
| 34 | pub async fn get_products_by_category(
|
|---|
| 35 | token: String,
|
|---|
| 36 | category_id: i32,
|
|---|
| 37 | pool: State<'_, PgPool>,
|
|---|
| 38 | sessions: State<'_, ActiveSessions>,
|
|---|
| 39 | ) -> Result<Vec<Product>, String> {
|
|---|
| 40 | {
|
|---|
| 41 | let lock = sessions.0.lock().map_err(|_| "Системска грешка со сесијата".to_string())?;
|
|---|
| 42 | if !lock.contains_key(&token) {
|
|---|
| 43 | return Err("Неавторизиран пристап. Невалидна сесија.".to_string());
|
|---|
| 44 | }
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | let products = sqlx::query_as::<_, Product>(
|
|---|
| 48 | "
|
|---|
| 49 | SELECT product_id, category_id, name, description, price
|
|---|
| 50 | FROM product
|
|---|
| 51 | WHERE category_id = $1
|
|---|
| 52 | AND active = true
|
|---|
| 53 | ORDER BY name
|
|---|
| 54 | "
|
|---|
| 55 | )
|
|---|
| 56 | .bind(category_id)
|
|---|
| 57 | .fetch_all(pool.inner())
|
|---|
| 58 | .await
|
|---|
| 59 | .map_err(|e| e.to_string())?;
|
|---|
| 60 |
|
|---|
| 61 | Ok(products)
|
|---|
| 62 | } |
|---|