File size: 1,217 Bytes
7856e60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
use crate::infrastructure::db::DbPool;
use serde::{Deserialize, Serialize};
use uuid::Uuid;

#[derive(Debug, Serialize, Deserialize, sqlx::FromRow)]
pub struct ProductFeedback {
    pub id: Uuid,
    pub transaction_id: String,
    pub product_id: String,
    pub rating: i32,
    pub comment: Option<String>,
    pub created_at: Option<chrono::NaiveDateTime>,
}

impl ProductFeedback {
    pub async fn create(pool: &DbPool, feedback: &Self) -> sqlx::Result<()> {
        sqlx::query(
            "INSERT INTO product_feedback (id, transaction_id, product_id, rating, comment) VALUES ($1, $2, $3, $4, $5)"
        )
        .bind(feedback.id)
        .bind(&feedback.transaction_id)
        .bind(&feedback.product_id)
        .bind(feedback.rating)
        .bind(&feedback.comment)
        .execute(pool)
        .await?;
        Ok(())
    }

    pub async fn get_for_merchant(pool: &DbPool, merchant_id: &str) -> sqlx::Result<Vec<Self>> {
        sqlx::query_as::<_, Self>(
            "SELECT f.* FROM product_feedback f JOIN orders o ON f.transaction_id = o.transaction_id WHERE o.merchant_id = $1 ORDER BY f.created_at DESC"
        )
        .bind(merchant_id)
        .fetch_all(pool)
        .await
    }
}