Skip to content

Commit

Permalink
add /v1/developers/:id
Browse files Browse the repository at this point in the history
  • Loading branch information
Fleeym committed Jun 5, 2024
1 parent ade0638 commit d22ec81
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/endpoints/developers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,36 @@ pub async fn get_me(auth: Auth) -> Result<impl Responder, ApiError> {
}))
}

#[derive(Deserialize)]
struct GetDeveloperPath {
id: i32,
}

#[get("v1/developers/{id}")]
pub async fn get_developer(
data: web::Data<AppData>,
path: web::Path<GetDeveloperPath>,
) -> Result<impl Responder, ApiError> {
let mut pool = data.db.acquire().await.or(Err(ApiError::DbAcquireError))?;
let result = Developer::get_one(path.id, &mut pool).await?;

if result.is_none() {
return Err(ApiError::NotFound("Developer not found".to_string()));
}

let result = result.unwrap();
Ok(web::Json(ApiResponse {
error: "".to_string(),
payload: DeveloperProfile {
id: result.id,
username: result.username,
display_name: result.display_name,
verified: result.verified,
admin: result.admin,
},
}))
}

#[put("v1/developers/{id}")]
pub async fn update_developer(
auth: Auth,
Expand Down
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,10 @@ async fn main() -> anyhow::Result<()> {
.service(endpoints::auth::github::poll_github_login)
.service(endpoints::auth::github::start_github_login)
.service(endpoints::developers::developer_index)
.service(endpoints::developers::get_developer)
.service(endpoints::developers::add_developer_to_mod)
.service(endpoints::developers::remove_dev_from_mod)
.service(endpoints::developers::delete_token)
.service(endpoints::developers::delete_tokens)
.service(endpoints::developers::update_profile)
.service(endpoints::developers::get_own_mods)
Expand Down
31 changes: 31 additions & 0 deletions src/types/models/developer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,37 @@ impl Developer {
count,
})
}

pub async fn get_one(
id: i32,
pool: &mut PgConnection,
) -> Result<Option<FetchedDeveloper>, ApiError> {
let result = match sqlx::query_as!(
FetchedDeveloper,
"SELECT
id,
username,
display_name,
verified,
admin
FROM developers
WHERE id = $1
",
id
)
.fetch_optional(&mut *pool)
.await
{
Ok(d) => d,
Err(e) => {
log::error!("{}", e);
return Err(ApiError::DbError);
}
};

Ok(result)
}

pub async fn create(
github_id: i64,
username: String,
Expand Down

0 comments on commit d22ec81

Please sign in to comment.