-
-
Notifications
You must be signed in to change notification settings - Fork 402
feat/add-popular-tasks #1708
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
floscodes
wants to merge
21
commits into
loco-rs:feat/add-popular-tasks
Choose a base branch
from
floscodes:feat/add-popular-tasks
base: feat/add-popular-tasks
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.
+170
−24
Open
feat/add-popular-tasks #1708
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
85ac30e
Merge branch 'feat/add-popular-tasks' of https://github.com/floscodes…
floscodes 8f3c616
chore: user creation via CLI prompts
floscodes f73a400
fix: add dialoguer crate
floscodes 1f7eff6
chore: usage prompt
floscodes 76c186f
chore: implement user_delete task
floscodes 9577f13
chore: continue writing test for user deletion
floscodes 110d513
chore(setup-script): add user_delete task
floscodes d3490de
chore: run tasks by using CLI asks rather than prompts, add user_dele…
floscodes dd49180
fix: remove unused import
floscodes 6df0e11
Merge branch 'loco-rs:feat/add-popular-tasks' into feat/add-popular-t…
floscodes 3e0f0ab
chore: remove dialoguer dependency in base template
floscodes b20cc30
chore: refactor user deletion task
floscodes e34372f
chore: refactor code and test
floscodes 9c822b4
chore: remove unused deps
floscodes 872c136
chore: add to lowercase to confirmation force flag
floscodes c366abd
chore: refactor force flag check code, remove emojis and add logging
floscodes bd26dc9
fix: clone username, pid and email for logging before deleting user
floscodes b53b67d
fix: snapshots
floscodes 356cf81
style: refactor user_delete task method
floscodes d9a9f99
fix: add groups migration file
floscodes d341274
chore: uncomment test code
floscodes 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
24 changes: 24 additions & 0 deletions
24
loco-new/base_template/migration/src/m20220101_000002_groups.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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| use loco_rs::schema::*; | ||
| use sea_orm_migration::prelude::*; | ||
|
|
||
| #[derive(DeriveMigrationName)] | ||
| pub struct Migration; | ||
|
|
||
| #[async_trait::async_trait] | ||
| impl MigrationTrait for Migration { | ||
| async fn up(&self, m: &SchemaManager) -> Result<(), DbErr> { | ||
| create_table( | ||
| m, | ||
| "groups", | ||
| &[("id", ColType::PkAuto), ("name", ColType::StringUniq)], | ||
| &[], | ||
| ) | ||
| .await?; | ||
| Ok(()) | ||
| } | ||
|
|
||
| async fn down(&self, m: &SchemaManager) -> Result<(), DbErr> { | ||
| drop_table(m, "groups").await?; | ||
| 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
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,3 +1,4 @@ | ||
| {%- if settings.auth %} | ||
| pub mod user_create; | ||
| pub mod user_delete; | ||
| {%- endif %} |
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 |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| use crate::models::_entities::users; | ||
| use loco_rs::prelude::*; | ||
|
|
||
| pub struct UserDelete; | ||
| #[async_trait] | ||
| impl Task for UserDelete { | ||
| fn task(&self) -> TaskInfo { | ||
| TaskInfo { | ||
| name: "user:delete".to_string(), | ||
| detail: "Delete a user by entering pid.\nUsage:\ncargo loco run task user:delete" | ||
| .to_string(), | ||
| } | ||
| } | ||
| async fn run(&self, app_context: &AppContext, vars: &task::Vars) -> Result<()> { | ||
| let Ok(input) = vars.cli_arg("pid") else { | ||
| return Err(Error::string("pid is mandatory")); | ||
| }; | ||
| let force_flag = vars | ||
| .cli_arg("force") | ||
| .map(|v| v.trim().to_lowercase() == "true") | ||
| .unwrap_or(false); | ||
|
|
||
| let user_to_delete = users::Model::find_by_pid(&app_context.db, input).await?; | ||
|
|
||
| println!( | ||
| "User to delete:\nUsername: {}\nEmail: {}\nPID: {}", | ||
| user_to_delete.name, user_to_delete.email, user_to_delete.pid | ||
| ); | ||
|
|
||
| if !force_flag { | ||
| println!( | ||
| "Are you sure you want to delete the user {}\n({})\nwith pid '{}'?\nType 'yes' and hit enter to confirm", | ||
| user_to_delete.name, user_to_delete.email, user_to_delete.pid | ||
| ); | ||
| let mut confirm = String::new(); | ||
| let stdin = std::io::stdin(); | ||
| stdin.read_line(&mut confirm).map_err(|err| { | ||
| tracing::error!( | ||
| message = err.to_string(), | ||
| "could not read confirmation input" | ||
| ); | ||
| Error::string(&format!("Failed to read confirmation input. err: {err}",)) | ||
| })?; | ||
|
|
||
| if confirm.trim().to_lowercase() != "yes" { | ||
| println!("User deletion cancelled - nothing has been deleted!"); | ||
| return Ok(()); | ||
| } | ||
| } | ||
|
|
||
| let user_name = user_to_delete.name.clone(); | ||
| let user_email = user_to_delete.email.clone(); | ||
| let user_pid = user_to_delete.pid; | ||
|
|
||
| let _deleted_user = user_to_delete | ||
| .into_active_model() | ||
| .delete(&app_context.db) | ||
| .await | ||
| .map_err(|err| { | ||
| tracing::error!(message = err.to_string(), "could not delete user"); | ||
| Error::string(&format!("Failed to delete user. err: {err}",)) | ||
| })?; | ||
| println!("User deleted successfully!"); | ||
| tracing::info!( | ||
| pid = user_pid.to_string(), | ||
| username = user_name, | ||
| email = user_email, | ||
| "User deleted" | ||
| ); | ||
|
|
||
| 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 |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| {%- if settings.auth %} | ||
| pub mod user_create; | ||
| pub mod user_delete; | ||
| {%- endif %} |
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,35 @@ | ||
| use loco_rs::{task, testing::prelude::*}; | ||
| use {{settings.module_name}}::{app::App, models::users}; | ||
|
|
||
| use loco_rs::boot::run_task; | ||
| use serial_test::serial; | ||
|
|
||
|
|
||
| #[tokio::test] | ||
| #[serial] | ||
| async fn can_run_user_delete_by_pid() { | ||
| let boot = boot_test::<App>().await.unwrap(); | ||
|
|
||
| let user = users::Model::create_with_password( | ||
| &boot.app_context.db, | ||
| &users::RegisterParams { | ||
| email: "test@example.com".to_string(), | ||
| password: "securepassword".to_string(), | ||
| name: "Test User".to_string(), | ||
| }, | ||
| ) | ||
| .await | ||
| .unwrap(); | ||
|
|
||
| let pid = user.pid; | ||
|
|
||
| let vars = task::Vars::from_cli_args(vec![("pid".to_string(), pid.to_string()), ("force".to_string(), "true".to_string())]); | ||
|
|
||
| run_task::<App>(&boot.app_context, Some(&"user:delete".to_string()), &vars) | ||
| .await | ||
| .unwrap(); | ||
|
|
||
| let user = users::Model::find_by_pid(&boot.app_context.db, &pid.to_string()).await; | ||
| assert!(user.is_err()); | ||
| } | ||
|
|
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 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
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.