Skip to content

Commit

Permalink
Buscando alimentos que não venceram
Browse files Browse the repository at this point in the history
Ao buscar os alimentos da geladeira que ainda não estavam vencidos ele não retornava nada,
isso porque a data de vencimento estava como string no banco, então foi necessário mudar para date os lugares onde era usado esse campo (para manter uma formatação BR) e alterar o seeder
  • Loading branch information
DevCapu committed May 22, 2020
1 parent b33a0a3 commit 1ca94a2
Show file tree
Hide file tree
Showing 10 changed files with 18 additions and 12 deletions.
7 changes: 6 additions & 1 deletion app/Http/Controllers/RefrigeratorController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ class RefrigeratorController

public function index()
{
$foods = Auth::user()->refrigerator->foods()->wherePivot('expiration_date', '>', Carbon::now('America/Sao_Paulo')->format('d/m/y'))->get();

$foods = Auth::user()->refrigerator
->foods()
->wherePivot('expiration_date', '>', Carbon::now('America/Sao_Paulo')->format('Y-m-d'))
->get();

return view('refrigerator.index', [
'foods' => $foods,
'userId' => Auth::id()
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Requests/SaveFoodInsideRefrigeratorRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function rules()
return [
'food' => 'required|numeric|exists:foods,id',
'quantity' => 'required|numeric',
'expiration_date' => 'required|date_format:d/m/Y',
'expiration_date' => 'required|date_format:Y-m-d',
'notification' => 'required|numeric',
];
}
Expand Down
1 change: 1 addition & 0 deletions app/Services/InsertFoodInsideRefrigerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public function __construct(FoodRefrigerator $foodRefrigerator)

public function saveFoodInsideTheRefrigerator(SaveFoodInsideRefrigeratorRequest $request): void
{

$this->foodRefrigerator->food_id = $request->food;
$this->foodRefrigerator->refrigerator_id = Auth::user()->refrigerator->id;
$this->foodRefrigerator->quantity = $request->quantity;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function up()
$table->bigInteger('food_id')->unsigned();
$table->bigInteger('refrigerator_id')->unsigned();
$table->integer('quantity')->unsigned();
$table->string('expiration_date');
$table->date('expiration_date');
$table->integer('notification');

$table->foreign('food_id')->references('id')->on('foods');
Expand Down
2 changes: 0 additions & 2 deletions database/seeds/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ class DatabaseSeeder extends Seeder
public function run()
{
$this->call([
UsuarioTableSeeder::class,
AlimentoTableSeeder::class,
UserSeeder::class,
FoodCategoriesSeeder::class,
FoodsSeeder::class,
Expand Down
2 changes: 1 addition & 1 deletion database/seeds/FoodRefrigeratorSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function run()
'food_id' => 2,
'refrigerator_id' => 1,
'quantity' => 3,
'expiration_date' => '06/08/2020',
'expiration_date' => '2020-08-15',
'notification' => 3
]);
}
Expand Down
2 changes: 1 addition & 1 deletion public/js/ingested-food/searchFoods.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
httpClient.get('/alimento')
.then(json => {
const foodOptions = json.map(food => (
`<option value="${food.id}">${food.nome}</option>`
`<option value="${food.id}">${food.name}</option>`
))

const foodDropdown = $('#alimentos');
Expand Down
7 changes: 4 additions & 3 deletions resources/views/refrigerator/create.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<input type="number" name="quantity" value="0" min="0" max="100">
</label>
<label for="validade">Validade
<input type="text" name="expiration_date">
<input type="date" name="expiration_date">
</label>
<label for="notificacao">Avisar quantos dias antes de vencer
<input type="number" name="notification" value="7" min="0">
Expand All @@ -49,6 +49,7 @@
$(this).formSelect();
});
</script>

<script src="{{asset('js/refeicao/busca-alimentos.js')}}"></script>
<script src="{{asset('js/HttpClient.js')}}"></script>
<script src="{{asset('js/ingested-food/initializeFields.js')}}"></script>
<script src="{{asset('js/ingested-food/searchFoods.js')}}"></script>
@endsection
3 changes: 2 additions & 1 deletion resources/views/refrigerator/edit.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
<input type="number" name="quantity" value="{{$food->pivot->quantity}}" min="0" max="100">
</label>
<label for="validade">Validade
<input type="text" name="expiration_date" value="{{$food->pivot->expiration_date}}">
<input type="date" name="expiration_date"
value="{{\Carbon\Carbon::parse($food->pivot->expiration_date)->format('d/m/Y')}}">
</label>
<label for="notificacao">Avisar quantos dias antes de vencer
<input type="number" name="notification" value="{{$food->pivot->notification}}" min="0">
Expand Down
2 changes: 1 addition & 1 deletion resources/views/refrigerator/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<table class="responsive-table highlight">
@forelse($foods as $food)
<tr>
<td>{{$food->name}} (Notificação no dia: {{$food->pivot->expiration_date}})</td>
<td>{{$food->name}} (Notificação no dia: {{\Carbon\Carbon::parse($food->pivot->expiration_date)->format('d/m/Y')}})</td>
<td>
<a href="{{route('refrigerator.edit', ['foodId' => $food->id])}}" class="btn">Editar</a>
</td>
Expand Down

0 comments on commit 1ca94a2

Please sign in to comment.