refactor: Remove unused code

- DbConn moved to /src/api/guards/db.rs
- Removed redundant imports in /src/api/routes

Signed-off-by: supercmmetry <vishaals2000@gmail.com>
This commit is contained in:
supercmmetry committed 2020-06-26 08:14:19 +05:30
1 parent f16e906ff4
commit de741f451e
6 files changed
+35 -173

No files matched your search

+1 -37
View File
@@ -1,12 +1,7 @@
use diesel::pg::PgConnection;
use diesel::{r2d2, Connection};
use diesel::r2d2::{PooledConnection, ConnectionManager};
use rocket::{Outcome, Request, State};
use rocket::http::Status;
use rocket::request::{self, FromRequest};
use diesel::r2d2::ConnectionManager;
use std::env;
use std::ops::Deref;
use crate::utils::errors::{Error, ErrorCode};
pub type Pool = r2d2::Pool<ConnectionManager<PgConnection>>;
@@ -21,35 +16,4 @@ fn database_url() -> String {
pub fn pg_connection() -> PgConnection {
PgConnection::establish(database_url().as_str()).unwrap()
}
pub struct DbConn(pub r2d2::PooledConnection<ConnectionManager<PgConnection>>);
impl<'a, 'r> FromRequest<'a, 'r> for DbConn {
type Error = ();
fn from_request(request: &'a Request<'r>) -> request::Outcome<DbConn, Self::Error> {
let pool = request.guard::<State<Pool>>()?;
match pool.get() {
Ok(conn) => Outcome::Success(DbConn(conn)),
Err(_) => Outcome::Failure((Status::ServiceUnavailable, ())),
}
}
}
impl Deref for DbConn {
type Target = PgConnection;
fn deref(&self) -> &Self::Target {
&self.0
}
}
pub fn get_connection(pool: &Pool) -> Result<PooledConnection<ConnectionManager<PgConnection>>, Error> {
let result = pool.get();
if let Err(e) = result {
return Err(Error::custom(ErrorCode::DatabaseError, e.to_string()));
}
Ok(result.unwrap())
}
-29
View File
@@ -94,33 +94,4 @@ impl<'r> Responder<'r> for Error {
.sized_body(Cursor::new(serde_json::to_string(&self).unwrap()))
.ok()
}
}
#[derive(Serialize)]
pub struct Success {
msg: String
}
impl Success {
pub fn new(msg: &str) -> Success {
Success {
msg: msg.to_string()
}
}
}
impl<'r> Responder<'r> for Success {
fn respond_to(self, request: &Request) -> response::Result<'r> {
let logger = request.guard::<State<Logger>>();
if let Outcome::Success(logger) = logger {
info!(logger, "{}", serde_json::to_string(&self).unwrap());
}
Response::build()
.status(Status::Ok)
.header(ContentType::JSON)
.sized_body(Cursor::new(serde_json::to_string(&self).unwrap()))
.ok()
}
}