diff --git a/src/cli/command.rs b/src/cli/command.rs index 234cb4a..e19a1d8 100644 --- a/src/cli/command.rs +++ b/src/cli/command.rs @@ -38,6 +38,8 @@ pub enum Commands { page_id: usize }, Job { - job_id: usize + job_id: usize, + #[arg(short, long)] + log: bool, } } diff --git a/src/entity/job.rs b/src/entity/job.rs index 2c991d3..3742e91 100644 --- a/src/entity/job.rs +++ b/src/entity/job.rs @@ -1,5 +1,13 @@ +use time::PrimitiveDateTime; + #[derive(Debug, PartialEq, Eq)] pub struct Job { pub file_name: String, pub description: String, } + +#[derive(Debug, PartialEq, Eq)] +pub struct JobLog { + pub content: String, + pub upd_ts: PrimitiveDateTime, +} diff --git a/src/main.rs b/src/main.rs index a3f5b80..5ca8a67 100644 --- a/src/main.rs +++ b/src/main.rs @@ -48,9 +48,13 @@ fn main() -> std::result::Result<(), Box> { let file_service: FileService = container::get_page_service(&mut conn); file_service.get_page(&page_id); } - Commands::Job { job_id } => { + Commands::Job { job_id, log } => { let mut job_service: JobService = container::get_job_service(&mut conn); - job_service.get_job_by_id(&job_id); + if log { + job_service.get_job_log(&job_id); + } else { + job_service.get_job_by_id(&job_id); + } } } diff --git a/src/repository/job_repo.rs b/src/repository/job_repo.rs index b4b458d..410e04c 100644 --- a/src/repository/job_repo.rs +++ b/src/repository/job_repo.rs @@ -1,6 +1,6 @@ use mysql::{params, prelude::Queryable, PooledConn}; -use crate::entity::job::Job; +use crate::entity::job::{Job, JobLog}; #[derive(Debug)] pub struct JobRepo<'a> { @@ -35,4 +35,26 @@ impl<'a> JobRepo<'a> { }, ) } + + pub fn find_log(&mut self, job_id: &usize) -> Result, mysql::Error> { + let stat = self + .db_pool + .prep( + " + SELECT jea.content, jea.upd_ts + FROM jobs j + JOIN job_events je on j.job_id = je.job_id AND je.m_id = j.m_id + JOIN job_event_attachments jea ON je.job_event_id = jea.job_event_id + WHERE + j.job_id = :job_id + ", + ) + .unwrap(); + + self.db_pool.exec_map( + stat, + params! {"job_id" => job_id}, + |( content, upd_ts )| JobLog { content, upd_ts }, + ) + } } diff --git a/src/service/job_service.rs b/src/service/job_service.rs index 16ca4b7..dff4996 100644 --- a/src/service/job_service.rs +++ b/src/service/job_service.rs @@ -1,4 +1,7 @@ -use crate::{repository::job_repo::JobRepo, entity::job::Job}; +use crate::{ + entity::job::{Job, JobLog}, + repository::job_repo::JobRepo, +}; pub struct JobService<'a> { pub repo: JobRepo<'a>, @@ -25,7 +28,26 @@ impl<'a> JobService<'a> { println!("{}", "-".repeat(150)); } - }, + } + Err(err) => panic!("{}", err), + } + } + + pub fn get_job_log(&mut self, job_id: &usize) { + let result = self.repo.find_log(job_id); + + match result { + Ok(joblog) => { + if joblog.is_empty() { + println!("Job log not found!"); + } else { + joblog.into_iter().for_each(|joblog: JobLog| { + println!("{0: <25} | {1: <20} ", joblog.upd_ts, joblog.content,); + }); + + println!("{}", "-".repeat(150)); + } + } Err(err) => panic!("{}", err), } }