Ejemplo n.º 1
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
            });
        }
Ejemplo n.º 2
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,
            });
        }
Ejemplo n.º 3
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
            });
        }