summaryrefslogtreecommitdiff
path: root/src/api
diff options
context:
space:
mode:
Diffstat (limited to 'src/api')
-rw-r--r--src/api/admin.rs27
-rw-r--r--src/api/auth.rs31
-rw-r--r--src/api/mod.rs27
-rw-r--r--src/api/posts.rs27
-rw-r--r--src/api/users.rs26
5 files changed, 95 insertions, 43 deletions
diff --git a/src/api/admin.rs b/src/api/admin.rs
index 6030315..f412d75 100644
--- a/src/api/admin.rs
+++ b/src/api/admin.rs
@@ -5,13 +5,12 @@ use serde::Deserialize;
use tower_cookies::{Cookie, Cookies};
use crate::{
- database,
public::{
admin,
docs::{EndpointDocumentation, EndpointMethod},
},
types::{
- extract::{AdminUser, Check, CheckResult, Json},
+ extract::{AdminUser, Check, CheckResult, Database, Json},
http::ResponseCode,
},
};
@@ -92,8 +91,8 @@ impl Check for QueryRequest {
}
}
-async fn query(_: AdminUser, Json(body): Json<QueryRequest>) -> Response {
- match database::query(body.query) {
+async fn query(_: AdminUser, Database(db): Database, Json(body): Json<QueryRequest>) -> Response {
+ match db.query(body.query) {
Ok(changes) => ResponseCode::Success.text(&format!(
"Query executed successfully. {changes} lines changed."
)),
@@ -114,8 +113,8 @@ pub const ADMIN_POSTS: EndpointDocumentation = EndpointDocumentation {
cookie: Some("admin"),
};
-async fn posts(_: AdminUser) -> Response {
- admin::generate_posts()
+async fn posts(_: AdminUser, Database(db): Database) -> Response {
+ admin::generate_posts(&db)
}
pub const ADMIN_USERS: EndpointDocumentation = EndpointDocumentation {
@@ -131,8 +130,8 @@ pub const ADMIN_USERS: EndpointDocumentation = EndpointDocumentation {
cookie: Some("admin"),
};
-async fn users(_: AdminUser) -> Response {
- admin::generate_users()
+async fn users(_: AdminUser, Database(db): Database) -> Response {
+ admin::generate_users(&db)
}
pub const ADMIN_SESSIONS: EndpointDocumentation = EndpointDocumentation {
@@ -148,8 +147,8 @@ pub const ADMIN_SESSIONS: EndpointDocumentation = EndpointDocumentation {
cookie: Some("admin"),
};
-async fn sessions(_: AdminUser) -> Response {
- admin::generate_sessions()
+async fn sessions(_: AdminUser, Database(db): Database) -> Response {
+ admin::generate_sessions(&db)
}
pub const ADMIN_COMMENTS: EndpointDocumentation = EndpointDocumentation {
@@ -165,8 +164,8 @@ pub const ADMIN_COMMENTS: EndpointDocumentation = EndpointDocumentation {
cookie: Some("admin"),
};
-async fn comments(_: AdminUser) -> Response {
- admin::generate_comments()
+async fn comments(_: AdminUser, Database(db): Database) -> Response {
+ admin::generate_comments(&db)
}
pub const ADMIN_LIKES: EndpointDocumentation = EndpointDocumentation {
@@ -182,8 +181,8 @@ pub const ADMIN_LIKES: EndpointDocumentation = EndpointDocumentation {
cookie: Some("admin"),
};
-async fn likes(_: AdminUser) -> Response {
- admin::generate_likes()
+async fn likes(_: AdminUser, Database(db): Database) -> Response {
+ admin::generate_likes(&db)
}
async fn check(check: Option<AdminUser>) -> Response {
diff --git a/src/api/auth.rs b/src/api/auth.rs
index 60ddc80..c48b583 100644
--- a/src/api/auth.rs
+++ b/src/api/auth.rs
@@ -6,7 +6,7 @@ use tower_cookies::{Cookie, Cookies};
use crate::{
public::docs::{EndpointDocumentation, EndpointMethod},
types::{
- extract::{AuthorizedUser, Check, CheckResult, Json, Log},
+ extract::{AuthorizedUser, Check, CheckResult, Database, Json, Log},
http::ResponseCode,
session::Session,
user::User,
@@ -99,13 +99,17 @@ impl Check for RegistrationRequet {
}
}
-async fn register(cookies: Cookies, Json(body): Json<RegistrationRequet>) -> Response {
- let user = match User::new(body) {
+async fn register(
+ cookies: Cookies,
+ Database(db): Database,
+ Json(body): Json<RegistrationRequet>,
+) -> Response {
+ let user = match User::new(&db, body) {
Ok(user) => user,
Err(err) => return err,
};
- let session = match Session::new(user.user_id) {
+ let session = match Session::new(&db, user.user_id) {
Ok(session) => session,
Err(err) => return err,
};
@@ -158,8 +162,12 @@ impl Check for LoginRequest {
}
}
-async fn login(cookies: Cookies, Json(body): Json<LoginRequest>) -> Response {
- let Ok(user) = User::from_email(&body.email) else {
+async fn login(
+ cookies: Cookies,
+ Database(db): Database,
+ Json(body): Json<LoginRequest>,
+) -> Response {
+ let Ok(user) = User::from_email(&db, &body.email) else {
return ResponseCode::BadRequest.text("Email is not registered")
};
@@ -167,7 +175,7 @@ async fn login(cookies: Cookies, Json(body): Json<LoginRequest>) -> Response {
return ResponseCode::BadRequest.text("Password is not correct");
}
- let session = match Session::new(user.user_id) {
+ let session = match Session::new(&db, user.user_id) {
Ok(session) => session,
Err(err) => return err,
};
@@ -199,10 +207,15 @@ pub const AUTH_LOGOUT: EndpointDocumentation = EndpointDocumentation {
cookie: None,
};
-async fn logout(cookies: Cookies, AuthorizedUser(user): AuthorizedUser, _: Log) -> Response {
+async fn logout(
+ cookies: Cookies,
+ AuthorizedUser(user): AuthorizedUser,
+ Database(db): Database,
+ _: Log,
+) -> Response {
cookies.remove(Cookie::new("auth", ""));
- if let Err(err) = Session::delete(user.user_id) {
+ if let Err(err) = Session::delete(&db, user.user_id) {
return err;
}
diff --git a/src/api/mod.rs b/src/api/mod.rs
index cd2190c..eeaaa0a 100644
--- a/src/api/mod.rs
+++ b/src/api/mod.rs
@@ -1,5 +1,15 @@
-use crate::types::extract::RouterURI;
-use axum::{error_handling::HandleErrorLayer, BoxError, Extension, Router};
+use crate::{
+ database,
+ types::extract::{DatabaseExtention, RouterURI},
+};
+use axum::{
+ error_handling::HandleErrorLayer,
+ http::Request,
+ middleware::{self, Next},
+ response::Response,
+ BoxError, Extension, Router,
+};
+use tokio::sync::Mutex;
use tower::ServiceBuilder;
use tower_governor::{
errors::display_error, governor::GovernorConfigBuilder, key_extractor::SmartIpKeyExtractor,
@@ -13,6 +23,18 @@ pub mod users;
pub use auth::RegistrationRequet;
+async fn connect<B>(mut req: Request<B>, next: Next<B>) -> Response
+where
+ B: Send,
+{
+ if let Ok(db) = database::Database::connect() {
+ let ex = DatabaseExtention(Mutex::new(db));
+ req.extensions_mut().insert(ex);
+ }
+
+ next.run(req).await
+}
+
pub fn router() -> Router {
let governor_conf = Box::new(
GovernorConfigBuilder::default()
@@ -49,4 +71,5 @@ pub fn router() -> Router {
config: Box::leak(governor_conf),
}),
)
+ .layer(middleware::from_fn(connect))
}
diff --git a/src/api/posts.rs b/src/api/posts.rs
index 57b2ca8..bd7e665 100644
--- a/src/api/posts.rs
+++ b/src/api/posts.rs
@@ -9,7 +9,7 @@ use crate::{
public::docs::{EndpointDocumentation, EndpointMethod},
types::{
comment::Comment,
- extract::{AuthorizedUser, Check, CheckResult, Json},
+ extract::{AuthorizedUser, Check, CheckResult, Database, Json},
http::ResponseCode,
like::Like,
post::Post,
@@ -55,9 +55,10 @@ impl Check for PostCreateRequest {
async fn create(
AuthorizedUser(user): AuthorizedUser,
+ Database(db): Database,
Json(body): Json<PostCreateRequest>,
) -> Response {
- let Ok(post) = Post::new(user.user_id, body.content) else {
+ let Ok(post) = Post::new(&db, user.user_id, body.content) else {
return ResponseCode::InternalServerError.text("Failed to create post")
};
@@ -101,9 +102,10 @@ impl Check for PostPageRequest {
async fn page(
AuthorizedUser(user): AuthorizedUser,
+ Database(db): Database,
Json(body): Json<PostPageRequest>,
) -> Response {
- let Ok(posts) = Post::from_post_page(user.user_id, body.page) else {
+ let Ok(posts) = Post::from_post_page(&db, user.user_id, body.page) else {
return ResponseCode::InternalServerError.text("Failed to fetch posts")
};
@@ -149,9 +151,10 @@ impl Check for CommentsPageRequest {
async fn comments(
AuthorizedUser(_user): AuthorizedUser,
+ Database(db): Database,
Json(body): Json<CommentsPageRequest>,
) -> Response {
- let Ok(comments) = Comment::from_comment_page(body.page, body.post_id) else {
+ let Ok(comments) = Comment::from_comment_page(&db, body.page, body.post_id) else {
return ResponseCode::InternalServerError.text("Failed to fetch comments")
};
@@ -197,9 +200,10 @@ impl Check for UsersPostsRequest {
async fn user(
AuthorizedUser(user): AuthorizedUser,
+ Database(db): Database,
Json(body): Json<UsersPostsRequest>,
) -> Response {
- let Ok(posts) = Post::from_user_post_page(user.user_id, body.user_id, body.page) else {
+ let Ok(posts) = Post::from_user_post_page(&db, user.user_id, body.user_id, body.page) else {
return ResponseCode::InternalServerError.text("Failed to fetch posts")
};
@@ -251,9 +255,10 @@ impl Check for PostCommentRequest {
async fn comment(
AuthorizedUser(user): AuthorizedUser,
+ Database(db): Database,
Json(body): Json<PostCommentRequest>,
) -> Response {
- if let Err(err) = Comment::new(user.user_id, body.post_id, &body.content) {
+ if let Err(err) = Comment::new(&db, user.user_id, body.post_id, &body.content) {
return err;
}
@@ -293,12 +298,16 @@ impl Check for PostLikeRequest {
}
}
-async fn like(AuthorizedUser(user): AuthorizedUser, Json(body): Json<PostLikeRequest>) -> Response {
+async fn like(
+ AuthorizedUser(user): AuthorizedUser,
+ Database(db): Database,
+ Json(body): Json<PostLikeRequest>,
+) -> Response {
if body.state {
- if let Err(err) = Like::add_liked(user.user_id, body.post_id) {
+ if let Err(err) = Like::add_liked(&db, user.user_id, body.post_id) {
return err;
}
- } else if let Err(err) = Like::remove_liked(user.user_id, body.post_id) {
+ } else if let Err(err) = Like::remove_liked(&db, user.user_id, body.post_id) {
return err;
}
diff --git a/src/api/users.rs b/src/api/users.rs
index 082926e..71305c5 100644
--- a/src/api/users.rs
+++ b/src/api/users.rs
@@ -1,7 +1,7 @@
use crate::{
public::docs::{EndpointDocumentation, EndpointMethod},
types::{
- extract::{AuthorizedUser, Check, CheckResult, Json, Log, Png},
+ extract::{AuthorizedUser, Check, CheckResult, Database, Json, Log, Png},
http::ResponseCode,
user::User,
},
@@ -46,9 +46,10 @@ impl Check for UserLoadRequest {
async fn load_batch(
AuthorizedUser(_user): AuthorizedUser,
+ Database(db): Database,
Json(body): Json<UserLoadRequest>,
) -> Response {
- let users = User::from_user_ids(body.ids);
+ let users = User::from_user_ids(&db, body.ids);
let Ok(json) = serde_json::to_string(&users) else {
return ResponseCode::InternalServerError.text("Failed to fetch users")
};
@@ -90,9 +91,10 @@ impl Check for UserPageReqiest {
async fn load_page(
AuthorizedUser(_user): AuthorizedUser,
+ Database(db): Database,
Json(body): Json<UserPageReqiest>,
) -> Response {
- let Ok(users) = User::from_user_page(body.page) else {
+ let Ok(users) = User::from_user_page(&db, body.page) else {
return ResponseCode::InternalServerError.text("Failed to fetch users")
};
@@ -207,17 +209,18 @@ impl Check for UserFollowRequest {
async fn follow(
AuthorizedUser(user): AuthorizedUser,
+ Database(db): Database,
Json(body): Json<UserFollowRequest>,
) -> Response {
if body.state {
- if let Err(err) = User::add_following(user.user_id, body.user_id) {
+ if let Err(err) = User::add_following(&db, user.user_id, body.user_id) {
return err;
}
- } else if let Err(err) = User::remove_following(user.user_id, body.user_id) {
+ } else if let Err(err) = User::remove_following(&db, user.user_id, body.user_id) {
return err;
}
- match User::get_following(user.user_id, body.user_id) {
+ match User::get_following(&db, user.user_id, body.user_id) {
Ok(status) => ResponseCode::Success.text(&format!("{status}")),
Err(err) => err,
}
@@ -259,9 +262,10 @@ impl Check for UserFollowStatusRequest {
async fn follow_status(
AuthorizedUser(user): AuthorizedUser,
+ Database(db): Database,
Json(body): Json<UserFollowStatusRequest>,
) -> Response {
- match User::get_following(user.user_id, body.user_id) {
+ match User::get_following(&db, user.user_id, body.user_id) {
Ok(status) => ResponseCode::Success.text(&format!("{status}")),
Err(err) => err,
}
@@ -297,8 +301,12 @@ impl Check for UserFriendsRequest {
}
}
-async fn friends(AuthorizedUser(_user): AuthorizedUser, Json(body): Json<UserFriendsRequest>) -> Response {
- let Ok(users) = User::get_friends(body.user_id) else {
+async fn friends(
+ AuthorizedUser(_user): AuthorizedUser,
+ Database(db): Database,
+ Json(body): Json<UserFriendsRequest>,
+) -> Response {
+ let Ok(users) = User::get_friends(&db, body.user_id) else {
return ResponseCode::InternalServerError.text("Failed to fetch user")
};