Skip to content

Commit

Permalink
minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
JojiiOfficial committed Mar 20, 2021
1 parent dea3f59 commit d0ecb59
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 19 deletions.
6 changes: 4 additions & 2 deletions src/handlers/list_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ pub async fn ep_list_files(
request: Json<FileList>,
user: Authenticateduser,
) -> Result<Json<FileListResponse>, RestError> {
let found = File::search(&pool.get()?, &request, user.user)?
let files = File::search(&pool.get()?, &request, user.user)?
.into_iter()
// Map fond files to a responable format
.map(|(file, namespace, attr)| -> response::FileItemResponse {
let mut res: response::FileItemResponse = file.into();
res.attributes.namespace = namespace.name;

let (tags, groups): (Vec<Attribute>, Vec<Attribute>) = attr
.into_iter()
.partition(|i| i.type_.eq(&AttributeType::Tag));
Expand All @@ -38,5 +40,5 @@ pub async fn ep_list_files(
})
.collect();

Ok(Json(FileListResponse { files: found }))
Ok(Json(FileListResponse { files }))
}
24 changes: 11 additions & 13 deletions src/handlers/namespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ pub async fn ep_create_namespace(
return Err(RestError::IllegalOperation);
}

let db = pool.get()?;

web::block(move || -> Result<(), RestError> {
namespace::CreateNamespace::new(&req.name, user.user.id).create(&pool.get()?)
namespace::CreateNamespace::new(&req.name, user.user.id).create(&db)
})
.await??;

Expand Down Expand Up @@ -62,12 +64,10 @@ pub async fn ep_delete_namespace(
}

web::block(move || -> Result<(), RestError> {
if let Some(ns) = Namespace::find_by_name(&db, &req.name, user.user.id)? {
ns.delete(&db, &config)?;
Ok(())
} else {
Err(RestError::DNotFound(Origin::Namespace))
}
let ns = Namespace::find_by_name(&db, &req.name, user.user.id)?
.ok_or(RestError::DNotFound(Origin::Namespace))?;

ns.delete(&db, &config)
})
.await??;

Expand Down Expand Up @@ -97,12 +97,10 @@ pub async fn ep_rename_namespace(
let db = pool.get()?;

web::block(move || -> Result<(), RestError> {
if let Some(ns) = Namespace::find_by_name(&db, &req.name, user.user.id)? {
ns.rename(&db, new_name.as_ref())?;
Ok(())
} else {
Err(RestError::DNotFound(Origin::Namespace))
}
let ns = Namespace::find_by_name(&db, &req.name, user.user.id)?
.ok_or(RestError::DNotFound(Origin::Namespace))?;

ns.rename(&db, new_name.as_ref())
})
.await??;

Expand Down
1 change: 0 additions & 1 deletion src/handlers/upload_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,6 @@ impl UploadBuffer {
// ensure to only pop actually overflowing items
.skip(left_to_fill)
.take(bytes.len() - left_to_fill)
// Put in right order
.copied()
.collect_vec()
} else {
Expand Down
6 changes: 3 additions & 3 deletions src/handlers/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ pub async fn ep_register(
config: web::Data<Config>,
req: web::Json<CredentialsRequest>,
) -> Result<Json<Success>, RestError> {
let req = req.into_inner();

// Don't allow the registration ep if disabled in config
config
.server
Expand All @@ -30,10 +32,8 @@ pub async fn ep_register(
return Err(RestError::BadRequest);
}

let new_user = User::new(req.username, req.password);
let db = pool.get()?;

let new_user = User::new(req.username.clone(), req.password.clone());

web::block(move || new_user.create(&db)).await??;

Ok(SUCCESS)
Expand Down

0 comments on commit d0ecb59

Please sign in to comment.