Skip to content

Commit

Permalink
feat: get repo by i (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
Extheoisah authored Nov 21, 2024
1 parent 3f2acd9 commit b8f7d70
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/app/routes/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use log::error;
use reqwest::StatusCode;
use serde::{Deserialize, Serialize};
use serde_json::json;
use uuid::Uuid;
#[derive(Debug, Serialize, Deserialize)]
pub struct CreateRepoResponse {
repo_name: String,
Expand All @@ -31,6 +32,7 @@ pub struct CreateRepoRequest {

#[derive(Serialize, Deserialize)]
pub struct GetRepoQuery {
id: Option<Uuid>,
repo_url: Option<String>,
soft_serve_url: Option<String>,
status: Option<Status>,
Expand Down Expand Up @@ -223,7 +225,11 @@ async fn get_repo(
query: web::Query<GetRepoQuery>,
pool: web::Data<DbPool>,
) -> Result<HttpResponse, RepositoryError> {
if query.repo_url.is_some() && query.soft_serve_url.is_some() && query.status.is_some() {
if query.repo_url.is_some()
&& query.soft_serve_url.is_some()
&& query.status.is_some()
&& query.id.is_some()
{
return Err(RepositoryError::BadRequest(
"Multiple parameters are not allowed. Please provide only one parameter.".to_string(),
));
Expand All @@ -243,6 +249,14 @@ async fn get_repo(
}
};

if let Some(id) = query.id {
let repository = Repository::get_repo_by_id(&mut conn, &id, &user_id).map_err(|e| {
error!("Error getting repository: {}", e);
RepositoryError::DatabaseError(e.to_string())
})?;
return Ok(HttpResponse::Ok().json(repository));
}

let pagination = PaginationParams {
page: query.page,
per_page: query.per_page,
Expand Down
102 changes: 102 additions & 0 deletions src/service/repository/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,4 +303,106 @@ impl Repository {
total_pages,
})
}

pub fn get_repo_by_id(
connection: &mut PgConnection,
id: &Uuid,
user_id: &Uuid,
) -> Result<RepositoryWithRelations> {
use crate::schema::{challenges, progress, repositories};

let result = repositories::table
.inner_join(challenges::table)
.inner_join(
progress::table.on(progress::user_id
.eq(repositories::user_id)
.and(progress::challenge_id.eq(repositories::challenge_id))),
)
.filter(repositories::user_id.eq(user_id))
.filter(repositories::id.eq(id))
.select((
repositories::id,
repositories::user_id,
repositories::challenge_id,
repositories::repo_url,
repositories::soft_serve_url,
repositories::created_at,
repositories::updated_at,
(
challenges::title,
challenges::description,
challenges::repo_url,
challenges::difficulty,
challenges::module_count,
challenges::mode,
challenges::created_at,
challenges::updated_at,
),
(
progress::id,
progress::status,
progress::progress_details,
progress::created_at,
progress::updated_at,
),
))
.first::<(
Uuid,
Uuid,
Uuid,
String,
String,
NaiveDateTime,
NaiveDateTime,
(
String,
String,
String,
String,
i32,
String,
NaiveDateTime,
NaiveDateTime,
),
(
Uuid,
String,
Option<serde_json::Value>,
NaiveDateTime,
NaiveDateTime,
),
)>(connection)
.map_err(|e| {
error!("Error getting repository with relations: {}", e);
anyhow::anyhow!("Failed to get repository with relations")
})?;

// Transform the raw result into our nested structure
Ok(RepositoryWithRelations {
id: result.0,
user_id: result.1,
challenge_id: result.2,
repo_url: result.3,
soft_serve_url: result.4,
created_at: result.5,
updated_at: result.6,
challenge: ChallengeInfo {
title: result.7 .0,
description: result.7 .1,
repo_url: result.7 .2,
difficulty: result.7 .3,
module_count: result.7 .4,
mode: result.7 .5,
created_at: result.7 .6,
updated_at: result.7 .7,
},
progress: ProgressInfo {
id: result.8 .0,
status: result.8 .1,
progress_details: result.8 .2,
created_at: result.8 .3,
updated_at: result.8 .4,
},
})
}
}

0 comments on commit b8f7d70

Please sign in to comment.