Skip to content

Commit

Permalink
Merge pull request #115 from suchapalaver/feature/replace-JSON-store-…
Browse files Browse the repository at this point in the history
…with-file-import-support

Replace JSON store with file import-export support
  • Loading branch information
suchapalaver authored Jan 4, 2024
2 parents 8501d98 + c261dd0 commit ca813c9
Show file tree
Hide file tree
Showing 36 changed files with 785 additions and 1,681 deletions.
2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
!Cargo.*
!crates
!.env
!items.json
!list.json
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ target/
# These are backup files generated by rustfmt
**/*.rs.bk

# Local JSON 'groceries.json' and 'list.json' files
# Local JSON 'items.json' and 'list.json' files
*.json

/sqlite.db
*.yaml

/sqlite.db
65 changes: 42 additions & 23 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ scraper = "0.18.1"
serde = { version = "*", features = ["derive"] }
serde_derive = "*"
serde_json = "*"
serde_yaml = "0.9.30"
thiserror = "1.0.48"
tokio = { version = "1", features = ["full"] }
tracing = "0.1.37"
Expand Down
18 changes: 17 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.PHONY: build help fetch read add list clear
.PHONY: build help fetch read add list export clear

build:
docker build --tag gust --file Dockerfile .
Expand Down Expand Up @@ -32,5 +32,21 @@ add:
list:
docker run --rm -v gust:/app gust read list

export:
if [ ! -f "$(PWD)/items.yaml" ]; then \
echo "Error: items.yaml not found in $(PWD)."; \
exit 1; \
fi
if [ ! -f "$(PWD)/list.yaml" ]; then \
echo "Error: list.yaml not found in $(PWD)."; \
exit 1; \
fi
docker run --rm \
-v gust_data:/app \
-v $(PWD)/items.yaml:/app/items.yaml \
-v $(PWD)/list.yaml:/app/list.yaml \
gust \
export

clear:
docker run --rm -v gust:/app gust update list clear
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,19 @@ All you need is to [install Docker](https://docs.docker.com/install/).

- [help](./docs/cli.md#help)
- [fetching recipes](./docs/cli.md#fetching-recipes)
- [importing/exporting data](./docs/cli.md#importing-and-exporting-data)

### [database](./docs/database.md)

- [storage options](./docs/database.md#storage-options)
- [json](./docs/database.md#json)
- [sqlite](./docs/database.md#sqlite)
- [postgres](./docs/database.md#postgresql)

### [docker](./docs/docker.md)

- [data volumes](./docker.md#creating-a-gust_data-volume)
- [migrating from JSON to SQLite](./docker.md#migrate-a-json-gust-store-to-sqlite)
- [data volumes](./docs/docker.md#creating-a-gust_data-volume)
- [migrating from JSON to SQLite](./docs/docker.md#migrate-a-json-gust-store-to-sqlite)
- [exporting data to YAML](./docs/docker.md#export-data-to-yaml)

---
## getting started
Expand Down
24 changes: 19 additions & 5 deletions crates/api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ use std::fmt::{self, Display};

use common::{
commands::ApiCommand,
item::{Item, Name, Section},
item::{Item, Name},
items::Items,
list::List,
recipes::{Ingredients, Recipe},
section::Section,
};
use persistence::store::{Store, StoreDispatch, StoreError, StoreResponse, StoreType};

Expand Down Expand Up @@ -117,10 +118,11 @@ pub enum ApiResponse {
Checklist(Vec<Item>),
DeletedRecipe(Recipe),
DeletedChecklistItem(Name),
Exported(Vec<Item>, List),
FetchedRecipe((Recipe, Ingredients)),
ItemAlreadyAdded(Name),
Items(Items),
JsonToSqlite,
ImportToSqlite,
List(List),
NothingReturned(ApiCommand),
Recipes(Vec<Recipe>),
Expand Down Expand Up @@ -149,6 +151,17 @@ impl Display for ApiResponse {
}
Self::DeletedChecklistItem(name) => writeln!(f, "\ndeleted from checklist: \n{name}"),
Self::DeletedRecipe(recipe) => writeln!(f, "\ndeleted recipe: \n{recipe}"),
Self::Exported(items, list) => {
writeln!(f, "\nexported items:")?;
for item in items {
writeln!(f, " {item}")?;
}
writeln!(f, "\nexported list:")?;
for item in list.items() {
writeln!(f, " {item}")?;
}
Ok(())
}
Self::FetchedRecipe((recipe, ingredients)) => {
writeln!(f, "\n{recipe}:")?;
for ingredient in ingredients.iter() {
Expand All @@ -159,12 +172,12 @@ impl Display for ApiResponse {
Self::ItemAlreadyAdded(item) => writeln!(f, "\nitem already added: {item}"),
Self::Items(items) => {
writeln!(f)?;
for item in items.collection() {
for item in items.collection_iter() {
writeln!(f, "{item}")?;
}
Ok(())
}
Self::JsonToSqlite => writeln!(f, "\nJSON to SQLite data store migration successful"),
Self::ImportToSqlite => writeln!(f, "\nImport successful"),
Self::List(list) => {
writeln!(f)?;
for item in list.items() {
Expand Down Expand Up @@ -213,10 +226,11 @@ impl From<StoreResponse> for ApiResponse {
StoreResponse::Checklist(item) => Self::Checklist(item),
StoreResponse::DeletedRecipe(item) => Self::DeletedRecipe(item),
StoreResponse::DeletedChecklistItem(item) => Self::DeletedChecklistItem(item),
StoreResponse::Exported(items, list) => Self::Exported(items, list),
StoreResponse::FetchedRecipe(item) => Self::FetchedRecipe(item),
StoreResponse::ItemAlreadyAdded(item) => Self::ItemAlreadyAdded(item),
StoreResponse::Items(item) => Self::Items(item),
StoreResponse::JsonToSqlite => Self::JsonToSqlite,
StoreResponse::ImportToSqlite => Self::ImportToSqlite,
StoreResponse::List(item) => Self::List(item),
StoreResponse::NothingReturned(item) => Self::NothingReturned(item),
StoreResponse::Recipes(item) => Self::Recipes(item),
Expand Down
1 change: 1 addition & 0 deletions crates/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ scraper = { workspace = true }
serde = { workspace = true }
serde_derive = { workspace = true }
serde_json = { workspace = true }
serde_yaml = { workspace = true }
thiserror = { workspace = true }
tokio = { workspace = true }
tracing = { workspace = true }
Expand Down
6 changes: 4 additions & 2 deletions crates/common/src/commands.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
use url::Url;

use crate::{
item::{Name, Section},
item::Name,
recipes::{Ingredients, Recipe},
section::Section,
};

#[derive(Debug)]
pub enum ApiCommand {
Add(Add),
Delete(Delete),
Export,
FetchRecipe(Url),
MigrateJsonDbToSqlite,
ImportFromJson,
Read(Read),
Update(Update),
}
Expand Down
Loading

0 comments on commit ca813c9

Please sign in to comment.