-
Notifications
You must be signed in to change notification settings - Fork 32
added validator section #94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
PoulavBhowmick03
wants to merge
2
commits into
skill-mind:master
Choose a base branch
from
PoulavBhowmick03:validator_section
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| -- Add migration script here | ||
|
|
||
| -- validators table | ||
| CREATE TABLE IF NOT EXISTS validators ( | ||
| id UUID PRIMARY KEY DEFAULT gen_random_uuid(), | ||
| wallet_address TEXT NOT NULL UNIQUE, | ||
| name TEXT NOT NULL, | ||
| bio TEXT, | ||
| created_at TIMESTAMPTZ NOT NULL DEFAULT now(), | ||
| updated_at TIMESTAMPTZ NOT NULL DEFAULT now() | ||
| ); | ||
|
|
||
| -- programming languages master list | ||
| CREATE TABLE IF NOT EXISTS programming_languages ( | ||
| id UUID PRIMARY KEY DEFAULT gen_random_uuid(), | ||
| name TEXT NOT NULL UNIQUE | ||
| ); | ||
|
|
||
| -- expertise areas master list | ||
| CREATE TABLE IF NOT EXISTS expertise_areas ( | ||
| id UUID PRIMARY KEY DEFAULT gen_random_uuid(), | ||
| name TEXT NOT NULL UNIQUE | ||
| ); | ||
|
|
||
| -- many‑to‑many from validators → languages | ||
| CREATE TABLE IF NOT EXISTS validator_programming_languages ( | ||
| validator_id UUID NOT NULL REFERENCES validators(id) ON DELETE CASCADE, | ||
| language_id UUID NOT NULL REFERENCES programming_languages(id) ON DELETE CASCADE, | ||
| PRIMARY KEY (validator_id, language_id) | ||
| ); | ||
|
|
||
| -- many‑to‑many from validators → expertise | ||
| CREATE TABLE IF NOT EXISTS validator_expertise_areas ( | ||
| validator_id UUID NOT NULL REFERENCES validators(id) ON DELETE CASCADE, | ||
| expertise_id UUID NOT NULL REFERENCES expertise_areas(id) ON DELETE CASCADE, | ||
| PRIMARY KEY (validator_id, expertise_id) | ||
| ); | ||
|
|
||
| -- index for fast wallet lookups | ||
| CREATE INDEX IF NOT EXISTS idx_validators_wallet | ||
| ON validators(wallet_address); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| use axum::{ | ||
| Router, | ||
| extract::{Json, State}, | ||
| http::StatusCode, | ||
| routing::post, | ||
| }; | ||
| use sqlx::{Executor, Postgres}; | ||
|
|
||
| use super::types::{RegisterValidatorParams, ValidatorProfile}; | ||
| use crate::AppState; | ||
|
|
||
| pub(crate) fn router() -> Router<AppState> { | ||
| Router::new().route("/validators", post(register_validator)) | ||
| } | ||
|
|
||
| async fn register_validator( | ||
| State(state): State<AppState>, | ||
| Json(payload): Json<RegisterValidatorParams>, | ||
| ) -> Result<(StatusCode, Json<ValidatorProfile>), StatusCode> { | ||
| let mut tx = state | ||
| .db | ||
| .pool | ||
| .begin() | ||
| .await | ||
| .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?; | ||
|
|
||
| // 2) insert validator (UUID PK) | ||
| let row = sqlx::query!( | ||
|
Check failure on line 28 in src/http/validators.rs
|
||
| r#" | ||
| INSERT INTO validators(wallet_address, name, bio) | ||
| VALUES ($1, $2, $3) | ||
| ON CONFLICT(wallet_address) DO NOTHING | ||
| RETURNING id AS "validator_id!: Uuid", | ||
| wallet_address, | ||
| name, | ||
| bio, | ||
| created_at, | ||
| updated_at | ||
| "#, | ||
| payload.wallet_address, | ||
| payload.name, | ||
| payload.bio, | ||
| ) | ||
| .fetch_optional(&mut *tx) | ||
| .await | ||
| .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)? | ||
| .ok_or(StatusCode::CONFLICT)?; // 409 on duplicate | ||
|
|
||
| // helper: upsert into a text‑lookup table, returning its numeric ID | ||
| async fn upsert_and_get_id( | ||
| executor: impl Executor<'_, Database = Postgres>, // Generic executor | ||
| table: &str, | ||
| value: &str, | ||
| ) -> Result<i32, sqlx::Error> { | ||
| let sql = format!( | ||
| "INSERT INTO {table} (value) VALUES ($1) ON CONFLICT (value) DO UPDATE SET value = EXCLUDED.value RETURNING id", | ||
| table = table | ||
| ); | ||
| sqlx::query_scalar(&sql) | ||
| .bind(value) | ||
| .fetch_one(executor) | ||
| .await | ||
| } | ||
|
|
||
| // 3) languages (note: payload.programming_languages) | ||
| for lang in &payload.programming_lang { | ||
| let lang_id: i32 = upsert_and_get_id(&mut *tx, "programming_languages", lang) | ||
| .await | ||
| .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?; | ||
| sqlx::query!( | ||
|
Check failure on line 70 in src/http/validators.rs
|
||
| r#" | ||
| INSERT INTO validator_programming_lang | ||
| (validator_id, language_id) | ||
| VALUES ($1, $2) | ||
| ON CONFLICT DO NOTHING | ||
| "#, | ||
| row.validator_id, | ||
| lang_id, | ||
| ) | ||
| .execute(&mut tx) | ||
| .await | ||
| .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?; | ||
| } | ||
|
|
||
| // 4) expertise areas (note: payload.expertise_areas) | ||
| for area in &payload.expertise_area { | ||
| let exp_id: i32 = upsert_and_get_id(&mut *tx, "expertise_areas", area) | ||
| .await | ||
| .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?; | ||
| sqlx::query!( | ||
|
Check failure on line 90 in src/http/validators.rs
|
||
| r#" | ||
| INSERT INTO validator_expertise_area | ||
| (validator_id, expertise_id) | ||
| VALUES ($1, $2) | ||
| ON CONFLICT DO NOTHING | ||
| "#, | ||
| row.validator_id, | ||
| exp_id, | ||
| ) | ||
| .execute(&mut tx) | ||
| .await | ||
| .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?; | ||
| } | ||
|
|
||
| // 5) commit | ||
| tx.commit() | ||
| .await | ||
| .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?; | ||
|
|
||
| // 6) build our JSON response | ||
| let profile = ValidatorProfile { | ||
| validator_id: row.validator_id, | ||
| wallet_address: row.wallet_address, | ||
| name: row.name, | ||
| bio: row.bio, | ||
| programming_languages: payload.programming_lang.clone(), | ||
| expertise_areas: payload.expertise_area.clone(), | ||
| created_at: row.created_at, | ||
| updated_at: row.updated_at, | ||
| }; | ||
|
|
||
| Ok((StatusCode::CREATED, Json(profile))) | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @PoulavBhowmick03 after going through the
setupmigration, anything particularly wrong with the validators table/schema in the migration script that spurred you to go ahead and create yours?