Exemple #1
0
        public async Task <ResultDto> UpdateTrackedPeriod(NewTrackedPeriodDto trackedPeriodDto, string userId)
        {
            var tp = await _context.TrackedPeriods.Where(tr => tr.UUID == trackedPeriodDto.UUID).FirstOrDefaultAsync();

            if (tp == null)
            {
                _logger.LogDebug("SharedMeal not found with UUID: ", trackedPeriodDto.UUID);
                return(new ResultDto(false, "Tracked Period not found"));
            }

            var days = new List <Day>();

            trackedPeriodDto.DayIds.ForEach(d => days.Add(
                                                _context.Days.Where(day => day.UUID == d && day.User.UUID == userId)
                                                .Include(day => day.Meals)
                                                .ThenInclude(m => m.MealFoods)
                                                .ThenInclude(mf => mf.Food)
                                                .FirstOrDefault()
                                                )
                                            );

            if (days == null || days.Count == 0)
            {
                return(new ResultDto(false, "No days found with given information"));
            }

            _context.TrackedPeriodDays.RemoveRange(_context.TrackedPeriodDays.Where(tpd => tpd.TrackedPeriodId == tp.Id));
            tp           = CalculateTotals(tp, days);
            tp.Name      = trackedPeriodDto.Name;
            tp.UpdatedAt = DateTime.Now;

            days.ForEach(d =>
            {
                var tpd = new TrackedPeriodDay
                {
                    Day           = d,
                    TrackedPeriod = tp
                };
                _context.Add(tpd);
            });

            _context.Entry(tp).State = EntityState.Modified;
            await _context.SaveChangesAsync();

            return(new ResultDto(true, "Tracked period updated succesfully"));
        }
Exemple #2
0
        public async Task <ResultDto> AddNewTrackedPeriod(NewTrackedPeriodDto newTrackedPeriodDto, string userId)
        {
            var days = new List <Day>();

            newTrackedPeriodDto.DayIds.ForEach(d => days.Add(
                                                   _context.Days.Where(day => day.UUID == d && day.User.UUID == userId)
                                                   .Include(day => day.Meals)
                                                   .ThenInclude(m => m.MealFoods)
                                                   .ThenInclude(mf => mf.Food)
                                                   .FirstOrDefault()
                                                   )
                                               );

            if (days == null || days.Count == 0)
            {
                return(new ResultDto(false, "No days found with given information"));
            }

            var tp = new TrackedPeriod
            {
                UUID      = Guid.NewGuid().ToString(),
                Name      = newTrackedPeriodDto.Name,
                CreatedAt = DateTime.Now,
                UpdatedAt = DateTime.Now,
                User      = await _context.Users.Where(u => u.UUID == userId).FirstOrDefaultAsync(),
            };

            tp = CalculateTotals(tp, days);

            _context.Add(tp);

            days.ForEach(d =>
            {
                var tpd = new TrackedPeriodDay
                {
                    Day           = d,
                    TrackedPeriod = tp
                };
                _context.Add(tpd);
            });

            await _context.SaveChangesAsync();

            return(new ResultDto(true, "New period saved succesfully"));
        }