Skip to content

Commit

Permalink
Add teacher oauth lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
Blckbrry-Pi committed Sep 22, 2023
1 parent d5beade commit 8698fe6
Show file tree
Hide file tree
Showing 9 changed files with 210 additions and 64 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 0 additions & 16 deletions sql_schema.txt

This file was deleted.

1 change: 1 addition & 0 deletions sql_schemas/v8/down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE teacher_oauths;
7 changes: 7 additions & 0 deletions sql_schemas/v8/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CREATE TABLE teacher_oauths (
teacher uuid NOT NULL REFERENCES teachers(id) ON DELETE CASCADE,
provider varchar(63) NOT NULL,
sub varchar(255) NOT NULL,

CONSTRAINT unique_sub_for_provider UNIQUE (teacher, provider)
);
101 changes: 54 additions & 47 deletions src/database/prepared/teacher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,53 +118,6 @@ pub async fn get_all_teachers(ctx: &mut Ctx) -> Result<Vec<Teacher>, sqlx::Error
}


// pub async fn update_chall(ctx: &mut Ctx, id: Uuid, input: ChallInput) -> Result<Option<Chall>, sqlx::Error> {
// let query = query!(
// r#"
// UPDATE challenges
// SET
// name = COALESCE($2, name),
// description = COALESCE($3, description),
// points = COALESCE($4, points),
// authors = COALESCE($5, authors),
// hints = COALESCE($6, hints),
// categories = COALESCE($7, categories),
// tags = COALESCE($8, tags),
// visible = COALESCE($9, visible),
// source_folder = COALESCE($10, source_folder)
// WHERE id = $1;
// "#,
// id,
// input.name: String,
// input.description,
// input.points,
// input.authors.as_deref(),
// input.hints.as_deref(),
// input.categories.as_deref(),
// input.tags.as_deref(),
// input.visible,
// input.source_folder,
// );
// let affected = query
// .execute(&mut *ctx)
// .await?
// .rows_affected();

// if affected != 1 { return Ok(None) }

// if let Some(links) = input.links {
// set_chall_links(&mut *ctx, id, links).await?;
// }
// set_chall_updated(&mut *ctx, id).await?;

// let Some(output) = get_chall(ctx, id).await? else {
// return Err(sqlx::Error::RowNotFound);
// };

// Ok(Some(output))
// }



struct Id { id: Uuid }

Expand Down Expand Up @@ -332,3 +285,57 @@ pub async fn update_teacher_full_absence(ctx: &mut Ctx, id: Uuid, fully_absent:

get_teacher(ctx, id).await
}



pub async fn get_teacher_by_oauth(ctx: &mut Ctx, provider: String, sub: String) -> Result<Teacher, sqlx::Error> {
let teacher_oauth_query = query_as!(
Id,
r#"
SELECT teacher as id
FROM teacher_oauths
WHERE
provider = $1 AND
sub = $2;
"#,
provider,
sub,
);

let teacher_id: Id = teacher_oauth_query.fetch_one(&mut **ctx).await?;

get_teacher(ctx, teacher_id.id).await
}

pub async fn add_teacher_oauth(ctx: &mut Ctx, teacher: Uuid, provider: String, sub: String) -> Result<(), sqlx::Error> {
let add_teacher_oauth = query!(
r#"
INSERT INTO teacher_oauths (teacher, provider, sub)
VALUES ($1, $2, $3);
"#,
teacher,
provider,
sub,
);

add_teacher_oauth.execute(&mut **ctx).await?;

Ok(())
}

pub async fn remove_teacher_oauth(ctx: &mut Ctx, teacher: Uuid, provider: String) -> Result<(), sqlx::Error> {
let remove_teacher_oauth = query!(
r#"
DELETE FROM teacher_oauths
WHERE
teacher = $1 AND
provider = $2;
"#,
teacher,
provider,
);

remove_teacher_oauth.execute(&mut **ctx).await?;

Ok(())
}
67 changes: 67 additions & 0 deletions src/graphql/resolvers/mutation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,73 @@ impl MutationRoot {
})
}

async fn add_teacher_associated_oauth(
&self,
ctx_accessor: &Context<'_>,
id: Uuid,
provider: String,
sub: String,
) -> GraphQlResult<Teacher> {
use crate::database::prepared::teacher::add_teacher_oauth as add_teacher_associated_oauth_in_db;

let ctx = ctx_accessor.data::<AppState>()?;

let mut db_conn = ctx.db()
.acquire()
.await
.map_err(|e| {
let e = e.to_string();
GraphQlError::new(format!("Could not open connection to the database: {e}"))
})?;

add_teacher_associated_oauth_in_db(&mut db_conn, id, provider, sub)
.await
.map_err(|e| {
let e = e.to_string();
GraphQlError::new(format!("Database error: {e}"))
})?;

get_teacher(&mut db_conn, id)
.await
.map_err(|e| {
let e = e.to_string();
GraphQlError::new(format!("Database error: {e}"))
})
}

async fn remove_teacher_associated_oauth(
&self,
ctx_accessor: &Context<'_>,
id: Uuid,
provider: String,
) -> GraphQlResult<Teacher> {
use crate::database::prepared::teacher::remove_teacher_oauth as remove_teacher_associated_oauth_in_db;

let ctx = ctx_accessor.data::<AppState>()?;

let mut db_conn = ctx.db()
.acquire()
.await
.map_err(|e| {
let e = e.to_string();
GraphQlError::new(format!("Could not open connection to the database: {e}"))
})?;

remove_teacher_associated_oauth_in_db(&mut db_conn, id, provider)
.await
.map_err(|e| {
let e = e.to_string();
GraphQlError::new(format!("Database error: {e}"))
})?;

get_teacher(&mut db_conn, id)
.await
.map_err(|e| {
let e = e.to_string();
GraphQlError::new(format!("Database error: {e}"))
})
}

// async fn delete_teacher(
// ctx: &Context,
// id: TeacherId,
Expand Down
28 changes: 27 additions & 1 deletion src/graphql/resolvers/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl QueryRoot {
.await
.map_err(|e| {
let e = e.to_string();
GraphQlError::new(format!("Failed to get teachers from database {e}"))
GraphQlError::new(format!("Failed to get teacher from database {e}"))
})
}

Expand Down Expand Up @@ -89,6 +89,32 @@ impl QueryRoot {
})
}

async fn get_teacher_by_oauth(
&self,
ctx_accessor: &Context<'_>,
#[graphql(desc = "Provider of OAuth")] provider: String,
#[graphql(desc = "Sub of OAuth")] sub: String,
) -> GraphQlResult<Teacher> {
use crate::database::prepared::teacher::get_teacher_by_oauth as get_teacher_by_oauth_from_db;

let ctx = ctx_accessor.data::<AppState>()?;

let mut db_conn = ctx.db()
.acquire()
.await
.map_err(|e| {
let e = e.to_string();
GraphQlError::new(format!("Could not open connection to the database {e}"))
})?;

get_teacher_by_oauth_from_db(&mut db_conn, provider, sub)
.await
.map_err(|e| {
let e = e.to_string();
GraphQlError::new(format!("Failed to get teacher from database {e}"))
})
}

async fn all_periods(
&self,
ctx: &Context<'_>,
Expand Down

0 comments on commit 8698fe6

Please sign in to comment.