Skip to content

Commit 8444101

Browse files
authored
Merge pull request #12 from spech66/copilot/remove-bmi-in-weight-inputs
Remove BMI from weight inputs - calculate from user settings
2 parents ccc563d + 53602c0 commit 8444101

18 files changed

Lines changed: 648 additions & 49 deletions
Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
11
using System.ComponentModel.DataAnnotations;
2-
using System.ComponentModel.DataAnnotations.Schema;
32

43
namespace LifelogBb.ApiDTOs.Weights
54
{
65
public class WeightInput
76
{
8-
[Range(40, 220)] // 100 cm => 39,3701 inch...
9-
public int Height { get; set; }
10-
117
[Range(40, 440)] // 200 Kg => 440 lbs...
128
public double BodyWeight { get; set; }
13-
14-
public double Bmi => ((BodyWeight * 1.0) / (((Height * 0.01) * Height) * 0.01));
159
}
1610
}

LifelogBb/ApiServices/WeightsService.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
using LifelogBb.ApiDTOs.Weights;
33
using LifelogBb.Interfaces;
44
using LifelogBb.Models.Entities;
5+
using LifelogBb.Utilities;
6+
using Microsoft.EntityFrameworkCore;
57

68
namespace LifelogBb.ApiServices
79
{
@@ -10,5 +12,38 @@ public class WeightsService : BaseCRUDService<Weight, WeightInput, WeightOutput>
1012
public WeightsService(IRepository<Weight> repository, IMapper mapper) : base(repository, mapper)
1113
{
1214
}
15+
16+
public override async Task<WeightOutput> Create(WeightInput inputModel)
17+
{
18+
var entry = _mapper.Map<Weight>(inputModel);
19+
var config = Config.GetConfig(_repository.Context);
20+
entry.Height = config.Height;
21+
entry.Bmi = BmiHelper.Calculate(entry.BodyWeight, entry.Height, config.UnitsType);
22+
entry.SetCreateFields();
23+
24+
_repository.Insert(entry);
25+
await _repository.Context.SaveChangesAsync();
26+
27+
return _mapper.Map<WeightOutput>(entry);
28+
}
29+
30+
public override async Task<WeightOutput> Update(long id, WeightInput inputModel)
31+
{
32+
var dbEntry = await _repository.Query.FirstOrDefaultAsync(m => m.Id == id);
33+
34+
if (dbEntry == null || id != dbEntry.Id)
35+
throw new Exception("Invalid id");
36+
37+
dbEntry = _mapper.Map<WeightInput, Weight>(inputModel, dbEntry);
38+
var config = Config.GetConfig(_repository.Context);
39+
dbEntry.Height = config.Height;
40+
dbEntry.Bmi = BmiHelper.Calculate(dbEntry.BodyWeight, dbEntry.Height, config.UnitsType);
41+
dbEntry.SetUpdateFields();
42+
43+
_repository.Update(dbEntry);
44+
await _repository.Context.SaveChangesAsync();
45+
return _mapper.Map<WeightOutput>(dbEntry);
46+
}
47+
1348
}
1449
}

LifelogBb/Controllers/WeightsController.cs

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,12 @@ public IActionResult Create()
119119
Weight? weight = _context.Weights.OrderByDescending(o => o.CreatedAt).FirstOrDefault();
120120
if(weight != null)
121121
{
122-
weight.Id = 0;
122+
var model = new EditWeightViewModel
123+
{
124+
BodyWeight = weight.BodyWeight
125+
};
123126
ViewData["UnitsType"] = Config.GetConfig(_context).UnitsType;
124-
return View(weight);
127+
return View(model);
125128
}
126129

127130
ViewData["UnitsType"] = Config.GetConfig(_context).UnitsType;
@@ -131,18 +134,24 @@ public IActionResult Create()
131134
// POST: Weights/Create
132135
[HttpPost]
133136
[ValidateAntiForgeryToken]
134-
public async Task<IActionResult> Create([Bind("Height,BodyWeight")] Weight weight)
137+
public async Task<IActionResult> Create([Bind("BodyWeight")] EditWeightViewModel weightViewModel)
135138
{
136139
if (ModelState.IsValid)
137140
{
138-
CalculateBmi(weight);
141+
var config = Config.GetConfig(_context);
142+
var weight = new Weight
143+
{
144+
BodyWeight = weightViewModel.BodyWeight,
145+
Height = config.Height
146+
};
147+
weight.Bmi = BmiHelper.Calculate(weight.BodyWeight, weight.Height, config.UnitsType);
139148
weight.SetCreateFields();
140149
_context.Add(weight);
141150
await _context.SaveChangesAsync();
142151
return RedirectToAction(nameof(Index));
143152
}
144153
ViewData["UnitsType"] = Config.GetConfig(_context).UnitsType;
145-
return View(weight);
154+
return View(weightViewModel);
146155
}
147156

148157
// GET: Weights/Edit/5
@@ -166,7 +175,7 @@ public async Task<IActionResult> Edit(long? id)
166175
// POST: Weights/Edit/5
167176
[HttpPost]
168177
[ValidateAntiForgeryToken]
169-
public async Task<IActionResult> Edit(long id, [Bind("Height,BodyWeight,Id")] EditWeightViewModel weightViewModel)
178+
public async Task<IActionResult> Edit(long id, [Bind("BodyWeight,Id")] EditWeightViewModel weightViewModel)
170179
{
171180
if (id != weightViewModel.Id)
172181
{
@@ -179,7 +188,9 @@ public async Task<IActionResult> Edit(long id, [Bind("Height,BodyWeight,Id")] Ed
179188
try
180189
{
181190
weightDb = _mapper.Map(weightViewModel, weightDb);
182-
CalculateBmi(weightDb);
191+
var config = Config.GetConfig(_context);
192+
weightDb.Height = config.Height;
193+
weightDb.Bmi = BmiHelper.Calculate(weightDb.BodyWeight, weightDb.Height, config.UnitsType);
183194
weightDb.SetUpdateFields();
184195
_context.Update(weightDb);
185196
await _context.SaveChangesAsync();
@@ -243,17 +254,5 @@ private bool WeightExists(long id)
243254
{
244255
return _context.Weights.Any(e => e.Id == id);
245256
}
246-
247-
private void CalculateBmi(Weight weight)
248-
{
249-
var measurements = Config.GetConfig(_context).UnitsType;
250-
if (measurements == Measurements.Metric)
251-
{
252-
weight.Bmi = (weight.BodyWeight * 1.0) / (((weight.Height * 0.01) * weight.Height) * 0.01);
253-
} else
254-
{
255-
weight.Bmi = weight.BodyWeight / (weight.Height * weight.Height) * 703.0;
256-
}
257-
}
258257
}
259258
}

0 commit comments

Comments
 (0)