Fakultas Ilmu Komputer UI

Skip to content
Snippets Groups Projects
Commit 8184b1d1 authored by DawnFall19's avatar DawnFall19
Browse files

Implement unsubscribe function in Notification service.

parent 8d56ad71
No related branches found
No related tags found
No related merge requests found
use std::thread;
use rocket::http::Status;
use rocket::log;
use rocket::http::{Accept, Status};
use rocket::{log, request};
use rocket::serde::json::to_string;
use rocket::tokio;
use bambangshop_receiver::{APP_CONFIG, REQWEST_CLIENT, Result, compose_error_response};
use crate::controller::notification;
use crate::model::notification::Notification;
use crate::model::subscriber::SubscriberRequest;
use crate::repository::notification::NotificationRepository;
......@@ -53,4 +54,40 @@ impl NotificationService {
return thread::spawn(move || Self::subscribe_request(product_type_clone))
.join().unwrap();
}
#[tokio::main]
async fn unsubscribe_request(product_type: String) -> Result<SubscriberRequest> {
let product_type_upper: String = product_type.to_uppercase();
let product_type_str: &str = product_type_upper.as_str();
let notification_receiver_url: String = format!("{}/receive",
APP_CONFIG.get_instance_root_url());
let request_url: String = format!("{}/notification/unsubscribe/{}?url={}",
APP_CONFIG.get_publisher_root_url(), product_type_str, notification_receiver_url);
let request = REQWEST_CLIENT
.post(request_url.clone())
.header("Content-Type", "application/json")
.header("Accept", "application/json")
.send().await;
log::warn_!("Sent unsubscribe request to: {}", request_url);
return match request {
Ok(f) => match f.json::<SubscriberRequest>().await {
Ok(x) => Ok(x),
Err(_y) => Err(compose_error_response(
Status::NotFound,
String::from("Already unsubscribed to the topic.")
))
},
Err(e) => Err(compose_error_response(
Status::NotFound,
e.to_string()
))
}
}
pub fn unsubscribe(product_type: &str) -> Result<SubscriberRequest> {
let product_type_clone = String::from(product_type);
return thread::spawn(move || Self::unsubscribe_request(product_type_clone))
.join().unwrap();
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment