Exemple #1
0
        /// <summary>
        /// Get Food by id
        /// </summary>
        /// <param name="id">Food id</param>
        /// <returns>Food json view model</returns>
        public IHttpActionResult Get(int id)
        {
            try
            {
                // get
                log.Debug("_foodService.GetFood - foodId: " + id + " ");

                var food = new FoodViewModel(_foodService.GetFood(id));

                log.Debug("_foodService.GetFood - " + FoodViewModel.FormatFoodViewModel(food));

                log.Debug("result: 'success'");

                //return Json(food, JsonRequestBehavior.AllowGet);
                //return Content(JsonConvert.SerializeObject(food), "application/json");
                //return food;
                //return JsonConvert.SerializeObject(food);
                return(Ok(food));
            }
            catch (Exception e)
            {
                // error
                log.Error(e.ToString());

                throw;
            }
        }
Exemple #2
0
        private FoodDTO Create(FoodViewModel viewModel)
        {
            try
            {
                log.Debug(FoodViewModel.FormatFoodViewModel(viewModel));

                FoodDTO food = new FoodDTO();

                // copy values
                viewModel.UpdateDTO(food, null); //RequestContext.Principal.Identity.GetUserId());

                // audit
                food.CreateBy = null; //RequestContext.Principal.Identity.GetUserId();
                food.CreateOn = DateTime.UtcNow;

                // add
                log.Debug("_foodService.AddFood - " + FoodDTO.FormatFoodDTO(food));

                int id = _foodService.AddFood(food);

                food.FoodId = id;

                log.Debug("result: 'success', id: " + id);

                return(food);
            }
            catch (Exception e)
            {
                // error
                log.Error(e.ToString());

                throw;
            }
        }
Exemple #3
0
        private FoodDTO Update(FoodViewModel viewModel)
        {
            try
            {
                log.Debug(FoodViewModel.FormatFoodViewModel(viewModel));

                // get
                log.Debug("_foodService.GetFood - foodId: " + viewModel.FoodId + " ");

                var existingFood = _foodService.GetFood(viewModel.FoodId);

                log.Debug("_foodService.GetFood - " + FoodDTO.FormatFoodDTO(existingFood));

                if (existingFood != null)
                {
                    // copy values
                    viewModel.UpdateDTO(existingFood, null); //RequestContext.Principal.Identity.GetUserId());

                    // update
                    log.Debug("_foodService.UpdateFood - " + FoodDTO.FormatFoodDTO(existingFood));

                    _foodService.UpdateFood(existingFood);

                    log.Debug("result: 'success'");
                }
                else
                {
                    log.Error("existingFood: null, FoodId: " + viewModel.FoodId);
                }

                return(existingFood);
            }
            catch (Exception e)
            {
                // error
                log.Error(e.ToString());

                throw;
            }
        }
Exemple #4
0
        //[ValidateAntiForgeryToken]
        /// <summary>
        /// Save a list of Food
        /// </summary>
        /// <param name="viewModels">Food view models</param>
        /// <param name="id">(not used)</param>
        /// <returns>true if the operation is successfull</returns>
        public IHttpActionResult SaveList(FoodViewModel[] viewModels, int?id)
        {
            try
            {
                log.Debug("SaveList");

                if (viewModels != null)
                {
                    // save list
                    foreach (FoodViewModel viewModel in viewModels)
                    {
                        log.Debug(FoodViewModel.FormatFoodViewModel(viewModel));

                        if (viewModel.FoodId > 0)
                        {
                            var t = Update(viewModel);
                        }
                        else
                        {
                            var t = Create(viewModel);
                        }
                    }
                }
                else
                {
                    log.Error("viewModels: null");
                }

                //return Json(true);
                //return JsonConvert.SerializeObject(true);
                return(Ok(true));
            }
            catch (Exception e)
            {
                // error
                log.Error(e.ToString());

                throw;
            }
        }