Skip to content

Commit

Permalink
Merge pull request #75 from avored/74-asset-table-page-bug
Browse files Browse the repository at this point in the history
fixed the asset bug
  • Loading branch information
indpurvesh authored Nov 17, 2023
2 parents 0718d92 + d327126 commit 7fb1a3d
Show file tree
Hide file tree
Showing 8 changed files with 549 additions and 393 deletions.
863 changes: 481 additions & 382 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ tower-http = { version = "0.4.4", features = ["fs"] }
dotenvy = "0.15.7"
async-session = "3.0.0"
axum-extra = { version = "0.7.7", features = ["cookie", "cookie-signed"] }
futures = "0.3.28"
futures = "0.3.29"
tower = "0.4.13"
argon2 = "0.5.2"
avored_better_query = { path = "./avored_better_query" }
validator = { version = "0.16.1", features = ["derive"] }
rand = "0.8.5"
urlencoding = "2.1.3"
serde_json = "1.0.107"
serde_json = "1.0.108"
surrealdb = { version = "1.0.0", features = ["kv-rocksdb"] }
19 changes: 19 additions & 0 deletions public/css/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -890,6 +890,11 @@ select {
margin-right: auto;
}

.my-3 {
margin-top: 0.75rem;
margin-bottom: 0.75rem;
}

.-mr-2 {
margin-right: -0.5rem;
}
Expand Down Expand Up @@ -986,6 +991,10 @@ select {
margin-top: 2rem;
}

.mb-2 {
margin-bottom: 0.5rem;
}

.block {
display: block;
}
Expand Down Expand Up @@ -2025,6 +2034,16 @@ select {
--tw-ring-color: rgb(168 85 247 / var(--tw-ring-opacity));
}

.ring-red-700 {
--tw-ring-opacity: 1;
--tw-ring-color: rgb(185 28 28 / var(--tw-ring-opacity));
}

.ring-red-400 {
--tw-ring-opacity: 1;
--tw-ring-color: rgb(248 113 113 / var(--tw-ring-opacity));
}

.ring-opacity-5 {
--tw-ring-opacity: 0.05;
}
Expand Down
2 changes: 1 addition & 1 deletion public/js/app.js

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions resources/js/asset-manager.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
export default () => ({
modal: false,
error_message: '',
files: [],
init() {
console.log("asset manager")
console.log("asset manager init")
},
close_modal() {
this.modal = false
},
save_asset() {
console.log("Test Save Asset")
var formData = new FormData();
//var imagefile = document.querySelector('#file');
this.files.forEach((f) => formData.append('file', f));
Expand All @@ -18,6 +18,10 @@ export default () => ({
'Content-Type': 'multipart/form-data'
}
}).then((res) => {
if (res.data.success === false) {
this.error_message = "Only Jpeg and PNG file is allowed only."
return
}
location.reload()
})
},
Expand Down
32 changes: 29 additions & 3 deletions src/api/asset/handlers/store_asset_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ use axum::extract::Multipart;
use rand::distributions::Alphanumeric;
use rand::Rng;
use serde::Serialize;
use crate::models::asset_model::CreatableAssetModel;
use crate::models::asset_model::{AssetModel, CreatableAssetModel};

const ALLOW_TYPES: [&'static str; 3] = ["image/jpeg", "image/jpg", "image/png"];

pub async fn store_asset_handler(
session: AvoRedSession,
Expand All @@ -26,6 +28,7 @@ pub async fn store_asset_handler(
};
let mut creatable_asset_model = CreatableAssetModel::default();
creatable_asset_model.logged_in_username = logged_in_user.email;
let mut is_allow_file_type = true;

while let Some(field) = multipart.next_field().await.unwrap() {
let name = field.name().unwrap().to_string();
Expand All @@ -37,7 +40,14 @@ pub async fn store_asset_handler(
.map(char::from)
.collect();

let file_type = field.content_type().unwrap().to_string();
creatable_asset_model.file_type = field.content_type().unwrap().to_string();

is_allow_file_type = ALLOW_TYPES.contains(&file_type.as_str());

if !is_allow_file_type {
continue;
}
let file_name = field.file_name().unwrap().to_string();
let data = field.bytes().await.unwrap();
creatable_asset_model.file_size = i64::try_from(data.len()).unwrap_or(0);
Expand All @@ -60,14 +70,30 @@ pub async fn store_asset_handler(
}
}

if !is_allow_file_type {
let asset_model = AssetModel::default();
let creatable_asset_response = AssetResponseViewModel {
asset_model,
success: false
};

return Ok(Json(creatable_asset_response).into_response())
}

let asset_model = state.asset_service
.create_asset(&state.db, creatable_asset_model)
.await?;

Ok(Json(asset_model).into_response())
let creatable_asset_response = AssetResponseViewModel {
asset_model,
success: true
};

Ok(Json(creatable_asset_response).into_response())
}

#[derive(Serialize)]
pub struct AssetResponseViewModel {
pub asset_path: String,
pub asset_model: AssetModel,
pub success: bool
}
7 changes: 5 additions & 2 deletions src/repositories/asset_repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,12 @@ impl AssetRepository {
Some(object) => object,
None => Err(Error::Generic("no record found")),
};
let model_count: Result<ModelCount> = result_object?.try_into();
let total_count = match result_object {
Ok(obj) => obj.try_into(),
Err(_) => Ok(ModelCount::default()),
};

model_count
total_count
}
//
// pub async fn find_by_id(
Expand Down
7 changes: 6 additions & 1 deletion views/asset/asset-table.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,12 @@
<div class="py-5">
<!-- MODAL CONTENT START -->
<div class="flex">
<div class="mt-5">
<div class="mt-3">
<div x-show="error_message.length > 0" x-text="error_message"
class="px-5 mb-2 py-3 rounded text-red-600 ring-1 ring-red-400">

</div>

{{> components/label
label=(translate_key "file")
for="file"
Expand Down

0 comments on commit 7fb1a3d

Please sign in to comment.