64 lines
2.2 KiB
Rust
64 lines
2.2 KiB
Rust
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 display_name: Option<String>,
|
|
pub bio: Option<String>,
|
|
pub location: Option<String>,
|
|
pub custom_data: Option<serde_json::Value>,
|
|
pub status: String,
|
|
pub created_at: DateTime<Utc>,
|
|
pub updated_at: DateTime<Utc>,
|
|
}
|
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
pub struct UpsertDeveloperProfilePayload {
|
|
pub display_name: Option<String>,
|
|
pub bio: Option<String>,
|
|
pub location: Option<String>,
|
|
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> {
|
|
sqlx::query_as::<_, DeveloperProfile>(
|
|
r#"SELECT id, user_id, display_name, bio, location,
|
|
custom_data,
|
|
status, created_at, updated_at
|
|
FROM developer_profiles WHERE user_id = $1"#,
|
|
)
|
|
.bind(user_id)
|
|
.fetch_optional(pool)
|
|
.await
|
|
}
|
|
|
|
pub async fn upsert(pool: &PgPool, user_id: Uuid, p: UpsertDeveloperProfilePayload) -> Result<DeveloperProfile, sqlx::Error> {
|
|
sqlx::query_as::<_, DeveloperProfile>(
|
|
r#"INSERT INTO developer_profiles (user_id, display_name, bio, location, custom_data)
|
|
VALUES ($1, $2, $3, $4, $5)
|
|
ON CONFLICT (user_id) DO UPDATE SET
|
|
display_name = COALESCE(EXCLUDED.display_name, developer_profiles.display_name),
|
|
bio = EXCLUDED.bio,
|
|
location = EXCLUDED.location,
|
|
custom_data = EXCLUDED.custom_data,
|
|
updated_at = NOW()
|
|
RETURNING id, user_id, display_name, bio, location,
|
|
custom_data,
|
|
status, created_at, updated_at"#,
|
|
)
|
|
.bind(user_id)
|
|
.bind(p.display_name)
|
|
.bind(p.bio)
|
|
.bind(p.location)
|
|
.bind(p.custom_data)
|
|
.fetch_one(pool)
|
|
.await
|
|
}
|
|
}
|