-
Notifications
You must be signed in to change notification settings - Fork 7
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
1 parent
99408e7
commit dab1202
Showing
15 changed files
with
202 additions
and
24 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
extern crate rocket; | ||
use std::error::Error; | ||
|
||
use primitives::Subscription; | ||
use storage::DatabaseClient; | ||
|
||
pub struct SubscriptionsClient { | ||
database: DatabaseClient, | ||
} | ||
|
||
impl SubscriptionsClient { | ||
pub async fn new( | ||
database_url: &str | ||
) -> Self { | ||
let database = DatabaseClient::new(database_url); | ||
Self { | ||
database, | ||
} | ||
} | ||
|
||
pub fn add_subscriptions(&mut self, device_id: &str, subscriptions: Vec<Subscription>) -> Result<usize, Box<dyn Error>> { | ||
let device = self.database.get_device(device_id)?; | ||
let subscriptions = subscriptions | ||
.into_iter() | ||
.map(|x| storage::models::Subscription::from_primitive(x, device.id)) | ||
.collect(); | ||
let result = self.database.add_subscriptions(subscriptions)?; | ||
|
||
return Ok(result) | ||
} | ||
|
||
pub fn get_subscriptions(&mut self, device_id: &str) -> Result<Vec<primitives::Subscription>, Box<dyn Error>> { | ||
let subscriptions = self.database | ||
.get_subscriptions_by_device_id(device_id)? | ||
.into_iter() | ||
.map(|x| x.as_primitive()) | ||
.collect(); | ||
return Ok(subscriptions) | ||
} | ||
|
||
pub fn delete_subscriptions(&mut self, device_id: &str, subscriptions: Vec<Subscription>) -> Result<usize, Box<dyn Error>> { | ||
let device = self.database.get_device(device_id)?; | ||
let values = subscriptions | ||
.into_iter() | ||
.map(|x| storage::models::Subscription::from_primitive(x, device.id)) | ||
.collect::<Vec<storage::models::Subscription>>(); | ||
|
||
//TODO: Implement to delete all subscriptions at once | ||
let mut result = 0; | ||
for subscription in values { | ||
let size = self.database.delete_subscription(subscription)?; | ||
result += size; | ||
} | ||
Ok(result) | ||
} | ||
} |
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,39 @@ | ||
extern crate rocket; | ||
use primitives::Subscription; | ||
use rocket::serde::json::Json; | ||
use self::client::SubscriptionsClient; | ||
use rocket::State; | ||
use rocket::tokio::sync::Mutex; | ||
|
||
pub mod client; | ||
|
||
#[get("/subscriptions/<device_id>")] | ||
pub async fn get_subscriptions( | ||
device_id: &str, | ||
client: &State<Mutex<SubscriptionsClient>>, | ||
) -> Json<Vec<Subscription>> { | ||
let subscriptions = client.lock().await.get_subscriptions(device_id).unwrap(); | ||
Json(subscriptions) | ||
} | ||
|
||
#[delete("/subscriptions/<device_id>", format = "json", data = "<subscriptions>")] | ||
pub async fn delete_subscriptions( | ||
subscriptions: Json<Vec<Subscription>>, | ||
device_id: &str, | ||
client: &State<Mutex<SubscriptionsClient>>, | ||
) -> Json<usize> { | ||
let result = client.lock().await.delete_subscriptions(device_id, subscriptions.0).unwrap(); | ||
Json(result) | ||
} | ||
|
||
|
||
#[post("/subscriptions/<device_id>", format = "json", data = "<subscriptions>")] | ||
pub async fn add_subscriptions( | ||
subscriptions: Json<Vec<Subscription>>, | ||
#[allow(unused)] | ||
device_id: &str, | ||
client: &State<Mutex<SubscriptionsClient>>, | ||
) -> Json<usize> { | ||
let subscriptions = client.lock().await.add_subscriptions(device_id, subscriptions.0).unwrap(); | ||
Json(subscriptions) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
use typeshare::typeshare; | ||
use serde::{Serialize, Deserialize}; | ||
|
||
use crate::chain::Chain; | ||
|
||
#[derive(Clone, Debug, Serialize, Deserialize)] | ||
#[typeshare(swift = "Equatable, Codable, Hashable")] | ||
pub struct Subscription { | ||
pub chain: Chain, | ||
pub address: String, | ||
} |
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
Oops, something went wrong.