From 2f21071606de35e7d5eb9c44fc868d9e831cabaf Mon Sep 17 00:00:00 2001 From: Felix Date: Thu, 23 Jan 2020 15:22:17 +0100 Subject: [PATCH] api fixes --- server/src/routes/api.rs | 95 +++++++++++++++++++++++----------------- 1 file changed, 55 insertions(+), 40 deletions(-) diff --git a/server/src/routes/api.rs b/server/src/routes/api.rs index 6ba627661..5121d2401 100644 --- a/server/src/routes/api.rs +++ b/server/src/routes/api.rs @@ -16,52 +16,52 @@ type DbParam = web::Data>>; pub fn config(cfg: &mut web::ServiceConfig) { cfg // Site - .route("/api/v1/site", web::get().to(route::)) - .route("/api/v1/categories", web::get().to(route::)) - .route("/api/v1/modlog", web::get().to(route::)) - .route("/api/v1/search", web::post().to(route::)) + .route("/api/v1/site", web::get().to(route_get::)) + .route("/api/v1/categories", web::get().to(route_get::)) + .route("/api/v1/modlog", web::get().to(route_get::)) + .route("/api/v1/search", web::get().to(route_get::)) // Community - .route("/api/v1/community", web::post().to(route::)) - .route("/api/v1/community", web::get().to(route::)) - .route("/api/v1/community", web::put().to(route::)) - .route("/api/v1/community/list", web::get().to(route::)) - .route("/api/v1/community/follow", web::post().to(route::)) + .route("/api/v1/community", web::post().to(route_post::)) + .route("/api/v1/community", web::get().to(route_get::)) + .route("/api/v1/community", web::put().to(route_post::)) + .route("/api/v1/community/list", web::get().to(route_get::)) + .route("/api/v1/community/follow", web::post().to(route_post::)) // Post - .route("/api/v1/post", web::post().to(route::)) - .route("/api/v1/post", web::put().to(route::)) - .route("/api/v1/post", web::get().to(route::)) - .route("/api/v1/post/list", web::get().to(route::)) - .route("/api/v1/post/like", web::post().to(route::)) - .route("/api/v1/post/save", web::post().to(route::)) - .route("/api/v1/post/replies", web::get().to(route::)) + .route("/api/v1/post", web::post().to(route_post::)) + .route("/api/v1/post", web::put().to(route_post::)) + .route("/api/v1/post", web::get().to(route_get::)) + .route("/api/v1/post/list", web::get().to(route_get::)) + .route("/api/v1/post/like", web::post().to(route_post::)) + .route("/api/v1/post/save", web::put().to(route_post::)) // Comment - .route("/api/v1/comment", web::post().to(route::)) - .route("/api/v1/comment", web::put().to(route::)) - .route("/api/v1/comment/like", web::post().to(route::)) - .route("/api/v1/comment/save", web::post().to(route::)) + .route("/api/v1/comment", web::post().to(route_post::)) + .route("/api/v1/comment", web::put().to(route_post::)) + .route("/api/v1/comment/like", web::post().to(route_post::)) + .route("/api/v1/comment/save", web::put().to(route_post::)) // User - .route("/api/v1/user", web::get().to(route::)) - .route("/api/v1/user/mentions", web::get().to(route::)) - .route("/api/v1/user/mentions", web::put().to(route::)) - .route("/api/v1/user/followed-communities", web::get().to(route::)) + .route("/api/v1/user", web::get().to(route_get::)) + .route("/api/v1/user/mention", web::get().to(route_get::)) + .route("/api/v1/user/mention", web::put().to(route_post::)) + .route("/api/v1/user/replies", web::get().to(route_get::)) + .route("/api/v1/user/followed_communities", web::get().to(route_get::)) // Mod actions - .route("/api/v1/community/transfer", web::post().to(route::)) - .route("/api/v1/community/ban-user", web::post().to(route::)) - .route("/api/v1/community/mod", web::post().to(route::)) + .route("/api/v1/community/transfer", web::post().to(route_post::)) + .route("/api/v1/community/ban_user", web::post().to(route_post::)) + .route("/api/v1/community/mod", web::post().to(route_post::)) // Admin actions - .route("/api/v1/site", web::post().to(route::)) - .route("/api/v1/site", web::put().to(route::)) - .route("/api/v1/site/transfer", web::post().to(route::)) - .route("/api/v1/admin/add", web::post().to(route::)) - .route("/api/v1/user/ban", web::post().to(route::)) + .route("/api/v1/site", web::post().to(route_post::)) + .route("/api/v1/site", web::put().to(route_post::)) + .route("/api/v1/site/transfer", web::post().to(route_post::)) + .route("/api/v1/admin/add", web::post().to(route_post::)) + .route("/api/v1/user/ban", web::post().to(route_post::)) // User account actions - .route("/api/v1/user/login", web::post().to(route::)) - .route("/api/v1/user/register", web::post().to(route::)) - .route("/api/v1/user/delete_account", web::post().to(route::)) - .route("/api/v1/user/password_reset", web::post().to(route::)) - .route("/api/v1/user/password_change", web::post().to(route::)) - .route("/api/v1/user/mark_all_as_read", web::post().to(route::)) - .route("/api/v1/user/save_user_settings", web::post().to(route::)); + .route("/api/v1/user/login", web::post().to(route_post::)) + .route("/api/v1/user/register", web::post().to(route_post::)) + .route("/api/v1/user/delete_account", web::post().to(route_post::)) + .route("/api/v1/user/password_reset", web::post().to(route_post::)) + .route("/api/v1/user/password_change", web::post().to(route_post::)) + .route("/api/v1/user/mark_all_as_read", web::post().to(route_post::)) + .route("/api/v1/user/save_user_settings", web::put().to(route_post::)); } fn perform(data: Request, db: DbParam) -> Result @@ -78,7 +78,22 @@ where Ok(HttpResponse::Ok().json(response?)) } -async fn route(data: web::Query, db: DbParam) -> Result +async fn route_get( + data: web::Query, + db: DbParam, +) -> Result +where + Data: Serialize, + Response: Serialize, + Oper: Perform, +{ + perform::(data.0, db) +} + +async fn route_post( + data: web::Json, + db: DbParam, +) -> Result where Data: Serialize, Response: Serialize,