Skip to content

Commit

Permalink
Fixed frontend state bug
Browse files Browse the repository at this point in the history
  • Loading branch information
TheButlah committed Feb 17, 2024
1 parent 8536669 commit d1f94f5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 31 deletions.
34 changes: 15 additions & 19 deletions apps/legacy_web/backend/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ async fn main() {
.route("/", get(|| async move { "welcome to Image upload api" }))
//route for uploading image or any file
.route("/upload/:username", post(upload_file))
.route("/create_user/:username", get(create_user))
.route("/get_avatars/:username", get(get_avatars))
.route("/delete_avatar/:username/:avatar_name", get(delete_avatar))
.route("/get_avatar/:username/:avatar_name", get(get_avatar_file))
Expand All @@ -50,32 +49,19 @@ async fn main() {
axum::serve(listener, app).await.unwrap();
}

async fn create_user(
Extension(mut db): Extension<Database>,
Extension(proj_dirs): Extension<ProjectDirs>,
Path(username): Path<String>,
) {
db.transaction(|data| {
if data.users.get(&username).is_none() {
println!("creating user: {}", username);
data.users
.insert(username.clone(), User::new(username.clone()));
} else {
println!("logging in as user: {}", username);
}
});
let path = proj_dirs.data_dir().join("avatars").join(username);
std::fs::create_dir_all(path).expect("failed to create directory for user");
}

#[axum_macros::debug_handler]
async fn upload_file(
Extension(mut db): Extension<Database>,
Extension(proj_dirs): Extension<ProjectDirs>,
Path(username): Path<String>,
mut files: Multipart,
) {
ensure_user_in_db(&mut db, &username).await;

let avatar_path = proj_dirs.data_dir().join("avatars").join(username.clone());
tokio::fs::create_dir_all(&avatar_path)
.await
.expect("unable to create avatar dir");

while let Some(file) = files.next_field().await.unwrap() {
// this is the name which is sent in formdata from frontend or whoever called the api, i am
Expand Down Expand Up @@ -158,3 +144,13 @@ async fn delete_avatar(
std::fs::remove_file(path).unwrap();
});
}

async fn ensure_user_in_db(db: &mut Database, username: &str) {
db.transaction(|data| {
if data.users.get(username).is_none() {
println!("creating user: {}", username);
data.users
.insert(username.to_owned(), User::new(username.to_owned()));
}
});
}
12 changes: 0 additions & 12 deletions apps/legacy_web/frontend/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,17 +93,6 @@ impl eframe::App for TemplateApp {
ui.text_edit_singleline(&mut self.temp_username);
if ui.button("login").clicked() {
self.username.replace(self.temp_username.clone());
let create_user_request = ehttp::Request::get(format!(
"{}/create_user/{}",
url,
self.username.as_ref().unwrap()
));
let ctx = ctx.clone();
let refresh_avatar = self.refresh_avatar_list(&ctx);
ehttp::fetch(create_user_request, move |_| {
ctx.request_repaint();
refresh_avatar();
});
}
return;
}
Expand Down Expand Up @@ -135,7 +124,6 @@ impl eframe::App for TemplateApp {
});
if ui.button("download").clicked() {
use wasm_bindgen::JsCast;
use web_sys;

let win = web_sys::window().unwrap();
let doc = win.document().unwrap();
Expand Down

0 comments on commit d1f94f5

Please sign in to comment.