-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
98 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,42 @@ | ||
use axum::Extension; | ||
use axum::{Extension, Json}; | ||
use axum::extract::Path; | ||
use axum::http::StatusCode; | ||
use axum::response::IntoResponse; | ||
use crate::app::auth::AuthSession; | ||
use crate::app::web::AppState; | ||
use crate::json::error::error_message; | ||
use crate::json::notifications::UpdateNotificationPayload; | ||
|
||
pub async fn mark_as_read( | ||
pub async fn read( | ||
Extension(state): Extension<AppState>, | ||
auth_session: AuthSession, | ||
Path(notification_id): Path<u32>, | ||
Path(notification_id): Path<i32>, | ||
Json(payload): Json<UpdateNotificationPayload>, | ||
) -> impl IntoResponse { | ||
todo!() | ||
let login_user = auth_session.user.unwrap(); | ||
|
||
let mut tx = state.pool.begin().await.unwrap(); | ||
let query = sqlx::query(r#" | ||
UPDATE notifications | ||
SET read = $2 | ||
WHERE id = $1 AND user_id = $3 | ||
"#) | ||
.bind(notification_id) | ||
.bind(payload.read) | ||
.bind(login_user.id) | ||
.execute(&mut *tx) | ||
.await | ||
.unwrap(); | ||
|
||
if query.rows_affected() == 0 { | ||
tx.rollback().await.unwrap(); | ||
|
||
return error_message( | ||
StatusCode::UNAUTHORIZED, | ||
"Você não é o dono da notificação ou a notificação não existe", | ||
); | ||
} | ||
|
||
tx.commit().await.unwrap(); | ||
StatusCode::OK.into_response() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Serialize, Deserialize)] | ||
pub struct UpdateNotificationPayload { | ||
pub read: bool | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
INSERT INTO notifications (user_id, title, content, status, type) | ||
VALUES | ||
(1, 'Notification 1', 'This is the content of notification 1', 1, 1), | ||
(2, 'Notification 2', 'This is the content of notification 2', 1, 1), | ||
(3, 'Notification 3', 'This is the content of notification 3', 1, 1), | ||
(4, 'Notification 4', 'This is the content of notification 4', 1, 1), | ||
(5, 'Notification 5', 'This is the content of notification 5', 1, 1), | ||
(6, 'Notification 6', 'This is the content of notification 6', 1, 1), | ||
(7, 'Notification 7', 'This is the content of notification 7', 1, 1), | ||
(8, 'Notification 8', 'This is the content of notification 8', 1, 1), | ||
(9, 'Notification 9', 'This is the content of notification 9', 1, 1), | ||
(10, 'Notification 10', 'This is the content of notification 10', 1, 1); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
use axum_test::TestServer; | ||
use sqlx::{Pool, Postgres}; | ||
use rest_server::json::auth::LoginCreds; | ||
use rest_server::json::notifications::UpdateNotificationPayload; | ||
use crate::common::{login, test_app}; | ||
|
||
mod common; | ||
|
||
#[sqlx::test(fixtures("users", "notifications"))] | ||
async fn test_notifications(pool: Pool<Postgres>) { | ||
let server = &mut test_app(pool); | ||
|
||
test_read_notifications(server) | ||
.await; | ||
} | ||
|
||
async fn test_read_notifications(server: &TestServer) { | ||
login(server, LoginCreds { | ||
email: "john.doe@gmail.com".to_string(), | ||
password: "secured123456".to_string(), | ||
}) | ||
.await; | ||
|
||
server.patch("/api/v1/users/@me/notifications/1") | ||
.json(&UpdateNotificationPayload { | ||
read: true, | ||
}) | ||
.expect_success() | ||
.await; | ||
|
||
server.patch("/api/v1/users/@me/notifications/2") | ||
.json(&UpdateNotificationPayload { | ||
read: true, | ||
}) | ||
.expect_failure() | ||
.await; | ||
} |