nxtgauge-backend-rust/crates/db/src/models/developer.rs

80 lines
2.1 KiB
Rust
Raw Normal View History

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use sqlx::{FromRow, PgPool};
use uuid::Uuid;
#[derive(Debug, Serialize, Deserialize, FromRow)]
pub struct DeveloperProfile {
pub id: Uuid,
pub user_id: Uuid,
pub bio: Option<String>,
pub experience_years: Option<i32>,
pub custom_data: Option<serde_json::Value>,
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct UpsertDeveloperProfilePayload {
pub bio: Option<String>,
pub experience_years: Option<i32>,
pub custom_data: Option<serde_json::Value>,
}
pub struct DeveloperRepository;
impl DeveloperRepository {
pub async fn get_by_user_id(
pool: &PgPool,
user_id: Uuid,
) -> Result<Option<DeveloperProfile>, sqlx::Error> {
let profile = sqlx::query_as!(
DeveloperProfile,
r#"
SELECT
id, user_id, bio, experience_years, custom_data,
created_at, updated_at
FROM developer_profiles
WHERE user_id = $1
"#,
user_id
)
.fetch_optional(pool)
.await?;
Ok(profile)
}
pub async fn upsert(
pool: &PgPool,
user_id: Uuid,
payload: UpsertDeveloperProfilePayload,
) -> Result<DeveloperProfile, sqlx::Error> {
let profile = sqlx::query_as!(
DeveloperProfile,
r#"
INSERT INTO developer_profiles (
user_id, bio, experience_years, custom_data
)
VALUES ($1, $2, $3, $4)
ON CONFLICT (user_id) DO UPDATE SET
bio = EXCLUDED.bio,
experience_years = EXCLUDED.experience_years,
custom_data = EXCLUDED.custom_data,
updated_at = NOW()
RETURNING
id, user_id, bio, experience_years, custom_data,
created_at, updated_at
"#,
user_id,
payload.bio,
payload.experience_years,
payload.custom_data
)
.fetch_one(pool)
.await?;
Ok(profile)
}
}