-
Notifications
You must be signed in to change notification settings - Fork 0
feat: refacto users endpoints #80
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
Quentintnrl
wants to merge
18
commits into
mair-69-refacto-auth
Choose a base branch
from
mair-44-refacto-users-endpoints
base: mair-69-refacto-auth
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
18 commits
Select commit
Hold shift + click to select a range
f2a5591
fix: update mairie360_api_lib version
Quentintnrl 5c52185
fix: remove depreciated type
Quentintnrl 4447ee4
del: remove previous auth middleware code
Quentintnrl 489f7f0
feat: re organise tests, add permissions and rights queries, add tests
Quentintnrl 6a82f48
fix: fix tests
Quentintnrl 5b77a82
feat: add get accesses by ressource id
Quentintnrl e12945c
feat: add ressources access endpoints
Quentintnrl 522a63a
feat: add groups doc
Quentintnrl 0b26054
feat: add groups database queries
Quentintnrl f5a8b59
feat: add group queries tests
Quentintnrl c4a8bc4
feat: add database group check
Quentintnrl 3bcaa6b
feat: add better params check
Quentintnrl af105f0
feat: add better security
Quentintnrl 72b88a1
Merge branch 'main' into mair-73-add-group-management
Quentintnrl bc01b34
fix: fix cicd errors
Quentintnrl 29a964e
Merge branch 'mair-69-refacto-auth' into mair-44-refacto-users-endpoints
Quentintnrl 39deca9
fix: fix endpoints tag
Quentintnrl 7fd59ff
feat: refacto users endpoints
Quentintnrl 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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,6 @@ | ||
| mod query; | ||
| pub use query::get_user_by_id_query; | ||
|
|
||
| mod view; | ||
| pub use view::GetUserByIdQueryResultView; | ||
| pub use view::GetUserByIdQueryView; |
12 changes: 6 additions & 6 deletions
12
src/database/users/about/query.rs → src/database/users/get_user_by_id/query.rs
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 |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| pub mod about; | ||
| pub mod get_user_by_id; | ||
| pub mod patch_user; |
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,5 @@ | ||
| mod query; | ||
| pub use query::patch_user_query; | ||
|
|
||
| mod view; | ||
| pub use view::PatchUserQueryView; |
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,56 @@ | ||
| use crate::database::users::patch_user::PatchUserQueryView; | ||
| use mairie360_api_lib::database::errors::DatabaseError; | ||
| use sqlx::PgPool; | ||
|
|
||
| pub async fn patch_user_query( | ||
| view: PatchUserQueryView, | ||
| pool: &PgPool, | ||
| ) -> Result<(), DatabaseError> { | ||
| let mut query_builder = sqlx::QueryBuilder::new("UPDATE users SET "); | ||
|
|
||
| // On utilise un booléen pour savoir si on a déjà ajouté un champ | ||
| let mut first = true; | ||
|
|
||
| // Macro pour ajouter proprement chaque champ | ||
| macro_rules! add_field { | ||
| ($field_name:expr, $value:expr) => { | ||
| if !first { | ||
| query_builder.push(", "); | ||
| } | ||
| query_builder.push($field_name); | ||
| query_builder.push(" = "); | ||
| query_builder.push_bind($value); | ||
| first = false; | ||
| }; | ||
| } | ||
|
|
||
| if let Some(first_name) = view.first_name() { | ||
| add_field!("first_name", first_name); | ||
| } | ||
| if let Some(last_name) = view.last_name() { | ||
| add_field!("last_name", last_name); | ||
| } | ||
| if let Some(email) = view.email() { | ||
| add_field!("email", email); | ||
| } | ||
| if let Some(phone_number) = view.phone_number() { | ||
| add_field!("phone_number", phone_number); | ||
| } | ||
|
|
||
| // Si aucun champ n'a été ajouté, on arrête tout | ||
| if first { | ||
| return Ok(()); | ||
| } | ||
|
|
||
| // Ajout de la clause WHERE | ||
| query_builder.push(" WHERE id = "); | ||
| query_builder.push_bind(view.id() as i32); | ||
|
|
||
| query_builder | ||
| .build() | ||
| .execute(pool) | ||
| .await | ||
| .map_err(DatabaseError::from)?; | ||
|
|
||
| Ok(()) | ||
| } | ||
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,75 @@ | ||
| use mairie360_api_lib::database::db_interface::DatabaseQueryView; | ||
| use std::fmt::Display; | ||
|
|
||
| pub struct PatchUserQueryView { | ||
| id: u64, | ||
| first_name: Option<String>, | ||
| last_name: Option<String>, | ||
| email: Option<String>, | ||
| phone_number: Option<String>, | ||
| } | ||
|
|
||
| impl PatchUserQueryView { | ||
| pub fn new( | ||
| id: u64, | ||
| first_name: Option<&str>, | ||
| last_name: Option<&str>, | ||
| email: Option<&str>, | ||
| phone_number: Option<&str>, | ||
| ) -> Self { | ||
| Self { | ||
| id, | ||
| first_name: first_name.map(|s| s.to_string()), | ||
| last_name: last_name.map(|s| s.to_string()), | ||
| email: email.map(|s| s.to_string()), | ||
| phone_number: phone_number.map(|s| s.to_string()), | ||
| } | ||
| } | ||
|
|
||
| pub fn id(&self) -> u64 { | ||
| self.id | ||
| } | ||
|
|
||
| pub fn first_name(&self) -> Option<&str> { | ||
| self.first_name.as_deref() | ||
| } | ||
| pub fn last_name(&self) -> Option<&str> { | ||
| self.last_name.as_deref() | ||
| } | ||
| pub fn email(&self) -> Option<&str> { | ||
| self.email.as_deref() | ||
| } | ||
| pub fn phone_number(&self) -> Option<&str> { | ||
| self.phone_number.as_deref() | ||
| } | ||
| } | ||
|
|
||
| impl DatabaseQueryView for PatchUserQueryView { | ||
| fn get_request(&self) -> String { | ||
| let mut request = "UPDATE users SET ".to_string(); | ||
| if let Some(first_name) = &self.first_name { | ||
| request.push_str(&format!("first_name = '{}', ", first_name)); | ||
| } | ||
| if let Some(last_name) = &self.last_name { | ||
| request.push_str(&format!("last_name = '{}', ", last_name)); | ||
| } | ||
| if let Some(email) = &self.email { | ||
| request.push_str(&format!("email = '{}', ", email)); | ||
| } | ||
| if let Some(phone_number) = &self.phone_number { | ||
| request.push_str(&format!("phone_number = '{}', ", phone_number)); | ||
| } | ||
| request.push_str(&format!("WHERE id = {}", self.id)); | ||
| request.push_str(" RETURNING true"); | ||
| request | ||
| } | ||
|
Quentintnrl marked this conversation as resolved.
|
||
| } | ||
|
|
||
| impl Display for PatchUserQueryView { | ||
| fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
| match self.phone_number { | ||
|
Quentintnrl marked this conversation as resolved.
|
||
| Some(_) => write!(f, "PatchUserQueryView: id = {:?}, first_name = {:?}, last_name = {:?}, email = {:?}, phone_number = {:?}", self.id(), self.first_name(), self.last_name(), self.email(), self.phone_number()), | ||
| None => write!(f, "PatchUserQueryView: id = {:?}, first_name = {:?}, last_name = {:?}, email = {:?}", self.id(), self.first_name(), self.last_name(), self.email()), | ||
| } | ||
| } | ||
| } | ||
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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.