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

Renamed HTMX files #40

Merged
merged 1 commit into from
Apr 20, 2024
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
15 changes: 7 additions & 8 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,20 @@ name: Rust

on:
push:
branches: [ "main" ]
branches: ["main"]
pull_request:
branches: [ "main" ]
branches: ["main"]

env:
CARGO_TERM_COLOR: always

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Build
run: cargo build --verbose
- name: Run tests
run: cargo test --verbose
- uses: actions/checkout@v3
- name: Build
run: cargo build --verbose
- name: Run tests
run: cargo test --verbose
8 changes: 6 additions & 2 deletions api/src/handlers/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ pub async fn root_path() -> impl IntoResponse {
pub async fn get_houses_web(State(database): State<Arc<AppState>>) -> impl IntoResponse {
let list_houses = HouseEntity::find().all(&database.db).await.unwrap();

Records { houses: list_houses }
Records {
houses: list_houses,
}
}

pub async fn create_house_web(
Expand All @@ -31,7 +33,9 @@ pub async fn create_house_web(
..Default::default()
};
let insert_response = new_house.insert(&database.db).await.unwrap();
HouseNewTemplate { house: insert_response };
HouseNewTemplate {
house: insert_response,
};
}

#[derive(Template)]
Expand Down
2 changes: 1 addition & 1 deletion api/src/handlers/room.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use entity::room::Entity as RoomEntity;
use sea_orm::{ActiveModelTrait, ColumnTrait, EntityTrait, QueryFilter, Set};
use uuid::Uuid;

use crate::models::room::{Room, CreateRoom};
use crate::models::room::{CreateRoom, Room};

// Todo - Fix internal server error
pub async fn list_rooms(
Expand Down
22 changes: 12 additions & 10 deletions api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,34 @@
use std::sync::Arc;
use anyhow::Ok;
use axum::serve;
use migration::{Migrator, MigratorTrait};
use sea_orm::{Database, DatabaseConnection};
use std::sync::Arc;
use tokio::net::TcpListener;

pub mod routes;
pub mod handlers;
pub mod models;
pub mod routes;

pub struct AppState {
db: DatabaseConnection,
db: DatabaseConnection,
}

#[tokio::main]
async fn start() -> anyhow::Result<()> {
dotenv::dotenv()?;
let database_uri = dotenvy::var("DATABASE_URL")?;
let db_connection = Database::connect(database_uri)
.await
.expect("Database connection failed");
.await
.expect("Database connection failed");

Migrator::up(&db_connection, None).await?;

// Initialize tracing
tracing_subscriber::fmt::init();

let app = routes::create_routes(Arc::new(AppState { db: db_connection.clone() }));
let app = routes::create_routes(Arc::new(AppState {
db: db_connection.clone(),
}));

let listener = TcpListener::bind(&"0.0.0.0:3000").await.unwrap();
serve(listener, app).await?;
Expand All @@ -35,9 +37,9 @@ async fn start() -> anyhow::Result<()> {
}

pub fn main() {
let result = start();
let result = start();

if let Some(err) = result.err() {
println!("Error: {err}");
}
if let Some(err) = result.err() {
println!("Error: {err}");
}
}
23 changes: 11 additions & 12 deletions api/src/models/house.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,21 @@ pub struct CreateHouse {
}

pub struct DeleteResponse {
pub success: bool,
pub message: String,
pub success: bool,
pub message: String,
}


// validations
impl CreateHouse {
pub fn validate(&self) -> Result<(), String> {
if self.title.trim().is_empty() {
return Err("Name is empty".to_string());
}
if self.body.trim().is_empty() {
return Err("Description is empty".to_string());
}
Ok(())
}
pub fn validate(&self) -> Result<(), String> {
if self.title.trim().is_empty() {
return Err("Name is empty".to_string());
}
if self.body.trim().is_empty() {
return Err("Description is empty".to_string());
}
Ok(())
}
}

// impl NewHouse {
Expand Down
16 changes: 8 additions & 8 deletions api/src/models/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub struct Item {
pub category: String,
pub purchase_date: String,
pub expiry_date: Option<String>,
pub value: f64
pub value: f64,
}

/// New Item.
Expand All @@ -23,7 +23,7 @@ pub struct NewItem {
pub category: String,
pub purchase_date: String,
pub expiry_date: Option<String>,
pub value: f64
pub value: f64,
}

#[derive(Deserialize)]
Expand All @@ -33,10 +33,10 @@ pub struct ItemQuery {

// validations
impl NewItem {
pub fn validate(&self) -> Result<(), String> {
if self.name.trim().is_empty() {
return Err("Name is empty".to_string());
}
Ok(())
}
pub fn validate(&self) -> Result<(), String> {
if self.name.trim().is_empty() {
return Err("Name is empty".to_string());
}
Ok(())
}
}
12 changes: 6 additions & 6 deletions api/src/models/room.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ pub struct CreateRoom {

// validations
impl CreateRoom {
pub fn validate(&self) -> Result<(), String> {
if self.name.trim().is_empty() {
return Err("Name is empty".to_string());
}
Ok(())
}
pub fn validate(&self) -> Result<(), String> {
if self.name.trim().is_empty() {
return Err("Name is empty".to_string());
}
Ok(())
}
}
16 changes: 11 additions & 5 deletions api/templates/base.html
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
<!DOCTYPE html>
<!doctype html>
<html lang="en">
<head>
<script src="https://unpkg.com/[email protected]" integrity="sha384-FhXw7b6AlE/jyjlZH5iHa/tTe9EpJ1Y55RjcgPbjeWMskSxZt1v9qkxLJWNJaGni" crossorigin="anonymous"></script>
<link rel="stylesheet" href="/styles.css"/>
<title>{% block title %}{{ title }} - My Site{% endblock %}</title>
<script
src="https://unpkg.com/[email protected]"
integrity="sha384-FhXw7b6AlE/jyjlZH5iHa/tTe9EpJ1Y55RjcgPbjeWMskSxZt1v9qkxLJWNJaGni"
crossorigin="anonymous"
></script>
<link rel="stylesheet" href="/styles.css" />
<title>{% block title %}{{ title }} - My Site{% endblock %}</title>
{% block head %}{% endblock %}
</head>
<body>
<div id="content">
{% block content %}<p>Placeholder content</p>{% endblock %}
{% block content %}
<p>Placeholder content</p>
{% endblock %}
</div>
</body>
</html>
19 changes: 13 additions & 6 deletions api/templates/house.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
<tr id="shuttle-house-{{ house.id }}">
<td> {{ house.id }} </td>
<td id="shuttle-house-desc-{{house.id}}"> {{ house.title }} </td>
<td id="shuttel-house-body={{house.id}}"> {{ house.body }}</td>
<td>
<button hx-delete="/houses/{{house.id}}" hx-trigger="click" hx-target="#shuttle-todo-{{house.id}}" hx-swap="delete">Delete</button>
</td>
<td>{{ house.id }}</td>
<td id="shuttle-house-desc-{{house.id}}">{{ house.title }}</td>
<td id="shuttel-house-body={{house.id}}">{{ house.body }}</td>
<td>
<button
hx-delete="/houses/{{house.id}}"
hx-trigger="click"
hx-target="#shuttle-todo-{{house.id}}"
hx-swap="delete"
>
Delete
</button>
</td>
</tr>
4 changes: 1 addition & 3 deletions api/templates/houses.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
</tr>
</thead>
<tbody id="houses-content">
{% for house in houses %}
{% include "house.html" %}
{% endfor %}
{% for house in houses %} {% include "house.html" %} {% endfor %}
</tbody>
</table>
</div>
28 changes: 19 additions & 9 deletions api/templates/index.html
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
{% extends "base.html" %}

{% block title %}Index{% endblock %}

{% block content %}
{% extends "base.html" %} {% block title %}Index{% endblock %} {% block content
%}
<h1>Inventory App</h1>
<form id="add-form">
<input placeholder="Your House name" required type=text name="title">
<input placeholder="Description" required type=text name="body">
<button hx-post="/houses" hx-trigger="click" hx-target="#houses-content" hx-swap="beforeend">Add</button>
<input placeholder="Your House name" required type="text" name="title" />
<input placeholder="Description" required type="text" name="body" />
<button
hx-post="/houses"
hx-trigger="click"
hx-target="#houses-content"
hx-swap="beforeend"
>
Add
</button>
</form>
<div id="list" hx-get="/houses" hx-target="this" hx-trigger="load" hx-swap="outerHTML">
<div
id="list"
hx-get="/houses"
hx-target="this"
hx-trigger="load"
hx-swap="outerHTML"
>
Loading...
</div>
{% endblock %}
14 changes: 8 additions & 6 deletions api/templates/styles.css
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#content {
display: flex;
flex-direction: column;
align-items: center;
gap: 1rem;
display: flex;
flex-direction: column;
align-items: center;
gap: 1rem;
}

table, th, td {
table,
th,
td {
border: 1px solid black;
padding: 0.25rem;
padding: 0.25rem;
}
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
fn main() {
inventory_api::main();
inventory_api::main();
}
Loading