Ejemplo n.º 1
0
        public void InsertFindGetDeleteWorks()
        {
            var model = new FoodEntryModel
            {
                AddedDate    = DateTime.Now,
                Fat          = 10,
                Protein      = 30,
                Carbohydrate = 40,
                UserId       = "testId",
                KCal         = 244,
            };

            _fixture.Repo.Insert(model);

            var inserted = _fixture.Repo.Get(x => x.UserId == "testId").First();

            inserted.Should().NotBeNull();

            var objectById = _fixture.Repo.Find(inserted.Id.ToString());

            objectById.Should().NotBeNull();

            _fixture.Repo.Delete(inserted.Id.ToString());

            var deleted = _fixture.Repo.Get(x => x.UserId == "testId").FirstOrDefault();

            deleted.Should().BeNull();
        }
        public JsonResult Entry(FoodEntryModel model)
        {
            if (ModelState.IsValid)
            {
                using (var entities = new DietJournalEntities())
                {
                    FoodEntry foodEntry = null;

                    if (model.Id > 0)
                        foodEntry = entities.FoodEntries.FirstOrDefault(e => e.Id == model.Id);

                    if (foodEntry == null)
                    {
                        foodEntry = new FoodEntry();
                        entities.FoodEntries.AddObject(foodEntry);
                    }

                    foodEntry.UserId = CurrentUserId.Value;
                    foodEntry.Title = model.Title;
                    foodEntry.Description = model.Description;
                    foodEntry.Calories = model.Calories;
                    foodEntry.Carbs = model.Carbs;
                    foodEntry.EntryDate = model.ConsumedDate;
                    foodEntry.Fat = model.Fat;
                    foodEntry.Protein = model.Protein;
                    foodEntry.SavedDate = DateTime.Now;
                    foodEntry.MealType = !String.IsNullOrEmpty(model.FoodEntryType) ? int.Parse(model.FoodEntryType) : 0;

                    if (model.Favorite)
                    {
                        var favorite = new FoodFavorite
                        {
                            UserId = CurrentUserId,
                            MealType = foodEntry.MealType,
                            Title = foodEntry.Title,
                            Description = foodEntry.Description,
                            Calories = foodEntry.Calories,
                            Carbs = foodEntry.Carbs,
                            Protein = foodEntry.Protein,
                            Fat = foodEntry.Fat
                        };

                        entities.FoodFavorites.AddObject(favorite);
                    }

                    entities.SaveChanges();

                    if (model.EntryValues != null && model.EntryValues.Count > 0)
                    {
                        foreach (var entryValue in model.EntryValues)
                        {
                            FoodEntryMealTypeValue value = null;

                            if (!String.IsNullOrEmpty(entryValue.Id))
                            {
                                var entryValueId = int.Parse(entryValue.Id);
                                value = entities.FoodEntryMealTypeValues.FirstOrDefault(v => v.Id == entryValueId);
                            }

                            if (value == null)
                            {
                                value = new FoodEntryMealTypeValue
                                {
                                    MealTypeValueId = int.Parse(entryValue.EntryTypeValueId),
                                    FoodEntryId = foodEntry.Id
                                };
                                entities.FoodEntryMealTypeValues.AddObject(value);
                            }

                            value.Value = entryValue.Value;
                        }

                        entities.SaveChanges();
                    }
                }

                return Json(new { IsValid = true });
            }

            return Json(new { IsValid = false, ErrorMessage = "" });
        }
        private void SetFoodEntryProfileSettings(FoodEntryModel model)
        {
            var profileSettings = CurrentProfileSettings;

            model.CaptureCalories = profileSettings.CaptureCalories;
            model.CaptureCarbs = profileSettings.CaptureCarbs;
            model.CaptureFat = profileSettings.CaptureFat;
            model.CaptureProtein = profileSettings.CaptureProtein;
            model.AvailableFoodEntryTypes = GetAvailableFoodEntryTypes(profileSettings);
        }
        private FoodEntryModel CreateEntryModel(DateTime consumedDate)
        {
            var model = new FoodEntryModel
            {
                ConsumedDate = consumedDate,
            };

            SetFoodEntryProfileSettings(model);

            return model;
        }
        private FoodEntryModel CreateEntryModel(FoodEntry foodEntry)
        {
            var profileSettings = CurrentProfileSettings;

            var model = new FoodEntryModel
            {
                Id = foodEntry.Id,
                Title = foodEntry.Title,
                Description = foodEntry.Description,
                ConsumedDate = foodEntry.EntryDate,
                FoodEntryType = foodEntry.MealType.ToString(),
                Calories = foodEntry.Calories.HasValue ? foodEntry.Calories.Value : 0,
                Carbs = foodEntry.Carbs.HasValue ? foodEntry.Carbs.Value : 0,
                Fat = foodEntry.Fat.HasValue ? foodEntry.Fat.Value : 0,
                Protein = foodEntry.Protein.HasValue ? foodEntry.Protein.Value : 0
            };

            SetFoodEntryProfileSettings(model);

            return model;
        }
        protected ActionResult EntryView(FoodEntryModel model)
        {
            ViewBag.Back = model.Id > 0;

            return View("Entry", model);
        }
        public ActionResult SelectFavorite(FoodFavoriteSelectionModel model)
        {
            var entryModel = new FoodEntryModel
            {
                ConsumedDate = model.ConsumedDate
            };

            if (ModelState.IsValid && !String.IsNullOrEmpty(model.Selection))
            {
                int favoriteId = int.Parse(model.Selection);

                using (var context = new DietJournalEntities())
                {
                    var favorite = context.FoodFavorites.FirstOrDefault(f => f.Id == favoriteId);
                    if (favorite != null)
                    {
                        entryModel.FoodEntryType = favorite.MealType.ToString();
                        entryModel.Title = favorite.Title;
                        entryModel.Description = favorite.Description;
                        entryModel.Calories = favorite.Calories.HasValue ? favorite.Calories.Value : 0;
                        entryModel.Protein = favorite.Protein.HasValue ? favorite.Protein.Value : 0;
                        entryModel.Carbs = favorite.Carbs.HasValue ? favorite.Carbs.Value : 0;
                        entryModel.Fat = favorite.Fat.HasValue ? favorite.Fat.Value : 0;
                        entryModel.Favorite = true;
                    }
                }
            }

            SetFoodEntryProfileSettings(entryModel);

            return EntryView(entryModel);
        }