public async Task <ActionResult> CreateBmi([FromForm] BmiRequestModel body) { try { if (body.Heigth == null) { throw new NullReferenceException("heigth cannot be null"); } if (body.Weigth == null) { throw new NullReferenceException("weigth cannot be null"); } string name = @User.Claims.FirstOrDefault(claim => claim.Type.Equals(new UserModel().Name))?.Value; string id = @User.Claims.FirstOrDefault(claim => claim.Type.Equals(new UserModel().Id))?.Value; #region Be sure we always have decimal seperator for culture. double weigth = GetDouble(body.Weigth); double heigth = GetDouble(body.Heigth); if (!heigth.ToString(CultureInfo.InvariantCulture).Contains(".")) { return(BadRequest("Du har ikke angivet en rigtig højde. Det skal være et decimaltal - ex: 0.00")); } double bmi = weigth / (heigth * heigth); #endregion if (Math.Abs(heigth) < 1) { return(BadRequest("Du skal angive en værdi højere end 0")); } if (Math.Abs(weigth) < 1) { return(BadRequest("Du skal angive en værdi højere end 0")); } BmiResponseModel result = await new BmiHandler(_dbConn, id, name).CreateBmi(weigth, heigth, bmi); if (result.Text.Equals("Kunne ikke beregne - mangler du et decimal?")) { return(BadRequest("Kunne ikke beregne - mangler du et decimal? - Tjek om højden indeholder et decimaltal - ex: 0.00")); } return(Ok(result)); } catch (Exception ex) { Console.WriteLine(ex); return(BadRequest("Der skete desværre en fejl. Prøv igen.")); } }
public async Task <BmiResponseModel> CreateBmi(double weigth, double heigth, double bmi) { try { if (string.IsNullOrEmpty(_dbConnectionString)) { throw new NullReferenceException("No db location is provided"); } if (string.IsNullOrEmpty(_userName)) { throw new NullReferenceException("No username is provided"); } if (string.IsNullOrEmpty(_id)) { throw new NullReferenceException("No id is provided"); } BmiResponseModel r = await Task.FromResult(new BmiResponseModel { Bmi = bmi, Text = BmiConverter(bmi), }); if (r == null) { throw new NullReferenceException("Result is null"); } if (r.Text.Equals("Kunne ikke beregne - mangler du et decimal?")) { return(r); } //Create in db try { SqlConnection sqlConnection = new SqlConnection(_dbConnectionString); SqlCommand cmd; string sql = $"INSERT INTO [dbo].[BmiResults]([Weigth],[Heigth],[Id],[UserName],[CreatedOn],[Bmi],[BmiText]) VALUES (@weigth, @heigth, @id, @userName, @createdOn, @bmi, @bmiText)"; cmd = new SqlCommand(sql, sqlConnection); cmd.Parameters.Add(new SqlParameter { ParameterName = "@weigth", Value = weigth }); cmd.Parameters.Add(new SqlParameter { ParameterName = "@heigth", Value = heigth }); cmd.Parameters.Add(new SqlParameter { ParameterName = "@id", Value = _id }); cmd.Parameters.Add(new SqlParameter { ParameterName = "@userName", Value = _userName }); cmd.Parameters.Add(new SqlParameter { ParameterName = "@createdOn", Value = DateTime.Now }); cmd.Parameters.Add(new SqlParameter { ParameterName = "@bmi", Value = r.Bmi }); cmd.Parameters.Add(new SqlParameter { ParameterName = "@bmiText", Value = r.Text }); if (sqlConnection.State != ConnectionState.Open) { sqlConnection.Open(); } await cmd.ExecuteNonQueryAsync(); sqlConnection.Close(); } catch (Exception e) { Console.WriteLine(e); throw; } return(r); } catch (Exception e) { Console.WriteLine(e); throw; } }