Skip to content

Commit d9530c9

Browse files
committed
feat: add notification/<id> route
1 parent e515ba1 commit d9530c9

File tree

2 files changed

+59
-6
lines changed

2 files changed

+59
-6
lines changed

src/controllers/notification_controller.rs

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,36 @@ use crate::{
33
custom::error::CustomError,
44
models::notification_model::Notification,
55
};
6-
use mongodb::{bson::doc, sync::Collection};
6+
use mongodb::{
7+
bson::{doc, oid::ObjectId},
8+
sync::Collection,
9+
};
710
use rocket::{http::Status, serde::json::Json, State};
811

12+
fn NOTIF_NOT_FOUND(id: String) -> String {
13+
format!("Notification with id {} not found", id)
14+
}
15+
916
impl MongoRepo {
10-
pub fn get_notifications(&self) -> Result<Vec<Notification>, String> {
17+
pub fn get_notification(&self, id: &String) -> Result<Notification, String> {
18+
let obj_id = match ObjectId::parse_str(id) {
19+
Ok(i) => i,
20+
Err(error) => return Err(error.to_string()),
21+
};
22+
23+
let filter = doc! {"_id": obj_id};
24+
let col: Collection<Notification> = self.db.collection("notifications");
25+
let notification = match col.find_one(filter, None).ok() {
26+
Some(n) => n,
27+
None => return Err(DB_CON_ERR.to_string()),
28+
};
29+
match notification {
30+
Some(n) => Ok(n),
31+
None => Err(NOTIF_NOT_FOUND(obj_id.to_string())),
32+
}
33+
}
34+
35+
pub fn get_all_notifications(&self) -> Result<Vec<Notification>, String> {
1136
let col: Collection<Notification> = self.db.collection("notifications");
1237
let cursors = match col.find(None, None).ok() {
1338
Some(c) => c,
@@ -18,9 +43,36 @@ impl MongoRepo {
1843
}
1944
}
2045

46+
#[get("/notification/<path>")]
47+
pub fn get_notification(
48+
db: &State<MongoRepo>,
49+
path: String,
50+
) -> Result<Json<Notification>, CustomError> {
51+
let id = path;
52+
if id.is_empty() {
53+
return Err(CustomError::Default(Status::BadRequest));
54+
};
55+
let notification = db.get_notification(&id);
56+
match notification {
57+
Ok(notification) => Ok(Json(notification)),
58+
Err(error) => {
59+
if error == NOTIF_NOT_FOUND(id) {
60+
Err(CustomError::ServiceUnavailable(Status::BadRequest, error))
61+
} else {
62+
Err(CustomError::ServiceUnavailable(
63+
Status::ServiceUnavailable,
64+
error,
65+
))
66+
}
67+
}
68+
}
69+
}
70+
2171
#[get("/notifications")]
22-
pub fn get_notifications(db: &State<MongoRepo>) -> Result<Json<Vec<Notification>>, CustomError> {
23-
let notifications = db.get_notifications();
72+
pub fn get_all_notifications(
73+
db: &State<MongoRepo>,
74+
) -> Result<Json<Vec<Notification>>, CustomError> {
75+
let notifications = db.get_all_notifications();
2476
match notifications {
2577
Ok(notifications) => {
2678
if notifications.is_empty() {

src/main.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use rocket::{
1616

1717
use crate::controllers::contactinfo_controller::get_contactinfo;
1818
use crate::controllers::location_controller::get_locations;
19-
use crate::controllers::notification_controller::get_notifications;
19+
use crate::controllers::notification_controller::{get_all_notifications, get_notification};
2020
use crate::controllers::service_controller::get_services;
2121
use crate::controllers::MongoRepo;
2222
use custom::error::CustomError;
@@ -74,7 +74,8 @@ async fn rocket() -> _ {
7474
"/",
7575
routes![
7676
get_services,
77-
get_notifications,
77+
get_notification,
78+
get_all_notifications,
7879
get_contactinfo,
7980
get_locations
8081
],

0 commit comments

Comments
 (0)