Skip to content

Commit d22ec81

Browse files
committed
add /v1/developers/:id
1 parent ade0638 commit d22ec81

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

src/endpoints/developers.rs

+30
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,36 @@ pub async fn get_me(auth: Auth) -> Result<impl Responder, ApiError> {
289289
}))
290290
}
291291

292+
#[derive(Deserialize)]
293+
struct GetDeveloperPath {
294+
id: i32,
295+
}
296+
297+
#[get("v1/developers/{id}")]
298+
pub async fn get_developer(
299+
data: web::Data<AppData>,
300+
path: web::Path<GetDeveloperPath>,
301+
) -> Result<impl Responder, ApiError> {
302+
let mut pool = data.db.acquire().await.or(Err(ApiError::DbAcquireError))?;
303+
let result = Developer::get_one(path.id, &mut pool).await?;
304+
305+
if result.is_none() {
306+
return Err(ApiError::NotFound("Developer not found".to_string()));
307+
}
308+
309+
let result = result.unwrap();
310+
Ok(web::Json(ApiResponse {
311+
error: "".to_string(),
312+
payload: DeveloperProfile {
313+
id: result.id,
314+
username: result.username,
315+
display_name: result.display_name,
316+
verified: result.verified,
317+
admin: result.admin,
318+
},
319+
}))
320+
}
321+
292322
#[put("v1/developers/{id}")]
293323
pub async fn update_developer(
294324
auth: Auth,

src/main.rs

+2
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,10 @@ async fn main() -> anyhow::Result<()> {
107107
.service(endpoints::auth::github::poll_github_login)
108108
.service(endpoints::auth::github::start_github_login)
109109
.service(endpoints::developers::developer_index)
110+
.service(endpoints::developers::get_developer)
110111
.service(endpoints::developers::add_developer_to_mod)
111112
.service(endpoints::developers::remove_dev_from_mod)
113+
.service(endpoints::developers::delete_token)
112114
.service(endpoints::developers::delete_tokens)
113115
.service(endpoints::developers::update_profile)
114116
.service(endpoints::developers::get_own_mods)

src/types/models/developer.rs

+31
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,37 @@ impl Developer {
124124
count,
125125
})
126126
}
127+
128+
pub async fn get_one(
129+
id: i32,
130+
pool: &mut PgConnection,
131+
) -> Result<Option<FetchedDeveloper>, ApiError> {
132+
let result = match sqlx::query_as!(
133+
FetchedDeveloper,
134+
"SELECT
135+
id,
136+
username,
137+
display_name,
138+
verified,
139+
admin
140+
FROM developers
141+
WHERE id = $1
142+
",
143+
id
144+
)
145+
.fetch_optional(&mut *pool)
146+
.await
147+
{
148+
Ok(d) => d,
149+
Err(e) => {
150+
log::error!("{}", e);
151+
return Err(ApiError::DbError);
152+
}
153+
};
154+
155+
Ok(result)
156+
}
157+
127158
pub async fn create(
128159
github_id: i64,
129160
username: String,

0 commit comments

Comments
 (0)