Skip to content
This repository has been archived by the owner on Jun 21, 2022. It is now read-only.

Ingredients editor #163

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "share/coocook-react-components"]
path = share/coocook-react-components
url = git@github.com:moseschmiedel/coocook-react-components
106 changes: 106 additions & 0 deletions doc/entities.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# Entities
## Article
**table:** articles
### Attributes
#### id
**type:** integer
Primary key of `Article` for database.

#### project_id
**type:** integer
Foreign key on `Project`. References the `Project` that this `Article` was created or imported in.

#### shop_section_id
**type:** integer
Foreign key on `ShopSection`. References the `ShopSection` where this `Article` can be found.

#### shelf_life_days
**type:** integer
Indicates how many days the `Article` could be stored, before it has to be processed.

#### preorder_servings
**type:** integer

#### preorder_workdays
**type:** integer

#### name
**type:** integer
Human-readable name of the `Article`.

#### comment
**type:** text
User specified comment of the `Article`.


## Unit
**table:** units
### Attributes
#### id
**type:** integer
Primary key of `Unit` for database.

#### project_id
**type:** integer
Foreign key on `Project`. References the `Project` that this `Unit` was created or imported in.

#### quantity_id
**type:** integer
Foreign key on `Quantity`. References the `Quantity` that this `Unit` is for.

#### to_quantity_default
**type:** real
Factor to multiply by to get to the value in the `Quantity`'s default `Unit`.

#### space'
**type:** boolean
Indicates if the `Unit`'s (short\_/long\_)name should be prepended with a space or not.

#### short_name
**type:** text
Human-readable short variant of the name of the `Unit`. E.g.: `kg` for Kilogram.

#### long_name
**type:** text
Human-readable long variant of the name of the `Unit`. E.g.: `Kilogram` for Kilogram.


## DishIngredient
**table:** dish_ingredients
### Attributes
#### id
**type:** integer
Primary key of `DishIngredient` for database.

#### position
**type:** integer
Position of the `DishIngredient` in the `IngredientsEditor`.

#### dish_id
**type:** integer
Foreign key on the `id` of a `Dish`. References the `Dish` that this `DishIngredient` belongs to.

#### prepare
**type:** boolean
Indicates wether this `DishIngredient` has to be prepared before processing or not.

#### article_id
**type:** integer
Foreign key on the `id` of an `Article`. References the `Article` that this `DishIngredient` is derived from.

#### unit_id
**type:** integer
Foreign key on the `id` of an `Unit`. References the `Unit` this `DishIngredient` is in.

#### value
**type:** real
Current value of the `DishIngredient`.

#### comment
**type:** text
User specified comment of the `DishIngredient`.

#### item_id
**type:** integer
Foreign key on `id` of `Item`. References the `Item` on a `PurchaseList` that belongs to this `DishIngredient`.

14 changes: 14 additions & 0 deletions lib/Coocook/ActionRole/Ajax.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package Coocook::ActionRole::Ajax;

# ABSTRACT: role for controller actions that respond with JSON and require a Session

use Moose::Role;
use MooseX::MarkAsMethods autoclean => 1;

before execute => sub {
my ( $self, $controller, $c ) = @_;

$c->stash( current_view => 'JSON' );
};

1;
25 changes: 25 additions & 0 deletions lib/Coocook/Controller/API.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package Coocook::Controller::API;

use Moose;
use MooseX::MarkAsMethods autoclean => 1;

# BEGIN-block necessary to make method attributes work
BEGIN { extends 'Coocook::Controller' }

sub base : Chained('/') PathPart('api') CaptureArgs(0) {
my ( $self, $c ) = @_;

$c->stash( current_view => 'JSON' );
}

sub statistics : GET HEAD Chained('base') Args(0) Public {
my ( $self, $c ) = @_;

$c->stash( json_data => $c->model('DB')->statistics );
}

sub end : ActionClass('RenderView') { } # Overrides Controller::Root->end

__PACKAGE__->meta->make_immutable;

1;
28 changes: 28 additions & 0 deletions lib/Coocook/Controller/Dish.pm
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package Coocook::Controller::Dish;

use Moose;
use MooseX::MarkAsMethods autoclean => 1;
use JSON;

BEGIN { extends 'Coocook::Controller' }

Expand Down Expand Up @@ -49,6 +50,11 @@ sub edit : GET HEAD Chained('base') PathPart('') Args(0) RequiresCapability('vie

$c->stash(
dish => {
project => {
id => $c->project->id,
name => $c->project->name,
},
id => $dish->id,
name => $dish->name,
comment => $dish->comment,
servings => $dish->servings,
Expand Down Expand Up @@ -228,6 +234,28 @@ sub reposition : POST Chained('/project/base') PathPart('dish_ingredient/reposit
$c->detach( redirect => [ $ingredient->dish_id, '#ingredients' ] );
}

sub ingredients : GET HEAD Does('~Ajax') Chained('base') {
my ( $self, $c ) = @_;

my $ingredients = $c->model('Ingredients')->new(
project => $c->project,
ingredients => $c->stash->{dish}->ingredients,
);

$c->stash->{json_data} = $ingredients->for_ingredients_editor;

#$c->forward('View::JSON');
}

sub updateAJAX : POST PathPart('ingredient/update') Does('~Ajax') Chained('base') RequiresCapability('edit_project') {
my ( $self, $c ) = @_;
my $json = $c->req->body_data;
my $ingredient = $json->{ingredient} || die "wrong JSON body!";


$c->stash->{json_data} = { id => $ingredient->{id} };
}

sub redirect : Private {
my ( $self, $c, $id, $fragment ) = @_;

Expand Down
1 change: 1 addition & 0 deletions lib/Coocook/Controller/Root.pm
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ sub auto : Private {
'/lib/jquery-3.5.1' . ( $c->debug ? '.js' : '.min.js' ),
'/lib/bootstrap-4.4.1-dist/js/bootstrap' . ( $c->debug ? '.js' : '.min.js' ),
'/lib/marked/marked' . ( $c->debug ? '.js' : '.min.js' ),
'/lib/ingredients_editor/dist/index' . ( $c->debug ? '.js' : '.min.js' ),
'/js/script.js',
],
);
Expand Down
44 changes: 38 additions & 6 deletions lib/Coocook/Model/Ingredients.pm
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,13 @@ sub as_arrayref {
while ( my $ingredient = $ingredients->next ) {
push @ingredients,
{
id => $ingredient->id,
prepare => $ingredient->format_bool( $ingredient->prepare ),
value => $ingredient->value * $self->factor,
comment => $ingredient->comment,
unit => $units{ $ingredient->unit_id },
article => $articles{ $ingredient->article_id },
id => $ingredient->id,
prepare => $ingredient->format_bool( $ingredient->prepare ),
position => $ingredient->position,
value => $ingredient->value * $self->factor,
comment => $ingredient->comment,
unit => $units{ $ingredient->unit_id },
article => $articles{ $ingredient->article_id },
};
}
}
Expand All @@ -88,4 +89,35 @@ sub as_arrayref {
return \@ingredients;
}

sub for_ingredients_editor {
my $self = shift;

my $ingredients = $self->as_arrayref;

my @ingredients = map {
my $unit = $_->{unit};
my @convertible_units = $unit->convertible_into->all;

# transform Result::Unit objects into plain hashes
for ( $unit, @convertible_units ) {
my $u = $_;

$_ = { map { $_ => $u->get_column($_) } qw<id short_name long_name> };
}

{
id => $_->{id},
prepare => $_->{prepare},
position => $_->{position},
value => $_->{value},
comment => $_->{comment},
article => { name => $_->{article}->name, comment => $_->{article}->comment },
current_unit => $unit,
units => \@convertible_units,
}
} @$ingredients;

return \@ingredients;
}

1;
16 changes: 16 additions & 0 deletions lib/Coocook/View/JSON.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package Coocook::View::JSON;

# ABSTRACT: view for Coocook to create

use Moose;

use MooseX::MarkAsMethods autoclean => 1;
use MooseX::NonMoose;

extends 'Catalyst::View::JSON';

__PACKAGE__->meta->make_immutable;

__PACKAGE__->config( expose_stash => 'json_data', );

1;
3 changes: 3 additions & 0 deletions root/static/lib/ingredients_editor/_snowpack/env.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const MODE = "development";
export const NODE_ENV = "development";
export const SSR = false;
Loading