Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add date to expense #5

Merged
merged 4 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .cargo/config.toml

This file was deleted.

2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/target
.shuttle-storage
Secrets*.toml
.env
.cargo

# ignore virtualenv
/bin
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ serde = { version = "1.0.193", features = ["derive"] }
shuttle-axum = { version = "0.35.1", default-features = false, features = ["axum-0-7"] }
shuttle-runtime = { version = "0.35.1", default-features = false, features = ["colored"] }
shuttle-shared-db = { version = "0.35.1", features = ["postgres"] }
sqlx = { version = "0.7.3", features = ["runtime-tokio-rustls", "postgres"] }
sqlx = { version = "0.7.3", features = ["runtime-tokio-rustls", "postgres", "chrono"] }
strum = { version = "0.25.0", features = ["strum_macros", "derive"] }
tokio = { version = "1.35.1", features = ["full"] }
tower = "0.4.13"
Expand Down
3 changes: 2 additions & 1 deletion migrations/1_schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ CREATE TABLE IF NOT EXISTS expenses (
description varchar(255) NOT NULL,
price real NOT NULL,
expense_type expense_type NOT NULL,
is_essencial boolean NOT NULL
is_essencial boolean NOT NULL,
date date NOT NULL DEFAULT CURRENT_DATE
);
60 changes: 60 additions & 0 deletions src/constant.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
macro_rules! TABLE_ROW {
() => {
"<tr>
<td>{}</td>
<td>{}</td>
<td>{}</td>
<td>{}</td>
<td>{}</td>
<td>
<button class=\"btn btn-danger\"
hx-get=\"/expenses/{}/edit\"
hx-trigger=\"edit\"
_=\"on click
if .editing is not empty
Swal.fire({{title: 'Already Editing',
showCancelButton: true,
confirmButtonText: 'Yep, Edit This Row!',
text:'Hey! You are already editing a row! Do you want to cancel that edit and continue?'}})
if the result's isConfirmed is false
halt
end
send cancel to .editing
end
trigger edit\">
Edit
</button>
</td>
</tr>"
};
}

macro_rules! EDITABLE_TABLE_ROW {
() => {
"<tr hx-trigger='cancel' class='editing' hx-get=\"/expenses/{}\">
<td><input type='date' name='date' value='{}'></td>
<td><input type='text' name='description' value='{}'></td>
<td><input type='number' name='price' value='{}'></td>
<td><select name='expense_type'>
<option value='Food'>Food</option>
<option value='Transport'>Transport</option>
<option value='Health'>Health</option>
<option value='Education'>Education</option>
<option value='Entertainment'>Entertainment</option>
<option value='Other'>Other</option>
</select></td>
<td><input type='checkbox' name='is_essencial' {}></td>
<td>
<button class=\"btn btn-danger\" hx-get=\"/expenses/{}\">
Cancel
</button>
<button class=\"btn btn-danger\" hx-put=\"/expenses/{}\" hx-include=\"closest tr\">
Save
</button>
</td>
</tr>"
};
}

pub(crate) use EDITABLE_TABLE_ROW;
pub(crate) use TABLE_ROW;
27 changes: 25 additions & 2 deletions src/data.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use std::fmt::Display;
use std::{fmt::Display, str::FromStr};

use chrono::Month;
use serde::Deserialize;
use strum::EnumIter;

#[derive(EnumIter, Debug, PartialEq, Clone)]
#[derive(EnumIter, Debug, PartialEq, Clone, Deserialize)]
pub enum Months {
January,
February,
Expand All @@ -19,6 +20,28 @@ pub enum Months {
December,
}

impl FromStr for Months {
type Err = String;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"January" => Ok(Months::January),
"February" => Ok(Months::February),
"March" => Ok(Months::March),
"April" => Ok(Months::April),
"May" => Ok(Months::May),
"June" => Ok(Months::June),
"July" => Ok(Months::July),
"August" => Ok(Months::August),
"September" => Ok(Months::September),
"October" => Ok(Months::October),
"November" => Ok(Months::November),
"December" => Ok(Months::December),
_ => Err(format!("{} is not a valid month", s)),
}
}
}

impl Display for Months {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Expand Down
Loading