summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2021-03-17 09:15:15 +0100
committerSanto Cariotti <santo@dcariotti.me>2021-03-17 09:15:15 +0100
commitbc9a0de90f0463b59e69e6bb2f2e75f3c29062e5 (patch)
treefae30f2cd8f008321d5abe0c409fe4f0e829df76 /src
parenteda7ce792c9875a437314924f90d66a37d81a1bb (diff)
feat: add commit models and get all of them
Diffstat (limited to 'src')
-rw-r--r--src/commit/mod.rs2
-rw-r--r--src/commit/models.rs42
-rw-r--r--src/commit/routes.rs28
-rw-r--r--src/main.rs2
4 files changed, 74 insertions, 0 deletions
diff --git a/src/commit/mod.rs b/src/commit/mod.rs
new file mode 100644
index 0000000..a0e1883
--- /dev/null
+++ b/src/commit/mod.rs
@@ -0,0 +1,2 @@
+pub mod models;
+pub mod routes;
diff --git a/src/commit/models.rs b/src/commit/models.rs
new file mode 100644
index 0000000..215ab57
--- /dev/null
+++ b/src/commit/models.rs
@@ -0,0 +1,42 @@
+use crate::db::get_client;
+use crate::errors::AppError;
+
+use chrono::NaiveDateTime;
+use deadpool_postgres::Pool;
+use serde::{Deserialize, Serialize};
+use tokio_pg_mapper::FromTokioPostgresRow;
+use tokio_pg_mapper_derive::PostgresMapper;
+
+#[derive(Serialize, Deserialize, PostgresMapper)]
+#[pg_mapper(table = "commit")]
+/// Commit model
+pub struct Commit {
+ pub hash: String,
+ pub tree: String,
+ pub text: String,
+ pub date: NaiveDateTime,
+ pub author_email: String, // Reference to Email
+ pub author_name: String,
+ pub committer_email: String, // Reference to Email
+ pub committer_name: String,
+ pub repository_url: String, // Reference to Repository
+}
+
+impl Commit {
+ /// Find all commits. Order them by descrescent `date` field
+ pub async fn find_all(pool: Pool) -> Result<Vec<Commit>, AppError> {
+ let client = get_client(pool.clone()).await.unwrap();
+ let statement = client
+ .prepare("SELECT * FROM commit ORDER BY date DESC")
+ .await?;
+
+ let commits = client
+ .query(&statement, &[])
+ .await?
+ .iter()
+ .map(|row| Commit::from_row_ref(row).unwrap())
+ .collect::<Vec<Commit>>();
+
+ Ok(commits)
+ }
+}
diff --git a/src/commit/routes.rs b/src/commit/routes.rs
new file mode 100644
index 0000000..bf04108
--- /dev/null
+++ b/src/commit/routes.rs
@@ -0,0 +1,28 @@
+use crate::commit::models::Commit;
+use crate::config::AppState;
+use crate::errors::AppErrorResponse;
+
+use actix_web::{web, HttpResponse, Responder};
+use slog::info;
+
+/// Endpoint used for getting all commits
+async fn index(state: web::Data<AppState>) -> impl Responder {
+ info!(state.log, "GET /commit/");
+ let result = Commit::find_all(state.pool.clone()).await;
+
+ match result {
+ Ok(commits) => HttpResponse::Ok().json(commits),
+ _ => HttpResponse::BadRequest().json(AppErrorResponse {
+ detail: "Error trying to read all commits from database"
+ .to_string(),
+ }),
+ }
+}
+
+/// Routes for commits
+pub fn config(cfg: &mut web::ServiceConfig) {
+ cfg.service(
+ web::scope("/commit")
+ .service(web::resource("{_:/?}").route(web::get().to(index))),
+ );
+}
diff --git a/src/main.rs b/src/main.rs
index 63f48fa..b2e0ba3 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -3,6 +3,7 @@ mod db;
mod errors;
mod helpers;
+mod commit;
mod email;
mod repository;
@@ -37,6 +38,7 @@ async fn main() -> std::io::Result<()> {
.wrap(middleware::Logger::default())
.configure(repository::routes::config)
.configure(email::routes::config)
+ .configure(commit::routes::config)
})
.bind(format!("{}:{}", config.server.host, config.server.port))?
.run()