-
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
ff4f9d1
commit 5253048
Showing
10 changed files
with
179 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
extern crate rocket; | ||
use std::error::Error; | ||
|
||
use storage::database::DatabaseClient; | ||
|
||
pub struct DevicesClient { | ||
database: DatabaseClient, | ||
} | ||
|
||
impl DevicesClient { | ||
pub async fn new( | ||
database_url: &str | ||
) -> Self { | ||
let database = DatabaseClient::new(database_url); | ||
Self { | ||
database, | ||
} | ||
} | ||
|
||
pub fn add_device(&mut self, device: primitives::device::Device) -> Result<primitives::device::Device, Box<dyn Error>> { | ||
let device_id = device.id.clone(); | ||
let add_device = self.map_device(device); | ||
let _ = self.database.add_device(add_device)?; | ||
return self.get_device(device_id.as_str()); | ||
} | ||
|
||
pub fn get_device(&mut self, device_id: &str) -> Result<primitives::device::Device, Box<dyn Error>> { | ||
let device = self.database.get_device(device_id.to_string())?; | ||
Ok( | ||
primitives::device::Device { | ||
id: device.device_id, | ||
platform: device.platform, | ||
token: device.token, | ||
is_push_enabled: device.is_push_enabled, | ||
} | ||
) | ||
} | ||
pub fn update_device(&mut self, device: primitives::device::Device) -> Result<primitives::device::Device, Box<dyn Error>> { | ||
let device_id = device.id.clone(); | ||
let update_device = self.map_device(device); | ||
let _ = self.database.update_device(update_device)?; | ||
return self.get_device(device_id.as_str()); | ||
} | ||
|
||
pub fn map_device(&self, device: primitives::device::Device) -> storage::models::Device { | ||
return storage::models::Device { | ||
device_id: device.id, | ||
platform: device.platform, | ||
token: device.token, | ||
is_push_enabled: device.is_push_enabled, | ||
}; | ||
} | ||
} |
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 @@ | ||
extern crate rocket; | ||
use primitives::device::Device; | ||
use rocket::serde::json::Json; | ||
use self::client::DevicesClient; | ||
use rocket::State; | ||
use rocket::tokio::sync::Mutex; | ||
|
||
pub mod client; | ||
|
||
#[post("/devices", format = "json", data = "<device>")] | ||
pub async fn add_device( | ||
device: Json<Device>, | ||
client: &State<Mutex<DevicesClient>>, | ||
) -> Json<Device> { | ||
let device = client.lock().await.add_device(device.0).unwrap(); | ||
Json(device) | ||
} | ||
|
||
#[get("/devices/<device_id>")] | ||
pub async fn get_device( | ||
device_id: &str, | ||
client: &State<Mutex<DevicesClient>>, | ||
) -> Json<Device> { | ||
let device = client.lock().await.get_device(device_id).unwrap(); | ||
Json(device) | ||
} | ||
|
||
#[put("/devices/<device_id>", format = "json", data = "<device>")] | ||
pub async fn update_device( | ||
device: Json<Device>, | ||
#[allow(unused)] | ||
device_id: &str, | ||
client: &State<Mutex<DevicesClient>>, | ||
) -> Json<Device> { | ||
let device = client.lock().await.update_device(device.0).unwrap(); | ||
Json(device) | ||
} |
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 @@ | ||
use typeshare::typeshare; | ||
use serde::{Serialize, Deserialize}; | ||
|
||
#[derive(Debug, Clone, Serialize, Deserialize)] | ||
#[typeshare(swift="Codable")] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct Device { | ||
pub id: String, | ||
pub platform: i32, | ||
pub token: String, | ||
pub is_push_enabled: 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
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 @@ | ||
drop table devices; |
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,14 @@ | ||
CREATE TABLE devices ( | ||
id SERIAL PRIMARY KEY, | ||
device_id VARCHAR NOT NULL, | ||
is_push_enabled boolean NOT NULL, | ||
platform INTEGER NOT NULL, | ||
token VARCHAR NOT NULL, | ||
updated_at timestamp NOT NULL default current_timestamp, | ||
created_at timestamp NOT NULL default current_timestamp, | ||
UNIQUE(device_id) | ||
); | ||
|
||
CREATE INDEX devices_token_idx ON devices (token); | ||
|
||
SELECT diesel_manage_updated_at('devices'); |
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