Ejemplo n.º 1
0
        public async Task <ActionResult <IEnumerable <DayTotal> > > GetDayTotal(int date)
        {
            var dateTime = DateTime.UtcNow.Date;

            date += 1;

            for (int index = 0; index <= 6; index++)
            {
                var temp = await _context.DayTotal.FirstOrDefaultAsync(d =>
                                                                       d.Date == dateTime.AddDays(date - index).AddHours(-8).ToString("dd/MM/yyyy"));


                //Trace.WriteLine("Hello Worsld!", index.ToString());

                if (temp == null)
                {
                    var temp2 = new DayTotal {
                    };
                    temp2.TotalCarbs   = 0;
                    temp2.TotalFats    = 0;
                    temp2.TotalProtein = 0;

                    temp2.Date     = dateTime.AddDays((date) - index).AddHours(-8).ToString("dd/MM/yyyy");
                    temp2.RealDate = dateTime.AddDays(date - index).AddHours(-8);

                    _context.DayTotal.Add(temp2);
                    await _context.SaveChangesAsync();
                }
            }

            var dbSet = await _context.DayTotal.Where(d =>
                                                      d.RealDate == dateTime.AddDays(date - 6).AddHours(-8) ||
                                                      d.RealDate == dateTime.AddDays(date - 5).AddHours(-8) ||
                                                      d.RealDate == dateTime.AddDays(date - 4).AddHours(-8) ||
                                                      d.RealDate == dateTime.AddDays(date - 3).AddHours(-8) ||
                                                      d.RealDate == dateTime.AddDays(date - 2).AddHours(-8) ||
                                                      d.RealDate == dateTime.AddDays(date - 1).AddHours(-8) ||
                                                      d.RealDate == dateTime.AddDays(date).AddHours(-8))
                        .OrderByDescending(d => d.RealDate)
                        .ToListAsync();

            for (int index = 0; index <= 6; index++)
            {
                var temp = await _context.DayTotal.FirstOrDefaultAsync(d =>
                                                                       d.Date == dateTime.AddDays(date - index).AddHours(-8).ToString("dd/MM/yyyy"));

                if (temp.TotalCarbs + temp.TotalFats + temp.TotalProtein == 0)
                {
                    _context.DayTotal.Remove(temp);
                    await _context.SaveChangesAsync();
                }
            }

            return(dbSet);
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> PutFood(int id, Food food)
        {
            if (id != food.FoodID)
            {
                return(BadRequest());
            }

            var f00d = await _context.Food.AsNoTracking().FirstOrDefaultAsync(f => f.FoodID == id);

            var dayTotal = await _context.DayTotal.FirstOrDefaultAsync(d => d.Date == food.Date);

            //If the entry is empty and we didnt give a blank food
            if (dayTotal == null &&
                food.Carbs + food.Fats + food.Protein != 0)
            {
                DayTotal.TotalCarbs   = food.Carbs * food.Servings;
                DayTotal.TotalFats    = food.Fats * food.Servings;
                DayTotal.TotalProtein = food.Protein * food.Servings;
                DayTotal.Date         = food.Date;
                DayTotal.RealDate     = food.RealDate;
                _context.DayTotal.Add(DayTotal);
            }
            //if the entry exists and we didnt give it a blank food
            if (dayTotal != null)
            {
                dayTotal.TotalCarbs   += (food.Carbs * food.Servings) - (f00d.Carbs * f00d.Servings);
                dayTotal.TotalFats    += (food.Fats * food.Servings) - (f00d.Fats * f00d.Servings);
                dayTotal.TotalProtein += (food.Protein * food.Servings) - (f00d.Protein * f00d.Servings);
                dayTotal.Date          = food.Date;

                if (dayTotal.TotalCarbs +
                    dayTotal.TotalFats +
                    dayTotal.TotalProtein == 0)
                {
                    _context.DayTotal.Remove(dayTotal);
                }
                else
                {
                    _context.Attach(dayTotal).State = EntityState.Modified;
                }
            }
            if (dayTotal != null &&
                food.Carbs + food.Fats + food.Protein == 0)
            {
                _context.DayTotal.Remove(dayTotal);
            }

            _context.Entry(food).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!FoodExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }