source: src-tauri/src/commands/order_item.rs@ 93e5ff0

main
Last change on this file since 93e5ff0 was a4ca79c, checked in by Georgi Paunkov <paunkovgeorgi@…>, 5 days ago

Initial commit

  • Property mode set to 100644
File size: 3.5 KB
Line 
1use sqlx::PgPool;
2use tauri::State;
3use crate::models::order_item::OrderItem;
4use crate::commands::auth::ActiveSessions;
5
6#[tauri::command]
7pub async fn add_order_item(
8 token: String,
9 order_id: i32,
10 product_id: i32,
11 quantity: i32,
12 pool: State<'_, PgPool>,
13 sessions: State<'_, ActiveSessions>,
14) -> Result<(), String> {
15 {
16 let lock = sessions.0.lock().map_err(|_| "Системска грешка".to_string())?;
17 if !lock.contains_key(&token) {
18 return Err("Невалидна сесија. Најавете се повторно.".to_string());
19 }
20 }
21
22 if quantity <= 0 {
23 return Err("Количината мора да биде поголема од 0".to_string());
24 }
25
26 let mut tx = pool.begin().await.map_err(|e| e.to_string())?;
27
28 let existing: Option<i32> = sqlx::query_scalar(
29 "
30 SELECT item_number
31 FROM order_item
32 WHERE order_id = $1
33 AND product_id = $2
34 "
35 )
36 .bind(order_id)
37 .bind(product_id)
38 .fetch_optional(&mut *tx)
39 .await
40 .map_err(|e| e.to_string())?;
41
42 if let Some(item_number) = existing {
43 sqlx::query(
44 "
45 UPDATE order_item
46 SET quantity = quantity + $3
47 WHERE order_id = $1
48 AND item_number = $2
49 "
50 )
51 .bind(order_id)
52 .bind(item_number)
53 .bind(quantity)
54 .execute(&mut *tx)
55 .await
56 .map_err(|e| e.to_string())?;
57 } else {
58 let next_item_number: i32 = sqlx::query_scalar(
59 "
60 SELECT COALESCE(MAX(item_number), 0) + 1
61 FROM order_item
62 WHERE order_id = $1
63 "
64 )
65 .bind(order_id)
66 .fetch_one(&mut *tx)
67 .await
68 .map_err(|e| e.to_string())?;
69
70 let unit_price: rust_decimal::Decimal = sqlx::query_scalar(
71 "SELECT price FROM product WHERE product_id = $1"
72 )
73 .bind(product_id)
74 .fetch_one(&mut *tx)
75 .await
76 .map_err(|e| e.to_string())?;
77
78 sqlx::query(
79 "
80 INSERT INTO order_item (order_id, item_number, product_id, quantity, unit_price)
81 VALUES ($1, $2, $3, $4, $5)
82 "
83 )
84 .bind(order_id)
85 .bind(next_item_number)
86 .bind(product_id)
87 .bind(quantity)
88 .bind(unit_price)
89 .execute(&mut *tx)
90 .await
91 .map_err(|e| e.to_string())?;
92 }
93
94 tx.commit().await.map_err(|e| e.to_string())?;
95 Ok(())
96}
97
98#[tauri::command]
99pub async fn get_order_items(
100 token: String,
101 order_id: i32,
102 pool: State<'_, PgPool>,
103 sessions: State<'_, ActiveSessions>,
104) -> Result<Vec<OrderItem>, String> {
105 {
106 let lock = sessions.0.lock().map_err(|_| "Системска грешка".to_string())?;
107 if !lock.contains_key(&token) {
108 return Err("Невалидна сесија.".to_string());
109 }
110 }
111
112 let items = sqlx::query_as::<_, OrderItem>(
113 "
114 SELECT
115 oi.order_id,
116 oi.item_number,
117 oi.product_id,
118 p.name as product_name,
119 oi.quantity,
120 oi.unit_price
121 FROM order_item oi
122 JOIN product p ON oi.product_id = p.product_id
123 WHERE oi.order_id = $1
124 ORDER BY oi.item_number
125 "
126 )
127 .bind(order_id)
128 .fetch_all(pool.inner())
129 .await
130 .map_err(|e| e.to_string())?;
131
132 Ok(items)
133}
Note: See TracBrowser for help on using the repository browser.