Beispiel #1
0
        //public async Task<HttpResponseMessage> PostMeal__Nutritional_Value([FromBody]ValuesViewModel[] date, [FromBody] int id)
        public async Task <JsonResult> PostMeal__Nutritional_Value([FromBody] MealViewModel data)
        {
            List <KeyValuePair <string, string> > result_message = new List <KeyValuePair <string, string> >();

            if (!ModelState.IsValid)
            {
                result_message.Add(new KeyValuePair <string, string>("message", "Wrong model"));
                return(Json(result_message));
            }
            var UserId = User.getUserId();

            if (await _context.CheckAccessToDiet(UserId, data.Meal.Id) == false)
            {
                result_message.Add(new KeyValuePair <string, string>("message", "Can't access to data"));
                return(Json(result_message));
            }
            try
            {
                bool result = await _context.Check__Modify_Save(data, UserId);

                await _context.Upadate_Meal(data.Meal.Id, _context.Calculate_makro(data));

                await _context.SaveChangesAsync();
            }
            catch (Exception e)
            {
                result_message.Add(new KeyValuePair <string, string>("message", e.Message));
                return(Json(result_message));
            }

            //_context.Meal__Nutritional_Values.Add(meal__Nutritional_Value);
            //await _context.SaveChangesAsync();
            result_message.Add(new KeyValuePair <string, string>("message", "Saved"));
            return(Json(result_message));
        }