@@ -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