try to simplify code with higher order functions

pull/442/head
Felix 2020-01-15 16:48:21 +01:00
parent c41082f98f
commit 7a97c981a0
1 changed files with 9 additions and 11 deletions

View File

@ -16,15 +16,15 @@ pub fn config(cfg: &mut web::ServiceConfig) {
// TODO: need to repeat this for every endpoint // TODO: need to repeat this for every endpoint
.route( .route(
"/api/v1/list_communities", "/api/v1/list_communities",
web::get().to(|info, db| { web::get().to(
route::<ListCommunities, ListCommunitiesResponse>(UserOperation::ListCommunities, info, db) route::<ListCommunities, ListCommunitiesResponse>(UserOperation::ListCommunities)
}), ),
) )
.route( .route(
"/api/v1/get_community", "/api/v1/get_community",
web::get().to(|info, db| { web::get().to(route::<GetCommunity, GetCommunityResponse>(
route::<GetCommunity, GetCommunityResponse>(UserOperation::GetCommunity, info, db) UserOperation::GetCommunity,
}), )),
); );
} }
@ -46,11 +46,9 @@ where
Ok(HttpResponse::Ok().json(response?)) Ok(HttpResponse::Ok().json(response?))
} }
async fn route<Data, Response>( fn route<Data, Response>(
op: UserOperation, op: UserOperation,
info: web::Query<Data>, ) -> Box<(dyn Fn(web::Query<Data>, DbParam) -> Result<HttpResponse, Error> + 'static)>
db: DbParam,
) -> Result<HttpResponse, Error>
where where
Data: Serialize, Data: Serialize,
Response: Serialize, Response: Serialize,
@ -58,5 +56,5 @@ where
{ {
// TODO: want an implementation like this, where useroperation is passed without explicitly passing the other params // TODO: want an implementation like this, where useroperation is passed without explicitly passing the other params
// maybe with a higher order functions? (but that would probably have worse performance) // maybe with a higher order functions? (but that would probably have worse performance)
perform::<Data, Response>(op, info.0, db) Box::new(|data, db| perform::<Data, Response>(op, data.0, db))
} }