Add request handler and authentication
Browse files at this commit4c71a7e033b16b4611717b775154e280a2a33b31Parent: 8f25212- feat
- add request handler and authentication
2 changed files +172 -0
crates/server/src/authentication.rsadded+13 -0
| @@ -0,0 +1,13 @@ | |||
| 1 | +use crate::{database_::DB, error::Error, user::User}; | ||
| 2 | + | ||
| 3 | +pub(crate) async fn authenticate( | ||
| 4 | + authentication_token: &str, | ||
| 5 | + database_connection: &DB, | ||
| 6 | +) -> Result<User, Error> { | ||
| 7 | + println!("{}", authentication_token); | ||
| 8 | + | ||
| 9 | + //todo | ||
| 10 | + //Err(Error::Common(common::error::Error::Authenticate)) | ||
| 11 | + let user = User::read(&"0".into(), database_connection).await?; | ||
| 12 | + Ok(user) | ||
| 13 | +} | ||
crates/server/src/request.rsadded+159 -0
| @@ -0,0 +1,159 @@ | |||
| 1 | +use common::{request::Request, response::Response}; | ||
| 2 | + | ||
| 3 | +use crate::{ | ||
| 4 | + authentication::authenticate, | ||
| 5 | + community::Community, | ||
| 6 | + database_::DB, | ||
| 7 | + error::Error, | ||
| 8 | + message::Message, | ||
| 9 | + user::User, | ||
| 10 | + user_community::{JoinCommunity, LeaveCommunity}, | ||
| 11 | +}; | ||
| 12 | + | ||
| 13 | +pub async fn handle_request(request: Request, database_connection: &DB) -> Response { | ||
| 14 | + let request_matcher = async || -> Result<Response, Error> { | ||
| 15 | + match request { | ||
| 16 | + Request::Authentication(authentication) => { | ||
| 17 | + let user = authenticate( | ||
| 18 | + authentication.get_authentication_token(), | ||
| 19 | + database_connection, | ||
| 20 | + ) | ||
| 21 | + .await?; | ||
| 22 | + | ||
| 23 | + Ok(Response::Authentication(user.into())) | ||
| 24 | + } | ||
| 25 | + Request::CreateUser(create_user) => { | ||
| 26 | + let user = User::create(create_user.get_username(), database_connection).await?; | ||
| 27 | + | ||
| 28 | + Ok(Response::CreateUser(user.into())) | ||
| 29 | + } | ||
| 30 | + Request::ReadUser(read_user) => { | ||
| 31 | + let user = User::read(&read_user.get_user_id().into(), database_connection).await?; | ||
| 32 | + | ||
| 33 | + Ok(Response::ReadUser(user.into())) | ||
| 34 | + } | ||
| 35 | + Request::UpdateUser(update_user) => { | ||
| 36 | + let user = | ||
| 37 | + User::read(&update_user.get_user_id().into(), database_connection).await?; | ||
| 38 | + let user = user | ||
| 39 | + .update(update_user.get_username(), database_connection) | ||
| 40 | + .await?; | ||
| 41 | + | ||
| 42 | + Ok(Response::UpdateUser(user.into())) | ||
| 43 | + } | ||
| 44 | + Request::DeleteUser(delete_user) => { | ||
| 45 | + let user = | ||
| 46 | + User::read(&delete_user.get_user_id().into(), database_connection).await?; | ||
| 47 | + let user = user.delete(database_connection).await?; | ||
| 48 | + | ||
| 49 | + Ok(Response::DeleteUser(user.into())) | ||
| 50 | + } | ||
| 51 | + Request::CreateCommunity(create_community) => { | ||
| 52 | + let community = | ||
| 53 | + Community::create(create_community.get_community_name(), database_connection) | ||
| 54 | + .await?; | ||
| 55 | + | ||
| 56 | + Ok(Response::CreateCommunity(community.into())) | ||
| 57 | + } | ||
| 58 | + Request::ReadCommunity(read_community) => { | ||
| 59 | + let community = Community::read( | ||
| 60 | + &read_community.get_community_id().into(), | ||
| 61 | + database_connection, | ||
| 62 | + ) | ||
| 63 | + .await?; | ||
| 64 | + | ||
| 65 | + Ok(Response::ReadCommunity(community.into())) | ||
| 66 | + } | ||
| 67 | + Request::UpdateCommunity(update_community) => { | ||
| 68 | + let community = Community::read( | ||
| 69 | + &update_community.get_community_id().into(), | ||
| 70 | + database_connection, | ||
| 71 | + ) | ||
| 72 | + .await?; | ||
| 73 | + | ||
| 74 | + let community = community | ||
| 75 | + .update(update_community.get_community_name(), database_connection) | ||
| 76 | + .await?; | ||
| 77 | + | ||
| 78 | + Ok(Response::UpdateCommunity(community.into())) | ||
| 79 | + } | ||
| 80 | + Request::DeleteCommunity(delete_community) => { | ||
| 81 | + let community = Community::read( | ||
| 82 | + &delete_community.get_community_id().into(), | ||
| 83 | + database_connection, | ||
| 84 | + ) | ||
| 85 | + .await?; | ||
| 86 | + | ||
| 87 | + let community = community.delete(database_connection).await?; | ||
| 88 | + | ||
| 89 | + Ok(Response::DeleteCommunity(community.into())) | ||
| 90 | + } | ||
| 91 | + Request::CreateMessage(create_message) => { | ||
| 92 | + let user = | ||
| 93 | + User::read(&create_message.get_user_id().into(), database_connection).await?; | ||
| 94 | + let community = Community::read( | ||
| 95 | + &create_message.get_community_id().into(), | ||
| 96 | + database_connection, | ||
| 97 | + ) | ||
| 98 | + .await?; | ||
| 99 | + let message = create_message.get_message_body(); | ||
| 100 | + let message = | ||
| 101 | + Message::create(&user, &community, &message, database_connection).await?; | ||
| 102 | + | ||
| 103 | + Ok(Response::CreateMessage(message.into())) | ||
| 104 | + } | ||
| 105 | + Request::ReadMessage(read_message) => { | ||
| 106 | + let message = | ||
| 107 | + Message::read(&read_message.get_message_id().into(), database_connection) | ||
| 108 | + .await?; | ||
| 109 | + | ||
| 110 | + Ok(Response::ReadMessage(message.into())) | ||
| 111 | + } | ||
| 112 | + Request::UpdateMessage(update_message) => { | ||
| 113 | + let message = | ||
| 114 | + Message::read(&update_message.get_message_id().into(), database_connection) | ||
| 115 | + .await?; | ||
| 116 | + | ||
| 117 | + let message = message | ||
| 118 | + .update(update_message.get_message_body(), database_connection) | ||
| 119 | + .await?; | ||
| 120 | + | ||
| 121 | + Ok(Response::UpdateMessage(message.into())) | ||
| 122 | + } | ||
| 123 | + Request::DeleteMessage(delete_message) => { | ||
| 124 | + let message = | ||
| 125 | + Message::read(&delete_message.get_message_id().into(), database_connection) | ||
| 126 | + .await?; | ||
| 127 | + | ||
| 128 | + let message = message.delete(database_connection).await?; | ||
| 129 | + | ||
| 130 | + Ok(Response::DeleteMessage(message.into())) | ||
| 131 | + } | ||
| 132 | + Request::JoinCommunity(join_community) => { | ||
| 133 | + JoinCommunity::apply( | ||
| 134 | + &join_community.get_user_id().into(), | ||
| 135 | + &join_community.get_community_id().into(), | ||
| 136 | + database_connection, | ||
| 137 | + ) | ||
| 138 | + .await?; | ||
| 139 | + | ||
| 140 | + Ok(Response::JoinCommunity) | ||
| 141 | + } | ||
| 142 | + Request::LeaveCommunity(leave_community) => { | ||
| 143 | + LeaveCommunity::apply( | ||
| 144 | + &leave_community.get_user_id().into(), | ||
| 145 | + &leave_community.get_community_id().into(), | ||
| 146 | + database_connection, | ||
| 147 | + ) | ||
| 148 | + .await?; | ||
| 149 | + | ||
| 150 | + Ok(Response::LeaveCommunity) | ||
| 151 | + } | ||
| 152 | + } | ||
| 153 | + }; | ||
| 154 | + | ||
| 155 | + match request_matcher().await { | ||
| 156 | + Ok(response) => response, | ||
| 157 | + Err(error_value) => Response::Error(error_value.into()), | ||
| 158 | + } | ||
| 159 | +} | ||