Skip to content

Commit

Permalink
Add active_no_publication_date_error to crud.rs, make Work Status fie…
Browse files Browse the repository at this point in the history
…ld required in View when work is Active
  • Loading branch information
brendan-oconnell committed May 29, 2024
1 parent 71a2452 commit a04a0b9
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions thoth-api/src/model/work/crud.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,7 @@ where
Self: WorkProperties,
{
fn validate(&self) -> ThothResult<()> {
self.active_no_publication_date_error()?;
self.withdrawn_date_error()?;
self.no_withdrawn_date_error()?;
self.withdrawn_date_before_publication_date_error()
Expand Down
18 changes: 18 additions & 0 deletions thoth-api/src/model/work/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,9 @@ impl WorkStatus {
fn is_withdrawn(&self) -> bool {
matches!(self, WorkStatus::WithdrawnFromSale)
}
fn is_active(&self) -> bool {
matches!(self, WorkStatus::Active)
}
}

pub trait WorkProperties {
Expand All @@ -337,10 +340,25 @@ pub trait WorkProperties {
self.work_status().is_withdrawn()
}

fn is_active(&self) -> bool {
self.work_status().is_active()
}

fn has_withdrawn_date(&self) -> bool {
self.withdrawn_date().is_some()
}

fn has_publication_date(&self) -> bool {
self.publication_date().is_some()
}

fn active_no_publication_date_error(&self) -> ThothResult<()> {
if self.is_active() && self.has_publication_date() {
return Err(ThothError::PublicationDateError);
}
Ok(())
}

fn withdrawn_date_error(&self) -> ThothResult<()> {
if !self.is_withdrawn() && self.has_withdrawn_date() {
return Err(ThothError::WithdrawnDateError);
Expand Down
3 changes: 3 additions & 0 deletions thoth-app/src/component/new_work.rs
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,8 @@ impl Component for NewWorkComponent {
let is_chapter = self.work.work_type == WorkType::BookChapter;
let is_not_withdrawn = self.work.work_status
!= WorkStatus::WithdrawnFromSale;
let is_active = self.work.work_status
== WorkStatus::Active;
html! {
<>
<nav class="level">
Expand Down Expand Up @@ -525,6 +527,7 @@ impl Component for NewWorkComponent {
label = "Publication Date"
value={ self.work.publication_date.clone() }
oninput={ ctx.link().callback(|e: InputEvent| Msg::ChangeDate(e.to_value())) }
required={ is_active }
/>
<FormDateInput
label = "Withdrawn Date"
Expand Down
3 changes: 3 additions & 0 deletions thoth-app/src/component/work.rs
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,8 @@ impl Component for WorkComponent {
let is_chapter = self.work.work_type == WorkType::BookChapter;
let is_not_withdrawn = self.work.work_status
!= WorkStatus::WithdrawnFromSale;
let is_active = self.work.work_status
== WorkStatus::Active;
html! {
<>
<nav class="level">
Expand Down Expand Up @@ -653,6 +655,7 @@ impl Component for WorkComponent {
label = "Publication Date"
value={ self.work.publication_date.clone() }
oninput={ ctx.link().callback(|e: InputEvent| Msg::ChangeDate(e.to_value())) }
required = { is_active }
/>
<FormDateInput
label = "Withdrawn Date"
Expand Down
2 changes: 2 additions & 0 deletions thoth-errors/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ pub enum ThothError {
"Price values must be greater than zero. To indicate an unpriced Publication, omit all Prices."
)]
PriceZeroError,
#[error("Publication Date is required for an Active Work.")]
PublicationDateError,
#[error("{0}")]
RequestError(String),
#[error("{0}")]
Expand Down

0 comments on commit a04a0b9

Please sign in to comment.