-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from DevCapu/migrando-refeicoes
Migrando refeicoes
- Loading branch information
Showing
12 changed files
with
297 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
|
||
namespace App\Http\Controllers; | ||
|
||
|
||
use App\Services\IngestedFood; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Support\Facades\Auth; | ||
|
||
class MealController extends Controller | ||
{ | ||
public function index() | ||
{ | ||
return view('meals.create', ['id' => Auth::id()]); | ||
} | ||
|
||
public function store(Request $request, IngestedFood $ingestedFood) | ||
{ | ||
foreach ($request->ingestedFoods as $food) { | ||
$food = json_decode(json_encode($food)); | ||
$ingestedFood->saveIngestedFood($food, $request->id); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
class HttpClient { | ||
constructor() { | ||
this.baseUrl = "http://localhost:8000/api"; | ||
} | ||
|
||
/** | ||
* @param {string} resource | ||
*/ | ||
get(resource) { | ||
return fetch(this.baseUrl + resource) | ||
.then(response => { | ||
if (response.ok) { | ||
return response.json(); | ||
} | ||
throw new Error('Não foi possível buscar o alimento') | ||
}) | ||
.catch(error => console.log(error)) | ||
} | ||
|
||
/** | ||
* @param {string} resource | ||
* @param {json} requestBody | ||
*/ | ||
post(resource, requestBody) { | ||
const body = JSON.stringify(requestBody) | ||
console.log(body) | ||
return fetch(this.baseUrl + resource, | ||
{ | ||
method: 'POST', | ||
body, | ||
headers: new Headers({"Content-Type": "application/json"}) | ||
}) | ||
.then(response => { | ||
if (response.ok) { | ||
return response.json(); | ||
} | ||
throw new Error('Não foi possível buscar o alimento') | ||
}) | ||
.catch(error => console.log(error)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
class TableManager { | ||
addFood(food, selectedMeal) { | ||
const table = $(`#tabela-${selectedMeal}`) | ||
const tableRow = $('<tr></tr>') | ||
const tableCellId = $('<td></td>') | ||
const tableCellName = $('<td></td>') | ||
const tableCellQuantity = $('<td></td>') | ||
|
||
this.insertCellTexts(tableCellId, tableCellName, tableCellQuantity, food); | ||
|
||
tableRow.append(tableCellId, tableCellName, tableCellQuantity) | ||
table.append(tableRow); | ||
} | ||
|
||
/** | ||
* @private | ||
* @param tableCellId | ||
* @param food | ||
* @param tableCellName | ||
* @param tableCellQuantity | ||
*/ | ||
insertCellTexts(tableCellId, tableCellName, tableCellQuantity, food) { | ||
tableCellId.text(food.id); | ||
tableCellId.addClass('hidden'); | ||
tableCellName.text(food.name) | ||
tableCellQuantity.text(food.quantity) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class GetFieldValues { | ||
static getFieldsValues() { | ||
const selectedFoodId = $('#alimentos').val(); | ||
|
||
return { | ||
id: selectedFoodId, | ||
name: $(`option[value="${selectedFoodId}"]`).text(), | ||
quantity: $('#quantidade').val() | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
(function () { | ||
$(document).ready(function () { | ||
$('#refeicao').formSelect(); | ||
$('#alimentos').formSelect(); | ||
$('.tabs').tabs(); | ||
}); | ||
|
||
$('#alimentos').on('contentChanged', function () { | ||
$(this).formSelect(); | ||
}); | ||
})() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
(function () { | ||
const formAlimento = document.querySelector('#adicionaAlimento'); | ||
formAlimento.addEventListener('submit', event => { | ||
event.preventDefault(); | ||
const food = GetFieldValues.getFieldsValues(); | ||
new TableManager().addFood(food, $('#refeicao').val()) | ||
}) | ||
})() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
(function () { | ||
$('#insertIngestedFoods').click(function () { | ||
let foods = []; | ||
let periods = ['BREAKFAST', 'LUNCH', 'AFTERNOON_COFFEE', 'DINNER']; | ||
|
||
$('table').each(function (index) { | ||
const rows = $(this).children('tbody').children('tr'); | ||
period = periods[index]; | ||
rows.each(function () { | ||
const id = $(this).children('td').eq(0).text(); | ||
const quantity = $(this).children('td').eq(2).text(); | ||
const ingestedFood = {id, quantity, period}; | ||
foods = [...foods, ingestedFood]; | ||
}) | ||
}) | ||
const requestBody = {ingestedFoods: foods, id}; | ||
var httpClient = new HttpClient(); | ||
httpClient.post('/meal', requestBody); | ||
}); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
(function () { | ||
const httpClient = new HttpClient(); | ||
httpClient.get('/alimento') | ||
.then(json => { | ||
const foodOptions = json.map(food => ( | ||
`<option value="${food.id}">${food.nome}</option>` | ||
)) | ||
|
||
const foodDropdown = $('#alimentos'); | ||
foodDropdown.append(foodOptions.join('')) | ||
$("#alimentos").trigger('contentChanged'); | ||
}); | ||
})() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
@extends("template") | ||
|
||
@section("conteudo") | ||
<div class="row"> | ||
<section class="col s12 m4"> | ||
<form action="#" id="adicionaAlimento"> | ||
<div class="input-field col s12"> | ||
<select name="refeicao" id="refeicao" required> | ||
<option value="cafeDaManha" selected>Café da manhã</option> | ||
<option value="almoco">Almoço</option> | ||
<option value="cafeDaTarde">Café da tarde</option> | ||
<option value="jantar">Jantar</option> | ||
</select> | ||
<label for="refeicao">Em qual refeição você deseja adicionar?</label> | ||
</div> | ||
|
||
<div class="input-field col s12"> | ||
<select name="alimento" id="alimentos" required> | ||
</select> | ||
<label for="alimentos">Qual alimento você ingeriu?</label> | ||
</div> | ||
<div class="input-field col s12"> | ||
<input id="quantidade" type="number" name="quantidade" step="1" min="1" max="100"> | ||
<label for="quantidade">Quantidade em unidades</label> | ||
</div> | ||
<button class="btn" type="submit">Adicionar</button> | ||
</form> | ||
</section> | ||
|
||
|
||
<div class="col s12 m8"> | ||
<button id="insertIngestedFoods" class="btn right">Salvar refeições</button> | ||
<br><br> | ||
<ul class="tabs"> | ||
<li class="tab col s3"><a class="active" href="#cafeDaManha">Café da manhã</a></li> | ||
<li class="tab col s3"><a href="#almoco">Almoço</a></li> | ||
<li class="tab col s3"><a href="#cafeDaTarde">Café da tarde</a></li> | ||
<li class="tab col s3"><a href="#jantar">Jantar</a></li> | ||
</ul> | ||
|
||
<div class="row" id="cafeDaManha"> | ||
<table class="centered col s12" id="tabela-cafeDaManha"> | ||
<thead> | ||
<tr> | ||
<th colspan="2">Café da manhã</th> | ||
</tr> | ||
<tr> | ||
<th>Nome</th> | ||
<th>Quantidade</th> | ||
</tr> | ||
</thead> | ||
<tbody></tbody> | ||
</table> | ||
</div> | ||
<div class="row" id="almoco"> | ||
<table class="centered col s12" id="tabela-almoco"> | ||
<thead> | ||
<tr> | ||
<th colspan="2">Almoço</th> | ||
</tr> | ||
<tr> | ||
<th>Nome</th> | ||
<th>Quantidade</th> | ||
</tr> | ||
</thead> | ||
<tbody></tbody> | ||
</table> | ||
</div> | ||
<div class="row" id="cafeDaTarde"> | ||
<table class="centered col s12" id="tabela-cafeDaTarde"> | ||
<thead> | ||
<tr> | ||
<th colspan="2">Café da tarde</th> | ||
</tr> | ||
<tr> | ||
<th>Nome</th> | ||
<th>Quantidade</th> | ||
</tr> | ||
</thead> | ||
<tbody></tbody> | ||
</table> | ||
</div> | ||
<div class="row" id="jantar"> | ||
<table class="centered col s12" id="tabela-jantar"> | ||
<thead> | ||
<tr> | ||
<th colspan="2">Janta</th> | ||
</tr> | ||
<tr> | ||
<th>Nome</th> | ||
<th>Quantidade</th> | ||
</tr> | ||
</thead> | ||
<tbody></tbody> | ||
</table> | ||
</div> | ||
</div> | ||
</div> | ||
@endsection | ||
|
||
@section("scripts") | ||
<script>const id = {{__($id)}};</script> | ||
<script src="{{asset('js/HttpClient.js')}}"></script> | ||
<script src="{{asset('js/ingested-food/initializeFields.js')}}"></script> | ||
<script src="{{asset('js/ingested-food/TableManager.js')}}"></script> | ||
<script src="{{asset('js/ingested-food/initializeInsertButton.js')}}"></script> | ||
<script src="{{asset('js/ingested-food/searchFoods.js')}}"></script> | ||
<script src="{{asset('js/ingested-food/getFieldValues.js')}}"></script> | ||
<script src="{{asset('js/ingested-food/insertIngestedFoods.js')}}"></script> | ||
@endsection |