gbserver/gb-storage/src/postgres.rs

99 lines
2.7 KiB
Rust
Raw Normal View History

2024-12-24 13:05:54 -03:00
use gb_core::{Result, Error};
2024-12-22 20:56:52 -03:00
use sqlx::PgPool;
2024-12-24 13:05:54 -03:00
use std::sync::Arc;
2024-12-22 20:56:52 -03:00
use uuid::Uuid;
2024-12-24 13:05:54 -03:00
use gb_core::models::Customer;
pub trait CustomerRepository {
async fn create(&self, customer: Customer) -> Result<Customer>;
async fn get(&self, id: Uuid) -> Result<Option<Customer>>;
async fn update(&self, customer: Customer) -> Result<Customer>;
async fn delete(&self, id: Uuid) -> Result<()>;
}
2024-12-22 20:56:52 -03:00
pub struct PostgresCustomerRepository {
2024-12-24 13:05:54 -03:00
pool: Arc<PgPool>,
2024-12-22 20:56:52 -03:00
}
impl PostgresCustomerRepository {
2024-12-24 13:05:54 -03:00
pub fn new(pool: Arc<PgPool>) -> Self {
2024-12-22 20:56:52 -03:00
Self { pool }
}
}
impl CustomerRepository for PostgresCustomerRepository {
2024-12-24 13:05:54 -03:00
async fn create(&self, customer: Customer) -> Result<Customer> {
let result = sqlx::query_as!(
2024-12-22 20:56:52 -03:00
Customer,
r#"
2024-12-24 13:05:54 -03:00
INSERT INTO customers (id, name, max_instances, email, created_at, updated_at)
VALUES ($1, $2, $3, $4, NOW(), NOW())
2024-12-22 20:56:52 -03:00
RETURNING *
"#,
customer.id,
customer.name,
2024-12-24 13:05:54 -03:00
customer.max_instances as i32,
customer.email,
2024-12-22 20:56:52 -03:00
)
2024-12-24 13:05:54 -03:00
.fetch_one(&*self.pool)
2024-12-22 20:56:52 -03:00
.await
2024-12-24 13:05:54 -03:00
.map_err(|e| Error::internal(format!("Database error: {}", e)))?;
2024-12-22 20:56:52 -03:00
2024-12-24 13:05:54 -03:00
Ok(result)
2024-12-22 20:56:52 -03:00
}
2024-12-24 13:05:54 -03:00
async fn get(&self, id: Uuid) -> Result<Option<Customer>> {
let result = sqlx::query_as!(
2024-12-22 20:56:52 -03:00
Customer,
r#"
2024-12-24 13:05:54 -03:00
SELECT id, name, max_instances::int as "max_instances!: i32",
email, created_at, updated_at
FROM customers
WHERE id = $1
2024-12-22 20:56:52 -03:00
"#,
id
)
2024-12-24 13:05:54 -03:00
.fetch_optional(&*self.pool)
2024-12-22 20:56:52 -03:00
.await
2024-12-24 13:05:54 -03:00
.map_err(|e| Error::internal(format!("Database error: {}", e)))?;
2024-12-22 20:56:52 -03:00
2024-12-24 13:05:54 -03:00
Ok(result)
2024-12-22 20:56:52 -03:00
}
2024-12-24 13:05:54 -03:00
async fn update(&self, customer: Customer) -> Result<Customer> {
let result = sqlx::query_as!(
2024-12-22 20:56:52 -03:00
Customer,
r#"
UPDATE customers
2024-12-24 13:05:54 -03:00
SET name = $2, max_instances = $3, email = $4, updated_at = NOW()
WHERE id = $1
RETURNING id, name, max_instances::int as "max_instances!: i32",
email, created_at, updated_at
2024-12-22 20:56:52 -03:00
"#,
2024-12-24 13:05:54 -03:00
customer.id,
2024-12-22 20:56:52 -03:00
customer.name,
2024-12-24 13:05:54 -03:00
customer.max_instances as i32,
customer.email,
2024-12-22 20:56:52 -03:00
)
2024-12-24 13:05:54 -03:00
.fetch_one(&*self.pool)
2024-12-22 20:56:52 -03:00
.await
2024-12-24 13:05:54 -03:00
.map_err(|e| Error::internal(format!("Database error: {}", e)))?;
2024-12-22 20:56:52 -03:00
2024-12-24 13:05:54 -03:00
Ok(result)
2024-12-22 20:56:52 -03:00
}
async fn delete(&self, id: Uuid) -> Result<()> {
sqlx::query!(
r#"
2024-12-24 13:05:54 -03:00
DELETE FROM customers
WHERE id = $1
2024-12-22 20:56:52 -03:00
"#,
id
)
2024-12-24 13:05:54 -03:00
.execute(&*self.pool)
2024-12-22 20:56:52 -03:00
.await
2024-12-24 13:05:54 -03:00
.map_err(|e| Error::internal(format!("Database error: {}", e)))?;
2024-12-22 20:56:52 -03:00
Ok(())
}
2024-12-24 13:05:54 -03:00
}