Skip to content

Commit

Permalink
47 user delete rating. partially
Browse files Browse the repository at this point in the history
  • Loading branch information
smart--petea committed Apr 12, 2024
1 parent 842df59 commit 9b130dd
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 1 deletion.
3 changes: 3 additions & 0 deletions migrations/20240412141011_casbin_user_rating_edit.down.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
DELETE FROM casbin_rule
WHERE ptype = 'p' and v0 = 'group_user' and v1 = '/rating/:id' and v2 = 'PUT';

DELETE FROM casbin_rule
WHERE ptype = 'p' and v0 = 'group_user' and v1 = '/rating/:id' and v2 = 'DELETE';

DELETE FROM casbin_rule
WHERE ptype = 'p' and v0 = 'group_admin' and v1 = '/admin/rating/:id' and v2 = 'GET';

Expand Down
4 changes: 4 additions & 0 deletions migrations/20240412141011_casbin_user_rating_edit.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ INSERT INTO casbin_rule
(id, ptype, v0, v1, v2, v3, v4, v5)
VALUES((select max(id) + 1 from casbin_rule cr), 'p', 'group_user', '/rating/:id', 'PUT', '', '', '');

INSERT INTO casbin_rule
(id, ptype, v0, v1, v2, v3, v4, v5)
VALUES((select max(id) + 1 from casbin_rule cr), 'p', 'group_user', '/rating/:id', 'DELETE', '', '', '');

INSERT INTO casbin_rule
(id, ptype, v0, v1, v2, v3, v4, v5)
VALUES((select max(id) + 1 from casbin_rule cr), 'p', 'group_admin', '/admin/rating/:id', 'GET', '', '', '');
Expand Down
25 changes: 25 additions & 0 deletions src/routes/rating/delete.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use crate::db;
use crate::helpers::JsonResponse;
use crate::models;
use actix_web::{delete, web, Responder, Result};
use sqlx::PgPool;

#[tracing::instrument(name = "User delete rating.")]
#[delete("/{id}")]
pub async fn user_delete_handler(
path: web::Path<(i32,)>,
pg_pool: web::Data<PgPool>,
) -> Result<impl Responder> {
let rate_id = path.0;
let rating = db::rating::fetch(pg_pool.get_ref(), rate_id)
.await
.map_err(|_err| JsonResponse::<models::Rating>::build().internal_server_error(""))
.and_then(|rating| {
match rating {
Some(rating) if rating.hidden == Some(false) => { Ok(rating) },
_ => Err(JsonResponse::<models::Rating>::build().not_found("not found"))
}
})?;

Ok(JsonResponse::build().set_item(rating).ok("OK"))
}
2 changes: 2 additions & 0 deletions src/routes/rating/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
pub mod add;
pub mod get;
mod edit;
mod delete;

pub use add::*;
pub use get::*;
pub use edit::*;
pub use delete::*;
3 changes: 2 additions & 1 deletion src/startup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,10 @@ pub async fn run(
)
.service(
web::scope("/rating")
.service(routes::rating::user_add_handler)
.service(routes::rating::anonymous_get_handler)
.service(routes::rating::anonymous_list_handler)
.service(routes::rating::user_add_handler)
.service(routes::rating::user_delete_handler)
.service(routes::rating::user_edit_handler),
)
.service(
Expand Down

0 comments on commit 9b130dd

Please sign in to comment.