Skip to content

Commit

Permalink
Repo cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
lucemans committed Dec 1, 2024
1 parent 92dfa53 commit 49dbcb0
Show file tree
Hide file tree
Showing 17 changed files with 220 additions and 157 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,25 @@ An example of a tracked item could be:
"modified": "2023-06-01T00:00:00Z"
}
```

## Developing Locally

This project consists of two parts:

- `engine`: A Rust application that runs the core logic and API.
- `web`: A Vite React (TS) application that runs the web interface.

To get started locally you need to have `docker` and `docker-compose` installed.

```
# Start Engine
cd engine
docker compose up -d # start the database
cargo sqlx migrate run # only required for development
cargo run # start the engine
# Start Web
cd web
pnpm install
pnpm dev
```
31 changes: 31 additions & 0 deletions engine/src/models/field/definition.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use crate::database::Database;
use crate::models::field::kind::FieldKind;
use serde::{Deserialize, Serialize};
use poem_openapi::Object;
use sqlx::FromRow;

#[derive(FromRow, Object, Debug, Clone, Serialize, Deserialize)]
pub struct FieldDefinition {
pub definition_id: String,
pub kind: FieldKind,
pub name: String,
pub created_at: Option<chrono::DateTime<chrono::Utc>>,
pub updated_at: Option<chrono::DateTime<chrono::Utc>>,
}

impl FieldDefinition {
pub async fn create(
kind: FieldKind,
name: String,
database: &Database,
) -> Result<FieldDefinition, sqlx::Error> {
sqlx::query_as!(
FieldDefinition,
"INSERT INTO field_definitions (kind, name) VALUES ($1, $2) RETURNING *",
kind.to_string(),
name
)
.fetch_one(&database.pool)
.await
}
}
43 changes: 43 additions & 0 deletions engine/src/models/field/kind.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use serde::{Deserialize, Serialize};
use sqlx::Type;
use poem_openapi::Enum;

#[derive(Type, Enum, Debug, Clone, Serialize, Deserialize)]
#[sqlx(type_name = "text")]
#[sqlx(rename_all = "lowercase")]
pub enum FieldKind {
String,
Number,
Boolean,
Json,
}

impl std::fmt::Display for FieldKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self)
}
}

impl FieldKind {
pub fn from_str(s: &str) -> Self {
match s.to_lowercase().as_str() {
"string" => FieldKind::String,
"number" => FieldKind::Number,
"boolean" => FieldKind::Boolean,
"json" => FieldKind::Json,
_ => FieldKind::String,
}
}
}

impl From<String> for FieldKind {
fn from(s: String) -> Self {
FieldKind::from_str(&s)
}
}

impl From<FieldKind> for String {
fn from(kind: FieldKind) -> Self {
kind.to_string().to_lowercase()
}
}
7 changes: 7 additions & 0 deletions engine/src/models/field/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use serde::{Deserialize, Serialize};
use sqlx::prelude::*;

use crate::database::Database;

pub mod kind;
pub mod definition;
64 changes: 0 additions & 64 deletions engine/src/models/field_definitions.rs

This file was deleted.

31 changes: 31 additions & 0 deletions engine/src/models/item/field.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

use crate::database::Database;
use serde::{Deserialize, Serialize};

#[derive(sqlx::FromRow, poem_openapi::Object, Debug, Clone, Serialize, Deserialize)]
pub struct ItemField {
pub item_id: String,
pub definition_id: String,
pub value: serde_json::Value,
pub created_at: Option<chrono::DateTime<chrono::Utc>>,
pub updated_at: Option<chrono::DateTime<chrono::Utc>>,
}

impl ItemField {
pub async fn create(
item_id: String,
definition_id: String,
value: serde_json::Value,
database: &Database,
) -> Result<ItemField, sqlx::Error> {
sqlx::query_as!(
ItemField,
"INSERT INTO item_fields (item_id, definition_id, value) VALUES ($1, $2, $3) RETURNING *",
item_id,
definition_id,
value
)
.fetch_one(&database.pool)
.await
}
}
25 changes: 25 additions & 0 deletions engine/src/models/item/media.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use crate::database::Database;
use serde::{Deserialize, Serialize};

#[derive(sqlx::FromRow, poem_openapi::Object, Debug, Clone, Serialize, Deserialize)]
pub struct ItemMedia {
pub item_id: String,
pub media_id: i32,
}

impl ItemMedia {
pub async fn create(
item_id: String,
media_id: i32,
database: &Database,
) -> Result<ItemMedia, sqlx::Error> {
sqlx::query_as!(
ItemMedia,
"INSERT INTO item_media (item_id, media_id) VALUES ($1, $2) RETURNING *",
item_id,
media_id
)
.fetch_one(&database.pool)
.await
}
}
68 changes: 17 additions & 51 deletions engine/src/models/items.rs → engine/src/models/item/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ use tracing::info;

use crate::database::Database;

pub mod field;
pub mod media;

#[derive(sqlx::FromRow, poem_openapi::Object, Debug, Clone, Serialize, Deserialize)]
pub struct Item {
pub item_id: String,
Expand All @@ -15,6 +18,20 @@ pub struct Item {
pub updated_at: Option<chrono::DateTime<chrono::Utc>>,
}

impl Default for Item {
fn default() -> Self {
Item {
item_id: "".to_string(),
name: None,
product_id: None,
owner_id: None,
location_id: None,
created_at: Some(chrono::Utc::now()),
updated_at: Some(chrono::Utc::now()),
}
}
}

impl Item {
pub async fn create(
item_id: String,
Expand Down Expand Up @@ -73,54 +90,3 @@ impl Item {
}
}
}

#[derive(sqlx::FromRow, poem_openapi::Object, Debug, Clone, Serialize, Deserialize)]
pub struct ItemField {
pub item_id: String,
pub definition_id: String,
pub value: serde_json::Value,
pub created_at: Option<chrono::DateTime<chrono::Utc>>,
pub updated_at: Option<chrono::DateTime<chrono::Utc>>,
}

impl ItemField {
pub async fn create(
item_id: String,
definition_id: String,
value: serde_json::Value,
database: &Database,
) -> Result<ItemField, sqlx::Error> {
sqlx::query_as!(
ItemField,
"INSERT INTO item_fields (item_id, definition_id, value) VALUES ($1, $2, $3) RETURNING *",
item_id,
definition_id,
value
)
.fetch_one(&database.pool)
.await
}
}

#[derive(sqlx::FromRow, poem_openapi::Object, Debug, Clone, Serialize, Deserialize)]
pub struct ItemMedia {
pub item_id: String,
pub media_id: i32,
}

impl ItemMedia {
pub async fn create(
item_id: String,
media_id: i32,
database: &Database,
) -> Result<ItemMedia, sqlx::Error> {
sqlx::query_as!(
ItemMedia,
"INSERT INTO item_media (item_id, media_id) VALUES ($1, $2) RETURNING *",
item_id,
media_id
)
.fetch_one(&database.pool)
.await
}
}
10 changes: 6 additions & 4 deletions engine/src/models/locations.rs → engine/src/models/location.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
use serde::{Deserialize, Serialize};
use sqlx::prelude::*;
use chrono::{DateTime, Utc};
use poem_openapi::Object;
use sqlx::FromRow;

use crate::database::Database;

#[derive(sqlx::FromRow, poem_openapi::Object, Debug, Clone, Serialize, Deserialize)]
#[derive(FromRow, Object, Debug, Clone, Serialize, Deserialize)]
pub struct Location {
pub location_id: i32,
pub name: String,
pub created_at: Option<chrono::DateTime<chrono::Utc>>,
pub updated_at: Option<chrono::DateTime<chrono::Utc>>,
pub created_at: Option<DateTime<Utc>>,
pub updated_at: Option<DateTime<Utc>>,
}

impl Location {
Expand Down
11 changes: 6 additions & 5 deletions engine/src/models/media.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
use serde::{Deserialize, Serialize};
use sqlx::prelude::*;

use sqlx::FromRow;
use poem_openapi::Object;
use chrono::{DateTime, Utc};
use crate::database::Database;

#[derive(sqlx::FromRow, poem_openapi::Object, Debug, Clone, Serialize, Deserialize)]
#[derive(FromRow, Object, Debug, Clone, Serialize, Deserialize)]
pub struct Media {
pub media_id: i32,
pub description: Option<String>,
pub url: String,
pub kind: String,
pub created_at: Option<chrono::DateTime<chrono::Utc>>,
pub updated_at: Option<chrono::DateTime<chrono::Utc>>,
pub created_at: Option<DateTime<Utc>>,
pub updated_at: Option<DateTime<Utc>>,
}

impl Media {
Expand Down
6 changes: 3 additions & 3 deletions engine/src/models/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub mod field_definitions;
pub mod items;
pub mod locations;
pub mod field;
pub mod item;
pub mod location;
pub mod media;
pub mod products;
pub mod sessions;
Expand Down
11 changes: 6 additions & 5 deletions engine/src/models/products.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
use serde::{Deserialize, Serialize};
use sqlx::prelude::*;

use sqlx::FromRow;
use poem_openapi::Object;
use chrono::{DateTime, Utc};
use crate::database::Database;

#[derive(sqlx::FromRow, poem_openapi::Object, Debug, Clone, Serialize, Deserialize)]
#[derive(FromRow, Object, Debug, Clone, Serialize, Deserialize)]
pub struct Product {
pub product_id: i32,
pub name: String,
pub created_at: Option<chrono::DateTime<chrono::Utc>>,
pub updated_at: Option<chrono::DateTime<chrono::Utc>>,
pub created_at: Option<DateTime<Utc>>,
pub updated_at: Option<DateTime<Utc>>,
}

impl Product {
Expand Down
Loading

0 comments on commit 49dbcb0

Please sign in to comment.