Skip to content

Commit

Permalink
feat: add notifications read route
Browse files Browse the repository at this point in the history
  • Loading branch information
luisfbl committed Feb 23, 2024
1 parent a4d4a0b commit 8708cab
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 10 deletions.
3 changes: 3 additions & 0 deletions rest-server/migrations/08_notifications.sql
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
CREATE TABLE IF NOT EXISTS "notifications" (
id SERIAL PRIMARY KEY,
user_id INT REFERENCES "users"(id),
title VARCHAR(64) NOT NULL,
content VARCHAR(256) NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
"read" BOOLEAN NOT NULL DEFAULT FALSE,
status SMALLINT NOT NULL,
type SMALLINT NOT NULL
);

Expand Down
4 changes: 2 additions & 2 deletions rest-server/src/api/v1/users/me/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use axum::response::IntoResponse;
use common::entities::UserRole;
use crate::app::auth::AuthSession;
use crate::app::web::AppState;
use crate::json::users::{UserMePayload, UserType};
use crate::json::users::{UserMeResponse, UserType};
use crate::models::users::{CustomerUser, ProtectedUser, ViewerUser};

pub async fn me(
Expand Down Expand Up @@ -59,5 +59,5 @@ pub async fn me(

tx.commit().await;

Json(UserMePayload { user: protected_user, info: user_info })
Json(UserMeResponse { user: protected_user, info: user_info })
}
4 changes: 2 additions & 2 deletions rest-server/src/api/v1/users/me/notifications/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::app::web::AppState;

pub async fn notifications(
Extension(state): Extension<AppState>,
auth_session: AuthSession,
auth_session: AuthSession
) -> impl IntoResponse {
todo!()

}
2 changes: 1 addition & 1 deletion rest-server/src/api/v1/users/me/notifications/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ mod patch;
pub fn router() -> Router {
Router::new()
.route("/", get(get::notifications))
.route("/:notification_id/read", patch(patch::mark_as_read))
.route("/:notification_id", patch(patch::read))
}
37 changes: 33 additions & 4 deletions rest-server/src/api/v1/users/me/notifications/patch.rs
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()
}
1 change: 1 addition & 0 deletions rest-server/src/json/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use sqlx::types::chrono::NaiveDateTime;
pub mod auth;
pub mod error;
pub mod users;
pub mod notifications;

pub fn serialize_timestamp<S>(naive_datetime: &NaiveDateTime, serializer: S) -> Result<S::Ok, S::Error>
where
Expand Down
6 changes: 6 additions & 0 deletions rest-server/src/json/notifications.rs
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
}
2 changes: 1 addition & 1 deletion rest-server/src/json/users.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use serde::{Serialize, Serializer};
use crate::models::users::{CustomerUser, ProtectedUser, SellerUser, ViewerUser};

#[derive(Serialize)]
pub struct UserMePayload {
pub struct UserMeResponse {
pub user: ProtectedUser,
pub info: Option<UserType>
}
Expand Down
12 changes: 12 additions & 0 deletions rest-server/tests/fixtures/notifications.sql
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);
37 changes: 37 additions & 0 deletions rest-server/tests/notifications.rs
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;
}

0 comments on commit 8708cab

Please sign in to comment.