From a41adc5414df63ca3a1d024ee8f6aa92dd01c671 Mon Sep 17 00:00:00 2001 From: Felipe Moreno Borges Date: Fri, 8 May 2020 23:59:32 -0300 Subject: [PATCH 1/3] Refatorando alimentos ingeridos no dia --- public/js/HttpClient.js | 19 +++++++++++++++++++ public/js/meals/addFood.js | 0 public/js/meals/initializeFields.js | 11 +++++++++++ public/js/meals/searchFoods.js | 13 +++++++++++++ 4 files changed, 43 insertions(+) create mode 100644 public/js/HttpClient.js create mode 100644 public/js/meals/addFood.js create mode 100644 public/js/meals/initializeFields.js create mode 100644 public/js/meals/searchFoods.js diff --git a/public/js/HttpClient.js b/public/js/HttpClient.js new file mode 100644 index 0000000..fe5daeb --- /dev/null +++ b/public/js/HttpClient.js @@ -0,0 +1,19 @@ +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)) + } +} diff --git a/public/js/meals/addFood.js b/public/js/meals/addFood.js new file mode 100644 index 0000000..e69de29 diff --git a/public/js/meals/initializeFields.js b/public/js/meals/initializeFields.js new file mode 100644 index 0000000..2e5449e --- /dev/null +++ b/public/js/meals/initializeFields.js @@ -0,0 +1,11 @@ +(function () { + $(document).ready(function () { + $('#refeicao').formSelect(); + $('#alimentos').formSelect(); + $('.tabs').tabs(); + }); + + $('#alimentos').on('contentChanged', function () { + $(this).formSelect(); + }); +})() diff --git a/public/js/meals/searchFoods.js b/public/js/meals/searchFoods.js new file mode 100644 index 0000000..877b850 --- /dev/null +++ b/public/js/meals/searchFoods.js @@ -0,0 +1,13 @@ +(function () { + const httpClient = new HttpClient(); + httpClient.get('/alimento') + .then(json => { + const foodOptions = json.map(food => ( + `` + )) + + const foodDropdown = $('#alimentos'); + foodDropdown.append(foodOptions.join('')) + $("#alimentos").trigger('contentChanged'); + }); +})() From 6a3657b3d774277041328fbb9b6940e7022509bc Mon Sep 17 00:00:00 2001 From: Felipe Moreno Borges Date: Thu, 14 May 2020 01:39:04 -0300 Subject: [PATCH 2/3] Criando view e refatorando JS pra usar ES6 --- public/js/HttpClient.js | 22 ++++ public/js/ingested-food/TableManager.js | 28 +++++ public/js/ingested-food/getFieldValues.js | 11 ++ .../initializeFields.js | 0 .../ingested-food/initializeInsertButton.js | 8 ++ .../js/ingested-food/insertIngestedFoods.js | 20 ++++ .../{meals => ingested-food}/searchFoods.js | 0 public/js/meals/addFood.js | 0 public/js/refeicao/busca-alimentos.js | 1 + public/js/refeicao/salva-refeicoes.js | 26 +---- resources/views/meals/create.blade.php | 110 ++++++++++++++++++ 11 files changed, 206 insertions(+), 20 deletions(-) create mode 100644 public/js/ingested-food/TableManager.js create mode 100644 public/js/ingested-food/getFieldValues.js rename public/js/{meals => ingested-food}/initializeFields.js (100%) create mode 100644 public/js/ingested-food/initializeInsertButton.js create mode 100644 public/js/ingested-food/insertIngestedFoods.js rename public/js/{meals => ingested-food}/searchFoods.js (100%) delete mode 100644 public/js/meals/addFood.js create mode 100644 resources/views/meals/create.blade.php diff --git a/public/js/HttpClient.js b/public/js/HttpClient.js index fe5daeb..6627e66 100644 --- a/public/js/HttpClient.js +++ b/public/js/HttpClient.js @@ -16,4 +16,26 @@ class HttpClient { }) .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)) + } } diff --git a/public/js/ingested-food/TableManager.js b/public/js/ingested-food/TableManager.js new file mode 100644 index 0000000..de2f147 --- /dev/null +++ b/public/js/ingested-food/TableManager.js @@ -0,0 +1,28 @@ +class TableManager { + addFood(food, selectedMeal) { + const table = $(`#tabela-${selectedMeal}`) + const tableRow = $('') + const tableCellId = $('') + const tableCellName = $('') + const tableCellQuantity = $('') + + 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) + } +} diff --git a/public/js/ingested-food/getFieldValues.js b/public/js/ingested-food/getFieldValues.js new file mode 100644 index 0000000..3f792b5 --- /dev/null +++ b/public/js/ingested-food/getFieldValues.js @@ -0,0 +1,11 @@ +class GetFieldValues { + static getFieldsValues() { + const selectedFoodId = $('#alimentos').val(); + + return { + id: selectedFoodId, + name: $(`option[value="${selectedFoodId}"]`).text(), + quantity: $('#quantidade').val() + }; + } +} diff --git a/public/js/meals/initializeFields.js b/public/js/ingested-food/initializeFields.js similarity index 100% rename from public/js/meals/initializeFields.js rename to public/js/ingested-food/initializeFields.js diff --git a/public/js/ingested-food/initializeInsertButton.js b/public/js/ingested-food/initializeInsertButton.js new file mode 100644 index 0000000..21d6552 --- /dev/null +++ b/public/js/ingested-food/initializeInsertButton.js @@ -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()) + }) +})() diff --git a/public/js/ingested-food/insertIngestedFoods.js b/public/js/ingested-food/insertIngestedFoods.js new file mode 100644 index 0000000..e7a2ec8 --- /dev/null +++ b/public/js/ingested-food/insertIngestedFoods.js @@ -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); + }); +})(); diff --git a/public/js/meals/searchFoods.js b/public/js/ingested-food/searchFoods.js similarity index 100% rename from public/js/meals/searchFoods.js rename to public/js/ingested-food/searchFoods.js diff --git a/public/js/meals/addFood.js b/public/js/meals/addFood.js deleted file mode 100644 index e69de29..0000000 diff --git a/public/js/refeicao/busca-alimentos.js b/public/js/refeicao/busca-alimentos.js index 34c487a..ca9e43c 100644 --- a/public/js/refeicao/busca-alimentos.js +++ b/public/js/refeicao/busca-alimentos.js @@ -14,6 +14,7 @@ ); }); selectAlimentos.innerHTML = opcoesDeAlimentos.join(''); + $("#alimentos").trigger('contentChanged'); }) .catch(error => console.error(error)); })(); diff --git a/public/js/refeicao/salva-refeicoes.js b/public/js/refeicao/salva-refeicoes.js index b09628f..1c097c0 100644 --- a/public/js/refeicao/salva-refeicoes.js +++ b/public/js/refeicao/salva-refeicoes.js @@ -6,33 +6,20 @@ tabelas.each(function (index) { let alimentos = []; let periodo; - console.log(index); - switch (index) { - case 0: - periodo = 'CAFÉ DA MANHÃ'; - break; - case 1: - periodo = 'ALMOÇO'; - break; - case 2: - periodo = 'CAFÉ DA TARDE'; - break; - case 3: - periodo = 'JANTAR'; - break; - } + + meals = ['BREAKFAST', 'LUNCH', 'AFTERNOON_COFFEE', 'DINNER']; + periodo = meals[index]; const tr = $(this).children('tbody').children('tr'); tr.each(function () { const tdId = $(this).children('td').eq(0); const tdQuantidade = $(this).children('td').eq(2); - const alimento = {id: tdId.text(), quantidade: tdQuantidade.text()}; + const alimento = {id: tdId.text(), quantity: tdQuantidade.text()}; alimentos = [...alimentos, alimento]; }); - requestBody = {"periodo": periodo, "alimentos": alimentos, "id": id}; + requestBody = {"period": periodo, "foods": alimentos, "id": id}; const body = JSON.stringify(requestBody); console.log(body); - console.log(body); - fetch('/api/refeicao', + fetch('/api/meal', { method: 'POST', body: body, @@ -45,7 +32,6 @@ if (response.ok) { return response.json(); } - console.log(response); throw new Error("Não foi possível buscar os alimentos"); }) .then(json => { diff --git a/resources/views/meals/create.blade.php b/resources/views/meals/create.blade.php new file mode 100644 index 0000000..f8fe660 --- /dev/null +++ b/resources/views/meals/create.blade.php @@ -0,0 +1,110 @@ +@extends("template") + +@section("conteudo") +
+
+
+
+ + +
+ +
+ + +
+
+ + +
+ +
+
+ + +
+ +

+ + +
+ + + + + + + + + + + +
Café da manhã
NomeQuantidade
+
+
+ + + + + + + + + + + +
Almoço
NomeQuantidade
+
+
+ + + + + + + + + + + +
Café da tarde
NomeQuantidade
+
+
+ + + + + + + + + + + +
Janta
NomeQuantidade
+
+
+
+@endsection + +@section("scripts") + + + + + + + + +@endsection From 02f584010c6648f627a2770eec9ff6d45a81b6b6 Mon Sep 17 00:00:00 2001 From: Felipe Moreno Borges Date: Thu, 14 May 2020 01:39:50 -0300 Subject: [PATCH 3/3] Criando controller de alimentos ingeridos --- app/Http/Controllers/MealController.php | 26 +++++++++++++++++++++++++ app/Services/IngestedFood.php | 22 +++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 app/Http/Controllers/MealController.php diff --git a/app/Http/Controllers/MealController.php b/app/Http/Controllers/MealController.php new file mode 100644 index 0000000..1342be7 --- /dev/null +++ b/app/Http/Controllers/MealController.php @@ -0,0 +1,26 @@ + 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); + } + } + +} diff --git a/app/Services/IngestedFood.php b/app/Services/IngestedFood.php index 1e71f64..686bdfa 100644 --- a/app/Services/IngestedFood.php +++ b/app/Services/IngestedFood.php @@ -4,9 +4,12 @@ namespace App\Services; +use App\Models\Food; +use App\Models\Ingested; use App\Models\User; use Carbon\Carbon; use Illuminate\Database\Eloquent\Collection; +use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; class IngestedFood @@ -94,4 +97,23 @@ public function getRows() } return $max; } + + public function saveIngestedFood($food, int $userId): void + { + $ingested = new Ingested(); + + + $ingested->food_id = $food->id; + $ingested->quantity = $food->quantity; + + $ingested->user_id = $userId; + $ingested->period = $food->period; + + $ingested->calories = (Food::find($ingested->food_id)) + ->calories * $ingested->quantity; + + $ingested->date = Carbon::now('America/Sao_Paulo')->format('d/m/y'); + + $ingested->saveOrFail(); + } }