| 1 | use sqlx::PgPool;
|
|---|
| 2 | use tauri::State;
|
|---|
| 3 |
|
|---|
| 4 | use crate::commands::auth::{require_manager_session, ActiveSessions};
|
|---|
| 5 |
|
|---|
| 6 | #[tauri::command]
|
|---|
| 7 | pub async fn add_product(
|
|---|
| 8 | token: String,
|
|---|
| 9 | category_id: i32,
|
|---|
| 10 | name: String,
|
|---|
| 11 | description: String,
|
|---|
| 12 | price: f64,
|
|---|
| 13 | pool: State<'_, PgPool>,
|
|---|
| 14 | sessions: State<'_, ActiveSessions>,
|
|---|
| 15 | ) -> Result<(), String> {
|
|---|
| 16 |
|
|---|
| 17 | require_manager_session(&token, sessions.inner()).await?;
|
|---|
| 18 |
|
|---|
| 19 | if name.trim().is_empty() {
|
|---|
| 20 | return Err("Името е задолжително".to_string());
|
|---|
| 21 | }
|
|---|
| 22 |
|
|---|
| 23 | if price < 0.0 {
|
|---|
| 24 | return Err("Цената не смее да биде негативна".to_string());
|
|---|
| 25 | }
|
|---|
| 26 |
|
|---|
| 27 | sqlx::query(
|
|---|
| 28 | "
|
|---|
| 29 | INSERT INTO product
|
|---|
| 30 | (
|
|---|
| 31 | category_id,
|
|---|
| 32 | name,
|
|---|
| 33 | description,
|
|---|
| 34 | price
|
|---|
| 35 | )
|
|---|
| 36 | VALUES
|
|---|
| 37 | ($1,$2,$3,$4)
|
|---|
| 38 | "
|
|---|
| 39 | )
|
|---|
| 40 | .bind(category_id)
|
|---|
| 41 | .bind(name)
|
|---|
| 42 | .bind(description)
|
|---|
| 43 | .bind(price)
|
|---|
| 44 | .execute(pool.inner())
|
|---|
| 45 | .await
|
|---|
| 46 | .map_err(|e| e.to_string())?;
|
|---|
| 47 |
|
|---|
| 48 | Ok(())
|
|---|
| 49 | }
|
|---|
| 50 |
|
|---|
| 51 | #[tauri::command]
|
|---|
| 52 | pub async fn update_product(
|
|---|
| 53 | token: String,
|
|---|
| 54 | product_id: i32,
|
|---|
| 55 | category_id: i32,
|
|---|
| 56 | name: String,
|
|---|
| 57 | description: String,
|
|---|
| 58 | price: f64,
|
|---|
| 59 | pool: State<'_, PgPool>,
|
|---|
| 60 | sessions: State<'_, ActiveSessions>,
|
|---|
| 61 | ) -> Result<(), String> {
|
|---|
| 62 |
|
|---|
| 63 | require_manager_session(&token, sessions.inner()).await?;
|
|---|
| 64 |
|
|---|
| 65 | if name.trim().is_empty() {
|
|---|
| 66 | return Err("Името е задолжително".to_string());
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | if price < 0.0 {
|
|---|
| 70 | return Err("Цената не смее да биде негативна".to_string());
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | sqlx::query(
|
|---|
| 74 | "
|
|---|
| 75 | UPDATE product
|
|---|
| 76 | SET
|
|---|
| 77 | category_id = $1,
|
|---|
| 78 | name = $2,
|
|---|
| 79 | description = $3,
|
|---|
| 80 | price = $4
|
|---|
| 81 | WHERE product_id = $5
|
|---|
| 82 | "
|
|---|
| 83 | )
|
|---|
| 84 | .bind(category_id)
|
|---|
| 85 | .bind(name)
|
|---|
| 86 | .bind(description)
|
|---|
| 87 | .bind(price)
|
|---|
| 88 | .bind(product_id)
|
|---|
| 89 | .execute(pool.inner())
|
|---|
| 90 | .await
|
|---|
| 91 | .map_err(|e| e.to_string())?;
|
|---|
| 92 |
|
|---|
| 93 | Ok(())
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | #[tauri::command]
|
|---|
| 97 | pub async fn delete_product(
|
|---|
| 98 | token: String,
|
|---|
| 99 | product_id: i32,
|
|---|
| 100 | pool: State<'_, PgPool>,
|
|---|
| 101 | sessions: State<'_, ActiveSessions>,
|
|---|
| 102 | ) -> Result<(), String> {
|
|---|
| 103 |
|
|---|
| 104 | require_manager_session(&token, sessions.inner()).await?;
|
|---|
| 105 |
|
|---|
| 106 | sqlx::query(
|
|---|
| 107 | "
|
|---|
| 108 | UPDATE product
|
|---|
| 109 | SET active=false
|
|---|
| 110 | WHERE product_id=$1
|
|---|
| 111 | "
|
|---|
| 112 | )
|
|---|
| 113 | .bind(product_id)
|
|---|
| 114 | .execute(pool.inner())
|
|---|
| 115 | .await
|
|---|
| 116 | .map_err(|e| e.to_string())?;
|
|---|
| 117 |
|
|---|
| 118 | Ok(())
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | #[tauri::command]
|
|---|
| 122 | pub async fn update_product_price(
|
|---|
| 123 | token: String,
|
|---|
| 124 | product_id: i32,
|
|---|
| 125 | price: f64,
|
|---|
| 126 | pool: State<'_, PgPool>,
|
|---|
| 127 | sessions: State<'_, ActiveSessions>,
|
|---|
| 128 | ) -> Result<(), String> {
|
|---|
| 129 |
|
|---|
| 130 | require_manager_session(&token, sessions.inner()).await?;
|
|---|
| 131 |
|
|---|
| 132 | if price < 0.0 {
|
|---|
| 133 | return Err("Цената не смее да биде негативна".to_string());
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | sqlx::query(
|
|---|
| 137 | "
|
|---|
| 138 | UPDATE product
|
|---|
| 139 | SET price=$1
|
|---|
| 140 | WHERE product_id=$2
|
|---|
| 141 | "
|
|---|
| 142 | )
|
|---|
| 143 | .bind(price)
|
|---|
| 144 | .bind(product_id)
|
|---|
| 145 | .execute(pool.inner())
|
|---|
| 146 | .await
|
|---|
| 147 | .map_err(|e| e.to_string())?;
|
|---|
| 148 |
|
|---|
| 149 | Ok(())
|
|---|
| 150 | } |
|---|