示例#1
0
        public IHttpActionResult Put(JournalEntryEdit journal)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var service = CreateJournalService();

            if (!service.UpdateJournalEntry(journal))
            {
                return(InternalServerError());
            }

            return(Ok());
        }
示例#2
0
        public bool UpdateEntry(JournalEntryEdit model)
        {
            using (var ctx = new ApplicationDbContext())
            {
                var entity =
                    ctx
                    .JournalEntries
                    .Single(x => x.JournalEntryId == model.JournalEntryId && x.UserId == _userId);

                entity.Content         = model.Content;
                entity.PhotoUrl        = model.PhotoUrl;
                entity.PublicOrPrivate = model.PublicOrPrivate;
                entity.Tag             = model.Tag;

                return(ctx.SaveChanges() == 1);
            }
        }
示例#3
0
        public ActionResult Edit(int id)
        {
            var userId  = Guid.Parse(User.Identity.GetUserId());
            var service = new JournalEntryServices(userId);

            var detail = service.GetEntryById(id);

            var model =
                new JournalEntryEdit
            {
                JournalEntryId  = detail.JournalEntryId,
                Content         = detail.Content,
                PhotoUrl        = detail.PhotoUrl,
                PublicOrPrivate = detail.PublicOrPrivate,
                Tag             = detail.Tag
            };

            return(View(model));
        }
示例#4
0
        public ActionResult Edit(int id, JournalEntryEdit model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }
            if (model.JournalEntryId != id)
            {
                ModelState.AddModelError("", "Sorry, looks like we didn't find a Journal Entry with that Id.");
                return(View(model));
            }
            var userId  = Guid.Parse(User.Identity.GetUserId());
            var service = new JournalEntryServices(userId);

            if (service.UpdateEntry(model))
            {
                TempData["SaveResult"] = "Your journal entry was updated.";
                return(RedirectToAction("Index"));
            }

            ModelState.AddModelError("", "Your entry could not be updated.");
            return(View(model));
        }
示例#5
0
        public bool UpdateJournalEntry(JournalEntryEdit model)
        {
            using (var ctx = new ApplicationDbContext())
            {
                var entity =
                    ctx
                    .JournalEntries
                    .Single(e => e.JournalEntryId == model.JournalEntryId && e.UserId == _userId);

                entity.TimeStamp  = model.JournalDate;
                entity.MealList   = model.MealList;
                entity.FoodList   = model.FoodList;
                entity.RecipeList = model.RecipeList;

                double carbs    = 0;
                double protein  = 0;
                double fats     = 0;
                double calories = 0;

                if (model.FoodList != null)
                {
                    foreach (int i in model.FoodList)
                    {
                        using (var ctx2 = new ApplicationDbContext())
                        {
                            carbs    += ctx2.FoodItems.Find(i).Carbs;
                            protein  += ctx2.FoodItems.Find(i).Protein;
                            fats     += ctx2.FoodItems.Find(i).Fat;
                            calories += ctx2.FoodItems.Find(i).Calories;
                        }
                    }
                }

                if (model.MealList != null)
                {
                    foreach (int i in model.MealList)
                    {
                        using (var ctx2 = new ApplicationDbContext())
                        {
                            carbs    += ctx2.DailyMeals.Find(i).Carbs;
                            protein  += ctx2.DailyMeals.Find(i).Protein;
                            fats     += ctx2.DailyMeals.Find(i).Fat;
                            calories += ctx2.DailyMeals.Find(i).Calories;
                        }
                    }
                }

                if (model.RecipeList != null)
                {
                    foreach (int i in model.RecipeList)
                    {
                        using (var ctx2 = new ApplicationDbContext())
                        {
                            carbs    += ctx2.Recipes.Find(i).Carbs / ctx2.Recipes.Find(i).HowManyPortions;
                            protein  += ctx2.Recipes.Find(i).Protein / ctx2.Recipes.Find(i).HowManyPortions;
                            fats     += ctx2.Recipes.Find(i).Fat / ctx2.Recipes.Find(i).HowManyPortions;
                            calories += ctx2.Recipes.Find(i).Calories / ctx2.Recipes.Find(i).HowManyPortions;
                        }
                    }
                }

                entity.Carbs    = carbs;
                entity.Proteins = protein;
                entity.Fats     = fats;
                entity.Calories = calories;

                var dayEntity =
                    ctx
                    .Days
                    .Where(e => e.UserId.Equals(_userId))
                    .ToList()
                    .SingleOrDefault(e => e.DateOfEntry.Date == entity.TimeStamp.Date);

                //If there is no day object for the date of the journal entry, create a day and give the journal entries dayID the new DayID
                if (dayEntity == null)
                {
                    var newDayEntity =
                        new Day
                    {
                        DateOfEntry = model.JournalDate,
                        UserId      = _userId
                    };

                    //save new day
                    ctx.Days.Add(newDayEntity);
                    entity.DayId = newDayEntity.DayId;
                    return(ctx.SaveChanges() > 0);
                }
                //If the day exists, make the journal entrys dayId the existing day ID.
                entity.DayId = dayEntity.DayId;
                return(ctx.SaveChanges() > 0);
            }
        }