Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
deatil committed Jul 27, 2024
1 parent c03ca2a commit baa5c0d
Show file tree
Hide file tree
Showing 33 changed files with 331 additions and 240 deletions.
40 changes: 20 additions & 20 deletions src/app/controller/admin/art.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ pub async fn list(
count: count,
};

Ok(nako_http::success_response_json("获取成功", res))
Ok(nako_http::success_json("获取成功", res))
}

// ==========================
Expand Down Expand Up @@ -173,13 +173,13 @@ pub async fn create_save(
let db = &state.db;

if params.cate_id == 0 {
return Ok(nako_http::error_response_json("所属分类不能为空"));
return Ok(nako_http::error_json("所属分类不能为空"));
}
if params.title.as_str() == "" {
return Ok(nako_http::error_response_json("文章不能为空"));
return Ok(nako_http::error_json("文章不能为空"));
}
if params.status != 0 && params.status != 1 {
return Ok(nako_http::error_response_json("状态不能为空"));
return Ok(nako_http::error_json("状态不能为空"));
}

let add_time = time::now().timestamp();
Expand All @@ -204,10 +204,10 @@ pub async fn create_save(
..entity::default()
}).await;
if !create_data.is_ok() {
return Ok(nako_http::error_response_json("添加失败"));
return Ok(nako_http::error_json("添加失败"));
}

Ok(nako_http::success_response_json("添加成功", ""))
Ok(nako_http::success_json("添加成功", ""))
}

// ==========================
Expand Down Expand Up @@ -283,7 +283,7 @@ pub async fn update_save(
params: web::Form<UpdateForm>,
) -> Result<HttpResponse, Error> {
if query.id == 0 {
return Ok(nako_http::error_response_json("ID不能为空"));
return Ok(nako_http::error_json("ID不能为空"));
}

let vali_data = UpdateValidate{
Expand All @@ -297,14 +297,14 @@ pub async fn update_save(

let vali = vali_data.validate();
if vali.is_err() {
return Ok(nako_http::error_response_json(format!("{}", vali.unwrap_err()).as_str()));
return Ok(nako_http::error_json(format!("{}", vali.unwrap_err()).as_str()));
}

let db = &state.db;

let info = art::ArtModel::find_by_id(db, query.id).await.unwrap_or_default().unwrap_or_default();
if info.id == 0 {
return Ok(nako_http::error_response_json("要更改的文章不存在"));
return Ok(nako_http::error_json("要更改的文章不存在"));
}

let add_time = time::parse(params.add_time.as_str()).timestamp();
Expand All @@ -327,10 +327,10 @@ pub async fn update_save(
})
.await;
if data.is_err() {
return Ok(nako_http::error_response_json("更新失败"));
return Ok(nako_http::error_json("更新失败"));
}

Ok(nako_http::success_response_json("更新成功", ""))
Ok(nako_http::success_json("更新成功", ""))
}

// ==========================
Expand All @@ -348,20 +348,20 @@ pub async fn delete(
let db = &state.db;

if query.id == 0 {
return Ok(nako_http::error_response_json("ID不能为空"));
return Ok(nako_http::error_json("ID不能为空"));
}

let data = art::ArtModel::find_by_id(db, query.id).await.unwrap_or_default().unwrap_or_default();
if data.id == 0 {
return Ok(nako_http::error_response_json("要删除的文章不存在"));
return Ok(nako_http::error_json("要删除的文章不存在"));
}

let delete_data = art::ArtModel::delete(db, query.id).await;
if delete_data.is_err() {
return Ok(nako_http::error_response_json("删除失败"));
return Ok(nako_http::error_json("删除失败"));
}

Ok(nako_http::success_response_json("删除成功", ""))
Ok(nako_http::success_json("删除成功", ""))
}

// ==========================
Expand All @@ -386,16 +386,16 @@ pub async fn update_status(
let db = &state.db;

if query.id == 0 {
return Ok(nako_http::error_response_json("ID不能为空"));
return Ok(nako_http::error_json("ID不能为空"));
}

if params.status != 0 && params.status != 1 {
return Ok(nako_http::error_response_json("状态不能为空"));
return Ok(nako_http::error_json("状态不能为空"));
}

let data = art::ArtModel::find_by_id(db, query.id).await.unwrap_or_default().unwrap_or_default();
if data.id == 0 {
return Ok(nako_http::error_response_json("要更改的文章不存在"));
return Ok(nako_http::error_json("要更改的文章不存在"));
}

// 更新
Expand All @@ -405,9 +405,9 @@ pub async fn update_status(
})
.await;
if status.is_err() {
return Ok(nako_http::error_response_json("更新失败"));
return Ok(nako_http::error_json("更新失败"));
}

Ok(nako_http::success_response_json("更新成功", ""))
Ok(nako_http::success_json("更新成功", ""))
}

10 changes: 5 additions & 5 deletions src/app/controller/admin/attach.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ pub async fn list(
count: count,
};

Ok(nako_http::success_response_json("获取成功", res))
Ok(nako_http::success_json("获取成功", res))
}

// ==========================
Expand Down Expand Up @@ -141,17 +141,17 @@ pub async fn delete(
let db = &state.db;

if query.id == 0 {
return Ok(nako_http::error_response_json("ID不能为空"));
return Ok(nako_http::error_json("ID不能为空"));
}

let data = attach::AttachModel::find_by_id(db, query.id).await.unwrap_or_default().unwrap_or_default();
if data.id == 0 {
return Ok(nako_http::error_response_json("要删除的附件不存在"));
return Ok(nako_http::error_json("要删除的附件不存在"));
}

let status = attach::AttachModel::delete(db, query.id).await;
if status.is_err() {
return Ok(nako_http::error_response_json("删除失败"));
return Ok(nako_http::error_json("删除失败"));
}

let mut upload_path = upload_path(data.path.clone());
Expand All @@ -161,7 +161,7 @@ pub async fn delete(

fs::remove_file(upload_path).unwrap_or_default();

Ok(nako_http::success_response_json("删除成功", ""))
Ok(nako_http::success_json("删除成功", ""))
}

// ==========================
Expand Down
16 changes: 8 additions & 8 deletions src/app/controller/admin/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ pub async fn login_check(
) -> Result<HttpResponse, Error> {
let login_id = session.get::<u32>("login_id").unwrap_or_default().unwrap_or_default();
if login_id > 0 {
return Ok(nako_http::error_response_json("你已经登陆了"));
return Ok(nako_http::error_json("你已经登陆了"));
}

let vali_data = LoginValidate{
Expand All @@ -129,19 +129,19 @@ pub async fn login_check(

let vali = vali_data.validate();
if vali.is_err() {
return Ok(nako_http::error_response_json(format!("{}", vali.unwrap_err()).as_str()));
return Ok(nako_http::error_json(format!("{}", vali.unwrap_err()).as_str()));
}

let auth_captcha = session.get::<String>("auth_captcha").unwrap_or_default().unwrap_or_default();
if params.captcha.to_uppercase() != auth_captcha.to_uppercase() {
return Ok(nako_http::error_response_json("验证码错误"));
return Ok(nako_http::error_json("验证码错误"));
}

let db = &state.db;
let user_info = user::UserModel::find_user_by_name(db, params.name.as_str()).await.unwrap_or_default().unwrap_or_default();

if user_info.id == 0 {
return Ok(nako_http::error_response_json("账号或者密码错误"));
return Ok(nako_http::error_json("账号或者密码错误"));
}

let pass = user_info.password.unwrap_or("".to_string());
Expand All @@ -157,21 +157,21 @@ pub async fn login_check(

// 验证密码
if !nako_auth::password_verify(depass.as_str(), pass.as_str()) {
return Ok(nako_http::error_response_json("账号或者密码错误"));
return Ok(nako_http::error_json("账号或者密码错误"));
}

let status = user_info.status.unwrap_or(0);
if status == 0 {
return Ok(nako_http::error_response_json("账号不存在或者已被禁用"));
return Ok(nako_http::error_json("账号不存在或者已被禁用"));
}

if session.insert("login_id", user_info.id).is_err() {
return Ok(nako_http::error_response_json("登陆失败"));
return Ok(nako_http::error_json("登陆失败"));
}

session.remove(AUTH_KEY);

Ok(nako_http::success_response_json("登陆成功", ""))
Ok(nako_http::success_json("登陆成功", ""))
}

// 退出
Expand Down
44 changes: 22 additions & 22 deletions src/app/controller/admin/cate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ pub async fn list(
count: count,
};

Ok(nako_http::success_response_json("获取成功", res))
Ok(nako_http::success_json("获取成功", res))
}

// ==========================
Expand Down Expand Up @@ -151,18 +151,18 @@ pub async fn create_save(
let db = &state.db;

if params.name.as_str() == "" {
return Ok(nako_http::error_response_json("分类不能为空"));
return Ok(nako_http::error_json("分类不能为空"));
}
if params.slug.as_str() == "" {
return Ok(nako_http::error_response_json("标识不能为空"));
return Ok(nako_http::error_json("标识不能为空"));
}
if params.status != 0 && params.status != 1 {
return Ok(nako_http::error_response_json("状态不能为空"));
return Ok(nako_http::error_json("状态不能为空"));
}

let data = cate::CateModel::find_by_slug(db, params.slug.as_str()).await.unwrap_or_default().unwrap_or_default();
if data.id > 0 {
return Ok(nako_http::error_response_json("分类标识已经存在"));
return Ok(nako_http::error_json("分类标识已经存在"));
}

let add_time = time::now().timestamp();
Expand All @@ -185,10 +185,10 @@ pub async fn create_save(
..entity::default()
}).await;
if create_data.is_ok() {
return Ok(nako_http::success_response_json("添加成功", ""));
return Ok(nako_http::success_json("添加成功", ""));
}

Ok(nako_http::error_response_json("添加失败"))
Ok(nako_http::error_json("添加失败"))
}

// ==========================
Expand Down Expand Up @@ -267,7 +267,7 @@ pub async fn update_save(
params: web::Form<UpdateForm>,
) -> Result<HttpResponse, Error> {
if query.id == 0 {
return Ok(nako_http::error_response_json("ID不能为空"));
return Ok(nako_http::error_json("ID不能为空"));
}

let vali_data = UpdateValidate{
Expand All @@ -282,20 +282,20 @@ pub async fn update_save(

let vali = vali_data.validate();
if vali.is_err() {
return Ok(nako_http::error_response_json(format!("{}", vali.unwrap_err()).as_str()));
return Ok(nako_http::error_json(format!("{}", vali.unwrap_err()).as_str()));
}

let db = &state.db;

let info = cate::CateModel::find_by_id(db, query.id).await.unwrap_or_default().unwrap_or_default();
if info.id == 0 {
return Ok(nako_http::error_response_json("要更改的分类不存在"));
return Ok(nako_http::error_json("要更改的分类不存在"));
}

let info_by_name = cate::CateModel::find_by_slug(db, params.slug.as_str()).await.unwrap_or_default().unwrap_or_default();
if info_by_name.id > 0 {
if info.id != info_by_name.id {
return Ok(nako_http::error_response_json("分类标识已经存在"));
return Ok(nako_http::error_json("分类标识已经存在"));
}
}

Expand All @@ -313,10 +313,10 @@ pub async fn update_save(
})
.await;
if data.is_err() {
return Ok(nako_http::error_response_json("更新失败"));
return Ok(nako_http::error_json("更新失败"));
}

Ok(nako_http::success_response_json("更新成功", ""))
Ok(nako_http::success_json("更新成功", ""))
}

// ==========================
Expand All @@ -334,20 +334,20 @@ pub async fn delete(
let db = &state.db;

if query.id == 0 {
return Ok(nako_http::error_response_json("ID不能为空"));
return Ok(nako_http::error_json("ID不能为空"));
}

let data = cate::CateModel::find_by_id(db, query.id).await.unwrap_or_default().unwrap_or_default();
if data.id == 0 {
return Ok(nako_http::error_response_json("要删除的分类不存在"));
return Ok(nako_http::error_json("要删除的分类不存在"));
}

let delete_data = cate::CateModel::delete(db, query.id).await;
if delete_data.is_err() {
return Ok(nako_http::error_response_json("删除失败"));
return Ok(nako_http::error_json("删除失败"));
}

Ok(nako_http::success_response_json("删除成功", ""))
Ok(nako_http::success_json("删除成功", ""))
}

// ==========================
Expand All @@ -372,16 +372,16 @@ pub async fn update_status(
let db = &state.db;

if query.id == 0 {
return Ok(nako_http::error_response_json("ID不能为空"));
return Ok(nako_http::error_json("ID不能为空"));
}

if params.status != 0 && params.status != 1 {
return Ok(nako_http::error_response_json("状态不能为空"));
return Ok(nako_http::error_json("状态不能为空"));
}

let data = cate::CateModel::find_by_id(db, query.id).await.unwrap_or_default().unwrap_or_default();
if data.id == 0 {
return Ok(nako_http::error_response_json("要更改的分类不存在"));
return Ok(nako_http::error_json("要更改的分类不存在"));
}

// 更新
Expand All @@ -391,9 +391,9 @@ pub async fn update_status(
})
.await;
if status.is_err() {
return Ok(nako_http::error_response_json("更新失败"));
return Ok(nako_http::error_json("更新失败"));
}

Ok(nako_http::success_response_json("更新成功", ""))
Ok(nako_http::success_json("更新成功", ""))
}

Loading

0 comments on commit baa5c0d

Please sign in to comment.