Beispiel #1
0
        public void CheckHabit2()
        {
            Guid     habit_id = Guid.NewGuid();
            Guid     user_id  = Guid.NewGuid();
            DateTime now      = DateTime.Now;

            Days[] dayoff = new Days[] { new Days("Mon"), new Days("Sun") };
            Habit  h      = new Habit(habit_id, "Nyanyi", dayoff, user_id, now);


            h.DoHabit(DateTime.Now.AddDays(0)); //Sun

            // h.DoHabit(DateTime.Now.AddDays(1)); //Mon
            h.DoHabit(DateTime.Now.AddDays(3)); //Wed
            h.DoHabit(DateTime.Now.AddDays(4)); //Thu

            h.DoHabit(DateTime.Now.AddDays(6)); //Sun
            h.DoHabit(DateTime.Now.AddDays(7)); //Sun

            HabitLogMemento hm = (HabitLogMemento)h.HabitLog.GetMemento();

            Assert.Equal(2, hm.longest_streak);
            Assert.Equal(2, hm.current_streak);
            Assert.Equal(5, hm.log_count);
        }
Beispiel #2
0
        // [Fact]
        public void GiveEpicComeback()
        {
            NpgsqlConnection _connection = new NpgsqlConnection(connString);

            _connection.Open();

            IHabitRepository repo = new PostgresHabitRepository(_connection, null);

            Guid id     = new Guid("41684bad-554b-4194-8fc2-d192273b9aa8");
            Guid userID = new Guid("e28c8034-f90d-48aa-8e55-2aad1282f3bd");

            Habit    habit = repo.FindHabitByID(id, userID);
            DateTime now   = DateTime.Now;

            habit.DoHabit(now);
            repo.DoHabit(habit, now);

            for (int i = 10; i < 20; i++)
            {
                now = DateTime.Now.AddDays(i);
                habit.DoHabit(now);
                repo.DoHabit(habit, now);
            }

            Habit habit2 = repo.FindHabitByID(id, userID);

            _connection.Close();
            HabitLogMemento hm = (HabitLogMemento)habit2.HabitLog.GetMemento();

            // Habit habit2 = repo.FindHabitByID(id, userID);
            // Badge badge = bRepo.FindByUserID(userID);
            // Assert.Equal("Dominating", badge.Name);

            _connection.Close();
        }
Beispiel #3
0
        public ActionResult <List <HabitAPI> > All(Guid userID)
        {
            NpgsqlConnection _connection = new NpgsqlConnection(connString);

            _connection.Open();

            IHabitRepository repo = new PostgresHabitRepository(_connection, null);
            List <Habit>     list = repo.FindByUserID(userID);

            _connection.Close();
            List <HabitAPI> listHabit = new List <HabitAPI>();


            for (int i = 0; i < list.Count; i++)
            {
                HabitLogMemento hm       = (HabitLogMemento)list[i].HabitLog.GetMemento();
                HabitAPI        habitAPI = new HabitAPI
                {
                    ID            = list[i].ID,
                    Name          = list[i].Name,
                    DaysOff       = changeToString(list[i].DaysOff),
                    LogCount      = hm.log_count,
                    CurrentStreak = hm.current_streak,
                    LongestStreak = hm.longest_streak,
                    Logs          = hm.logs,
                    UserID        = list[i].UserID,
                    CreatedAt     = list[i].CreatedAt
                };
                listHabit.Add(habitAPI);
            }
            return(listHabit);
        }
Beispiel #4
0
        public ActionResult <HabitAPI> Get(Guid userID, Guid id)
        {
            NpgsqlConnection _connection = new NpgsqlConnection(connString);

            _connection.Open();

            IHabitRepository repo = new PostgresHabitRepository(_connection, null);
            Habit            h    = repo.FindHabitByID(id, userID);
            HabitLogMemento  hm   = (HabitLogMemento)h.HabitLog.GetMemento();

            _connection.Close();

            return(new HabitAPI
            {
                ID = h.ID,
                Name = h.Name,
                DaysOff = changeToString(h.DaysOff),
                LogCount = hm.log_count,
                CurrentStreak = hm.current_streak,
                LongestStreak = hm.longest_streak,
                Logs = hm.logs,
                UserID = h.UserID,
                CreatedAt = h.CreatedAt
            });
        }
Beispiel #5
0
        public ActionResult <HabitAPI> AddNewHabit(Guid userID, [FromBody] RequestData data)
        {
            NpgsqlConnection _connection = new NpgsqlConnection(connString);

            _connection.Open();

            IHabitRepository repo = new PostgresHabitRepository(_connection, null);

            Guid     habit_id = Guid.NewGuid();
            DateTime now      = DateTime.Now;


            Habit habit = new Habit(habit_id, data.Name, changeToDays(data.DaysOff), userID, now);

            repo.Create(habit);

            Habit           habit3 = repo.FindHabitByID(habit_id, userID);
            HabitLogMemento hm     = (HabitLogMemento)habit3.HabitLog.GetMemento();

            _connection.Close();


            return(new HabitAPI
            {
                ID = habit3.ID,
                Name = habit3.Name,
                DaysOff = changeToString(habit3.DaysOff),
                LogCount = hm.log_count,
                CurrentStreak = hm.current_streak,
                LongestStreak = hm.longest_streak,
                Logs = hm.logs,
                UserID = hm.user_id,
                CreatedAt = habit3.CreatedAt,
            });
        }
Beispiel #6
0
        public ActionResult <HabitAPI> UpdateHabit(Guid userID, Guid id, [FromBody] RequestData data)
        {
            NpgsqlConnection _connection = new NpgsqlConnection(connString);

            _connection.Open();
            IHabitRepository repo = new PostgresHabitRepository(_connection, null);

            Habit h  = repo.FindHabitByID(id, userID);
            Habit hn = new Habit(id, data.Name, changeToDays(data.DaysOff), userID, h.CreatedAt);

            repo.Update(h, hn.Name, hn.DaysOff);
            h = repo.FindHabitByID(id, userID);
            _connection.Close();

            HabitLogMemento hm = (HabitLogMemento)h.HabitLog.GetMemento();

            return(new HabitAPI
            {
                ID = hm.id,
                Name = h.Name,
                DaysOff = changeToString(h.DaysOff),
                LogCount = hm.log_count,
                CurrentStreak = hm.current_streak,
                LongestStreak = hm.longest_streak,
                Logs = hm.logs,
                UserID = hm.user_id,
                CreatedAt = h.CreatedAt
            });
        }
Beispiel #7
0
        public ActionResult <HabitAPI> Log(Guid userID, Guid id)
        {
            NpgsqlConnection _connection = new NpgsqlConnection(connString);

            _connection.Open();

            IHabitRepository repo  = new PostgresHabitRepository(_connection, null);
            Habit            habit = repo.FindHabitByID(id, userID);
            DateTime         now   = DateTime.Now;

            habit.DoHabit(now);
            repo.DoHabit(habit, now);


            Habit habit2 = repo.FindHabitByID(id, userID);

            _connection.Close();
            HabitLogMemento hm = (HabitLogMemento)habit2.HabitLog.GetMemento();

            return(new HabitAPI
            {
                ID = hm.id,
                Name = habit2.Name,
                DaysOff = changeToString(habit2.DaysOff),
                LogCount = hm.log_count,
                CurrentStreak = hm.current_streak,
                LongestStreak = hm.longest_streak,
                Logs = hm.logs,
                UserID = hm.user_id,
                CreatedAt = habit2.CreatedAt
            });
        }
Beispiel #8
0
        public void CheckHabit()
        {
            Guid     habit_id = Guid.NewGuid();
            Guid     user_id  = Guid.NewGuid();
            DateTime now      = DateTime.Now;

            Days[] dayoff = new Days[] { new Days("Mon"), new Days("Tue") };
            Habit  h      = new Habit(habit_id, "Nyanyi", dayoff, user_id, now);

            for (int i = 0; i < 2; i++)
            {
                h.DoHabit(DateTime.Now.AddDays(i));
            }

            for (int i = 4; i < 5; i++)
            {
                h.DoHabit(DateTime.Now.AddDays(i));
            }
            HabitLogMemento hm = (HabitLogMemento)h.HabitLog.GetMemento();

            Assert.Equal(2, hm.longest_streak);
            Assert.Equal(1, hm.current_streak);
            Assert.Equal(3, hm.log_count);
        }