diff --git a/crates/common/src/event.rs b/crates/common/src/event.rs new file mode 100644 index 0000000..b4388ee --- /dev/null +++ b/crates/common/src/event.rs @@ -0,0 +1,14 @@ +use bitcode::{Decode, Encode}; + +use crate::{community::Community, message::Message, user::User}; + +#[derive(Debug, Clone, Encode, Decode)] +pub enum Event { + MessageCreate(Message), + MessageUpdate(Message), + MessageDelete(Message), + UserJoin(Community, User), + UserLeave(Community, User), + CommunityUpdate(Community), + CommunityDelete(Community), +} diff --git a/crates/common/src/request.rs b/crates/common/src/request.rs new file mode 100644 index 0000000..5f7690d --- /dev/null +++ b/crates/common/src/request.rs @@ -0,0 +1,41 @@ +use bitcode::{Decode, Encode}; + +use crate::request::{ + community::{CreateCommunity, DeleteCommunity, ReadCommunity, UpdateCommunity}, + message::{CreateMessage, DeleteMessage, ReadMessage, UpdateMessage}, + user::{CreateUser, DeleteUser, ReadUser, UpdateUser}, + user_community::{JoinCommunity, LeaveCommunity}, +}; + +pub mod community; +pub mod message; +pub mod user; +pub mod user_community; + +#[derive(Debug, Clone, Encode, Decode)] +pub struct Authentication(String); + +impl Authentication { + pub fn get_authentication_token(&self) -> &str { + &self.0 + } +} + +#[derive(Debug, Clone, Encode, Decode)] +pub enum Request { + Authentication(Authentication), + CreateUser(CreateUser), + ReadUser(ReadUser), + UpdateUser(UpdateUser), + DeleteUser(DeleteUser), + CreateCommunity(CreateCommunity), + ReadCommunity(ReadCommunity), + UpdateCommunity(UpdateCommunity), + DeleteCommunity(DeleteCommunity), + CreateMessage(CreateMessage), + ReadMessage(ReadMessage), + UpdateMessage(UpdateMessage), + DeleteMessage(DeleteMessage), + JoinCommunity(JoinCommunity), + LeaveCommunity(LeaveCommunity), +} diff --git a/crates/common/src/request/community.rs b/crates/common/src/request/community.rs new file mode 100644 index 0000000..f3a47f2 --- /dev/null +++ b/crates/common/src/request/community.rs @@ -0,0 +1,82 @@ +use bitcode::{Decode, Encode}; + +use crate::{error::Error, validate::validate_community_name}; + +#[derive(Debug, Clone, Encode, Decode)] +pub struct CreateCommunity { + community_name: String, +} + +impl CreateCommunity { + pub fn new(community_name: &str) -> Result { + let community_name = community_name.to_string(); + validate_community_name(&community_name)?; + + Ok(Self { community_name }) + } + + pub fn get_community_name(&self) -> &str { + &self.community_name + } +} + +#[derive(Debug, Clone, Encode, Decode)] +pub struct ReadCommunity { + community_id: String, +} + +impl ReadCommunity { + pub fn new(community_id: &str) -> Self { + let community_id = community_id.to_string(); + + Self { community_id } + } + + pub fn get_community_id(&self) -> &str { + &self.community_id + } +} + +#[derive(Debug, Clone, Encode, Decode)] +pub struct UpdateCommunity { + community_id: String, + community_name: String, +} + +impl UpdateCommunity { + pub fn new(community_id: &str, community_name: &str) -> Result { + let community_id = community_id.to_string(); + let community_name = community_name.to_string(); + validate_community_name(&community_name)?; + + Ok(Self { + community_id, + community_name, + }) + } + + pub fn get_community_id(&self) -> &str { + &self.community_id + } + + pub fn get_community_name(&self) -> &str { + &self.community_name + } +} + +#[derive(Debug, Clone, Encode, Decode)] +pub struct DeleteCommunity { + community_id: String, +} + +impl DeleteCommunity { + pub fn new(community_id: &str) -> Self { + let community_id = community_id.to_string(); + + Self { community_id } + } + + pub fn get_community_id(&self) -> &str { + &self.community_id + } +} diff --git a/crates/common/src/request/message.rs b/crates/common/src/request/message.rs new file mode 100644 index 0000000..7a9081b --- /dev/null +++ b/crates/common/src/request/message.rs @@ -0,0 +1,98 @@ +use bitcode::{Decode, Encode}; + +use crate::{error::Error, validate::validate_message_body}; + +#[derive(Debug, Clone, Encode, Decode)] +pub struct CreateMessage { + community_id: String, + user_id: String, + message_body: String, +} + +impl CreateMessage { + pub fn new(community_id: &str, user_id: &str, message_body: &str) -> Result { + let community_id = community_id.to_string(); + let user_id = user_id.to_string(); + let message_body = message_body.to_string(); + validate_message_body(&message_body)?; + + Ok(Self { + community_id, + user_id, + message_body, + }) + } + + pub fn get_community_id(&self) -> &str { + &self.community_id + } + + pub fn get_user_id(&self) -> &str { + &self.user_id + } + + pub fn get_message_body(&self) -> &str { + &self.message_body + } +} + +#[derive(Debug, Clone, Encode, Decode)] +pub struct ReadMessage { + message_id: String, +} + +impl ReadMessage { + pub fn new(message_id: &str) -> Self { + let message_id = message_id.to_string(); + + Self { message_id } + } + + pub fn get_message_id(&self) -> &str { + &self.message_id + } +} + +#[derive(Debug, Clone, Encode, Decode)] +pub struct UpdateMessage { + message_id: String, + message_body: String, +} + +impl UpdateMessage { + pub fn new(message_id: &str, message_body: &str) -> Result { + let message_id = message_id.to_string(); + let message_body = message_body.to_string(); + validate_message_body(&message_body)?; + + Ok(Self { + message_id, + message_body, + }) + } + + pub fn get_message_id(&self) -> &str { + &self.message_id + } + + pub fn get_message_body(&self) -> &str { + &self.message_body + } +} + +#[derive(Debug, Clone, Encode, Decode)] +pub struct DeleteMessage { + message_id: String, +} + +impl DeleteMessage { + pub fn new(message_id: &str) -> Self { + let message_id = message_id.to_string(); + + Self { message_id } + } + + pub fn get_message_id(&self) -> &str { + &self.message_id + } +} diff --git a/crates/common/src/request/user.rs b/crates/common/src/request/user.rs new file mode 100644 index 0000000..44274ed --- /dev/null +++ b/crates/common/src/request/user.rs @@ -0,0 +1,79 @@ +use bitcode::{Decode, Encode}; + +use crate::{error::Error, validate::validate_username}; + +#[derive(Debug, Clone, Encode, Decode)] +pub struct CreateUser { + username: String, +} + +impl CreateUser { + pub fn new(username: &str) -> Result { + let username = username.to_string(); + validate_username(&username)?; + + Ok(Self { username }) + } + + pub fn get_username(&self) -> &str { + &self.username + } +} + +#[derive(Debug, Clone, Encode, Decode)] +pub struct ReadUser { + user_id: String, +} + +impl ReadUser { + pub fn new(user_id: &str) -> Self { + let user_id = user_id.to_string(); + + Self { user_id } + } + + pub fn get_user_id(&self) -> &str { + &self.user_id + } +} + +#[derive(Debug, Clone, Encode, Decode)] +pub struct UpdateUser { + user_id: String, + username: String, +} + +impl UpdateUser { + pub fn new(user_id: &str, username: &str) -> Result { + let user_id = user_id.to_string(); + let username = username.to_string(); + validate_username(&username)?; + + Ok(Self { user_id, username }) + } + + pub fn get_user_id(&self) -> &str { + &self.user_id + } + + pub fn get_username(&self) -> &str { + &self.username + } +} + +#[derive(Debug, Clone, Encode, Decode)] +pub struct DeleteUser { + user_id: String, +} + +impl DeleteUser { + pub fn new(user_id: &str) -> Self { + let user_id = user_id.to_string(); + + Self { user_id } + } + + pub fn get_user_id(&self) -> &str { + &self.user_id + } +} diff --git a/crates/common/src/request/user_community.rs b/crates/common/src/request/user_community.rs new file mode 100644 index 0000000..180cab5 --- /dev/null +++ b/crates/common/src/request/user_community.rs @@ -0,0 +1,53 @@ +use bitcode::{Decode, Encode}; + +#[derive(Debug, Clone, Encode, Decode)] +pub struct JoinCommunity { + user_id: String, + community_id: String, +} + +impl JoinCommunity { + pub fn new(user_id: &str, community_id: &str) -> Self { + let user_id = user_id.to_string(); + let community_id = community_id.to_string(); + + Self { + user_id, + community_id, + } + } + + pub fn get_user_id(&self) -> &str { + &self.user_id + } + + pub fn get_community_id(&self) -> &str { + &self.community_id + } +} + +#[derive(Debug, Clone, Encode, Decode)] +pub struct LeaveCommunity { + user_id: String, + community_id: String, +} + +impl LeaveCommunity { + pub fn new(user_id: &str, community_id: &str) -> Self { + let user_id = user_id.to_string(); + let community_id = community_id.to_string(); + + Self { + user_id, + community_id, + } + } + + pub fn get_user_id(&self) -> &str { + &self.user_id + } + + pub fn get_community_id(&self) -> &str { + &self.community_id + } +} diff --git a/crates/common/src/response.rs b/crates/common/src/response.rs new file mode 100644 index 0000000..4392a2d --- /dev/null +++ b/crates/common/src/response.rs @@ -0,0 +1,27 @@ +use bitcode::{Decode, Encode}; + +use crate::{community::Community, error::Error, message::Message, user::User}; + +pub mod community; +pub mod message; +pub mod user; + +#[derive(Debug, Clone, Encode, Decode)] +pub enum Response { + Authentication(User), + CreateUser(User), + ReadUser(User), + UpdateUser(User), + DeleteUser(User), + CreateCommunity(Community), + ReadCommunity(Community), + UpdateCommunity(Community), + DeleteCommunity(Community), + CreateMessage(Message), + ReadMessage(Message), + UpdateMessage(Message), + DeleteMessage(Message), + JoinCommunity, + LeaveCommunity, + Error(Error), +} diff --git a/crates/common/src/response/community.rs b/crates/common/src/response/community.rs new file mode 100644 index 0000000..e69de29 diff --git a/crates/common/src/response/message.rs b/crates/common/src/response/message.rs new file mode 100644 index 0000000..e69de29 diff --git a/crates/common/src/response/user.rs b/crates/common/src/response/user.rs new file mode 100644 index 0000000..e69de29