public bool AddDailyWeight(DailyWeight newWeight) { try { var weight = _ctx.DailyWeights.SingleOrDefault( d => d.UserName == newWeight.UserName && d.WeightDate == newWeight.WeightDate); if (weight == null) { _ctx.DailyWeights.Add(newWeight); } else { newWeight.DailyWeightId = weight.DailyWeightId; newWeight.BodyFat = weight.BodyFat; weight.Weight = newWeight.Weight; } return true; } catch (Exception ex) { //TODO add logging return false; } }
public HttpResponseMessage Post([FromBody] DailyWeight newWeight) { if (User.Identity.IsAuthenticated) { if (newWeight.WeightDate == default(DateTime)) { newWeight.WeightDate = DateTime.Today; } newWeight.UserName = User.Identity.Name; if (_repository.AddDailyWeight(newWeight) && _repository.Save()) { return(Request.CreateResponse(HttpStatusCode.Created, newWeight)); } return(Request.CreateResponse(HttpStatusCode.BadRequest)); } return(Request.CreateResponse(HttpStatusCode.Unauthorized)); }