Exemple #1
0
        private IReadOnlyList <HabitStreak> CalculateStreaks(Habit habit)
        {
            var last  = habit.Created.Date;
            var dates = habit.Entries
                        .Select(z => z.Created.Date)
                        .Distinct()
                        .OrderBy(z => z)
                        .ToImmutableArray();

            var result = ImmutableArray <HabitStreak> .Empty;

            foreach (var date in dates)
            {
                var days = (int)(date - last).TotalDays;
                if (days > 1)
                {
                    result = result.Add(new HabitStreak(last, date, days));
                }

                last = date;
            }

            var today = DateTime.Today;

            if (last != today)
            {
                var days = (int)(today - last).TotalDays;
                result = result.Add(new HabitStreak(last, today, days));
            }

            return(result);
        }
Exemple #2
0
        public async Task <Habit> Update(Habit habit)
        {
            var entity = await _db.Habits.Where(z => z.Id == habit.Id).SingleOrDefaultAsync();

            entity.Description = habit.Description;
            entity.Name        = habit.Name;
            await _db.SaveChangesAsync();

            return(entity.ToDomain());
        }
Exemple #3
0
 public async Task <Habit> SaveHabit(Habit habit)
 {
     if (!await _habits.Exists(habit.Id))
     {
         return(await _habits.Insert(habit));
     }
     else
     {
         return(await _habits.Update(habit));
     }
 }
Exemple #4
0
        public async Task <Habit> Insert(Habit habit)
        {
            var entity = new Infrastructure.Data.Context.Habit
            {
                Id          = habit.Id,
                Created     = habit.Created,
                Description = habit.Description,
                Name        = habit.Name,
                UserId      = habit.UserId
            };
            await _db.Habits.AddAsync(entity);

            await _db.SaveChangesAsync();

            return(entity.ToDomain());
        }