From c2430b5df5ea46421ec06f06df41c1ee75bedbe0 Mon Sep 17 00:00:00 2001 From: timdegen <104884878+timdegen@users.noreply.github.com> Date: Sun, 3 Sep 2023 22:53:06 +0100 Subject: [PATCH] Use platform as string --- api/src/device/client.rs | 5 +++-- primitives/src/device.rs | 4 +++- primitives/src/platform.rs | 10 ++++++++++ .../src/migrations/2023-09-03-192926_devices/up.sql | 2 +- storage/src/models.rs | 2 +- storage/src/schema.rs | 2 +- 6 files changed, 19 insertions(+), 6 deletions(-) diff --git a/api/src/device/client.rs b/api/src/device/client.rs index 959afcb2..f1e7ea77 100644 --- a/api/src/device/client.rs +++ b/api/src/device/client.rs @@ -1,6 +1,7 @@ extern crate rocket; use std::error::Error; +use primitives::platform::Platform; use storage::database::DatabaseClient; pub struct DevicesClient { @@ -29,7 +30,7 @@ impl DevicesClient { Ok( primitives::device::Device { id: device.device_id, - platform: device.platform, + platform: Platform::from_str(device.platform.as_str()).unwrap(), token: device.token, is_push_enabled: device.is_push_enabled, } @@ -45,7 +46,7 @@ impl DevicesClient { pub fn map_device(&self, device: primitives::device::Device) -> storage::models::Device { return storage::models::Device { device_id: device.id, - platform: device.platform, + platform: device.platform.as_str().to_string(), token: device.token, is_push_enabled: device.is_push_enabled, }; diff --git a/primitives/src/device.rs b/primitives/src/device.rs index b46ca772..4270cd9d 100644 --- a/primitives/src/device.rs +++ b/primitives/src/device.rs @@ -1,12 +1,14 @@ use typeshare::typeshare; use serde::{Serialize, Deserialize}; +use crate::platform::Platform; + #[derive(Debug, Clone, Serialize, Deserialize)] #[typeshare(swift="Codable")] #[serde(rename_all = "camelCase")] pub struct Device { pub id: String, - pub platform: i32, + pub platform: Platform, pub token: String, pub is_push_enabled: bool, } \ No newline at end of file diff --git a/primitives/src/platform.rs b/primitives/src/platform.rs index ed04affa..33cd35a0 100644 --- a/primitives/src/platform.rs +++ b/primitives/src/platform.rs @@ -1,6 +1,8 @@ +use typeshare::typeshare; use serde::{Serialize, Deserialize}; #[derive(Debug, Clone, Serialize, Deserialize)] +#[typeshare(swift="Codable")] #[serde(rename_all = "lowercase")] pub enum Platform { IOS, @@ -14,4 +16,12 @@ impl Platform { Platform::Android => "android", } } + + pub fn from_str(s: &str) -> Option { + match s { + "ios" => Some(Platform::IOS), + "android" => Some(Platform::Android), + _ => None, + } + } } \ No newline at end of file diff --git a/storage/src/migrations/2023-09-03-192926_devices/up.sql b/storage/src/migrations/2023-09-03-192926_devices/up.sql index 0d9d6848..90cac18a 100644 --- a/storage/src/migrations/2023-09-03-192926_devices/up.sql +++ b/storage/src/migrations/2023-09-03-192926_devices/up.sql @@ -2,7 +2,7 @@ CREATE TABLE devices ( id SERIAL PRIMARY KEY, device_id VARCHAR NOT NULL, is_push_enabled boolean NOT NULL, - platform INTEGER NOT NULL, + platform VARCHAR NOT NULL, token VARCHAR NOT NULL, updated_at timestamp NOT NULL default current_timestamp, created_at timestamp NOT NULL default current_timestamp, diff --git a/storage/src/models.rs b/storage/src/models.rs index 8d958539..28acd4f3 100644 --- a/storage/src/models.rs +++ b/storage/src/models.rs @@ -152,7 +152,7 @@ impl Price { #[diesel(check_for_backend(diesel::pg::Pg))] pub struct Device { pub device_id: String, - pub platform: i32, + pub platform: String, pub token: String, pub is_push_enabled: bool, } \ No newline at end of file diff --git a/storage/src/schema.rs b/storage/src/schema.rs index 3faf44e5..8b826db9 100644 --- a/storage/src/schema.rs +++ b/storage/src/schema.rs @@ -16,7 +16,7 @@ diesel::table! { id -> Int4, device_id -> Varchar, is_push_enabled -> Bool, - platform -> Int4, + platform -> Varchar, token -> Varchar, updated_at -> Timestamp, created_at -> Timestamp,