public ActionResult <Habit> AddNewHabit(Guid userID, [FromBody] RequestData data) { PostgreConnectRepo connect = new PostgreConnectRepo(); Habit hbt = new Habit(); string[] arr = { "Mon", "Wed", "Fri" }; if (data.DaysOff != null) { arr = data.DaysOff; } if (connect.UserRepo.FindById(userID) == null) { User us = new User(userID, "Budi"); connect.UserRepo.Create(us); } _Habit b = _Habit.NewHabit(Guid.NewGuid(), data.Name, arr, userID, DateTime.Now); connect.HabitRepo.Create(b); connect.Commit(); _Habit foo = connect.HabitRepo.FindByUserId(userID, b.ID); hbt.ID = foo.ID; hbt.Name = foo.Name; hbt.DaysOff = foo.Days_Off.Days; hbt.CurrentStreak = foo.Logs.Current_Streak; hbt.LongestStreak = foo.Logs.Longest_Streak; hbt.LogCount = foo.Logs.Log_Count; hbt.Logs = foo.Logs.Logs; hbt.UserID = foo.userID; hbt.CreatedAt = foo.Created_At; return(hbt); }
public void Create(_Habit habit) { string query = "INSERT INTO \"habit\" (id,name,user_id) VALUES (@id,@name,@user_id)"; using (var cmd = new NpgsqlCommand(query, _connection, _transaction)){ cmd.Parameters.AddWithValue("id", habit.ID); cmd.Parameters.AddWithValue("name", habit.Name); cmd.Parameters.AddWithValue("user_id", habit.userID); cmd.ExecuteNonQuery(); } string query2 = "INSERT INTO \"daysoff\" (id,days_off,habit_id) VALUES (@id,@days_off,@habit_id)"; using (var cmd2 = new NpgsqlCommand(query2, _connection, _transaction)){ cmd2.Parameters.AddWithValue("id", Guid.NewGuid()); cmd2.Parameters.AddWithValue("days_off", habit.Days_Off.Days); cmd2.Parameters.AddWithValue("habit_id", habit.ID); cmd2.ExecuteNonQuery(); } string query3 = "INSERT INTO \"log\" (id,logs,log_count,longest_streak,current_streak,habit_id) VALUES(@id,@logs,@log_count,@longest_streak,@current_streak,@habit_id)"; using (var cmd3 = new NpgsqlCommand(query3, _connection, _transaction)){ cmd3.Parameters.AddWithValue("id", Guid.NewGuid()); cmd3.Parameters.AddWithValue("logs", habit.Logs.Logs); cmd3.Parameters.AddWithValue("log_count", habit.Logs.Log_Count); cmd3.Parameters.AddWithValue("longest_streak", habit.Logs.Longest_Streak); cmd3.Parameters.AddWithValue("current_streak", habit.Logs.Current_Streak); cmd3.Parameters.AddWithValue("habit_id", habit.ID); cmd3.ExecuteNonQuery(); } }
public ActionResult <Habit> Get(Guid userID, Guid id) { PostgreConnectRepo connect = new PostgreConnectRepo(); Habit hbt = new Habit(); if (connect.UserRepo.FindById(userID) == null) { throw new Exception("Does not have that Habit with that userID"); } if (connect.HabitRepo.FindByUserId(userID, id) == null) { throw new Exception("Does not have that Habit with that userID"); } _Habit foo = connect.HabitRepo.FindByUserId(userID, id); hbt.ID = foo.ID; hbt.Name = foo.Name; hbt.DaysOff = foo.Days_Off.Days; hbt.CurrentStreak = foo.Logs.Current_Streak; hbt.LongestStreak = foo.Logs.Longest_Streak; hbt.LogCount = foo.Logs.Log_Count; hbt.Logs = foo.Logs.Logs; hbt.UserID = foo.userID; hbt.CreatedAt = foo.Created_At; return(hbt); }
public void UpdateLog(Guid id, _Habit habit) { string query = "UPDATE log SET logs = @logs, log_count = @log_count, longest_streak = @longest_streak, current_streak = @current_streak WHERE habit_id = @id"; using (var cmd = new NpgsqlCommand(query, _connection, _transaction)){ cmd.Parameters.AddWithValue("id", habit.ID); cmd.Parameters.AddWithValue("logs", habit.Logs.Logs); cmd.Parameters.AddWithValue("log_count", habit.Logs.Log_Count); cmd.Parameters.AddWithValue("longest_streak", habit.Logs.Longest_Streak); cmd.Parameters.AddWithValue("current_streak", habit.Logs.Current_Streak); cmd.ExecuteNonQuery(); } }
public void CheckHabit() { User lele = new User(Guid.NewGuid(), "mayboy"); string[] arr = { "Mon", "Tue" }; _Habit habit = new _Habit(Guid.NewGuid(), "lari", arr, lele.ID, DateTime.Now); Assert.Equal("lari", habit.Name); Assert.Equal(lele.ID, habit.userID); habit.doHabit("Mon", lele); Assert.Equal(1, habit.Logs.Current_Streak); Assert.Equal(1, habit.Logs.Log_Count); habit.doHabit("Mon", lele); habit.doHabit("Mon", lele); Assert.Equal(lele.ID, habit.userID); }
public ActionResult <Habit> Log(Guid userID, Guid id) { PostgreConnectRepo connect = new PostgreConnectRepo(); Habit hbt = new Habit(); User us; _Habit foo; if (connect.UserRepo.FindById(userID) == null) { throw new Exception("Does not have that Habit with that userID"); } else { us = connect.UserRepo.FindById(userID); } if (connect.HabitRepo.FindByUserId(userID, id) == null) { throw new Exception("Does not have that Habit with that userID"); } else { foo = connect.HabitRepo.FindByUserId(userID, id); foo.doHabit(foo.Days_Off.Days[0], us); foo.doHabit(foo.Days_Off.Days[0], us); foo.doHabit(foo.Days_Off.Days[0], us); foo.doHabit(foo.Days_Off.Days[0], us); foo.doHabit(foo.Days_Off.Days[0], us); connect.HabitRepo.UpdateLog(id, foo); connect.Commit(); _Habit boo = connect.HabitRepo.FindByUserId(userID, id); hbt.ID = boo.ID; hbt.Name = boo.Name; hbt.DaysOff = boo.Days_Off.Days; hbt.CurrentStreak = boo.Logs.Current_Streak; hbt.LongestStreak = boo.Logs.Longest_Streak; hbt.LogCount = boo.Logs.Log_Count; hbt.Logs = boo.Logs.Logs; hbt.UserID = boo.userID; hbt.CreatedAt = boo.Created_At; } return(hbt); }
public ActionResult <Habit> UpdateHabit(Guid userID, Guid id, [FromBody] RequestData data) { PostgreConnectRepo connect = new PostgreConnectRepo(); Habit hbt = new Habit(); if (connect.UserRepo.FindById(userID) == null) { throw new Exception("Does not have that Habit with that userID"); } if (connect.HabitRepo.FindByUserId(userID, id) == null) { throw new Exception("Does not have that Habit with that userID"); } if (data.DaysOff == null) { connect.HabitRepo.UpdateName(id, data.Name, userID); connect.Commit(); } else { connect.HabitRepo.UpdateNameAndDays(id, data.Name, data.DaysOff, userID); connect.Commit(); } _Habit foo = connect.HabitRepo.FindByUserId(userID, id); hbt.ID = foo.ID; hbt.Name = foo.Name; hbt.DaysOff = foo.Days_Off.Days; hbt.CurrentStreak = foo.Logs.Current_Streak; hbt.LongestStreak = foo.Logs.Longest_Streak; hbt.LogCount = foo.Logs.Log_Count; hbt.Logs = foo.Logs.Logs; hbt.UserID = foo.userID; hbt.CreatedAt = foo.Created_At; return(hbt); }
public void CreateHabit() { NpgsqlConnection _connection = new NpgsqlConnection(connString); _connection.Open(); IHabitRepository repo = new PostgreHabitRepo(_connection, null); IUserRepository repo2 = new PostgreUserRepo(_connection, null); User u = User.NewUser(Guid.NewGuid(), "mayboy"); repo2.Create(u); User u2 = repo2.FindById(u.ID); string[] arr = { "Mon", "Tue" }; _Habit b = _Habit.NewHabit(Guid.NewGuid(), "jogging", arr, u2.ID, DateTime.Now); repo.Create(b); _Habit b2 = repo.FindByUserId(u.ID, b.ID); Assert.NotNull(b2); Assert.Equal(b.ID, b2.ID); Assert.Equal(b.Name, b2.Name); Assert.Equal(b.Days_Off.Days, b2.Days_Off.Days); Assert.Equal(b.Logs.Log_Count, b2.Logs.Log_Count); Assert.Equal(b.Logs.Logs, b2.Logs.Logs); b2.doHabit("Mon", u2); b2.doHabit("Mon", u2); b2.doHabit("Mon", u2); b2.doHabit("Mon", u2); Assert.Equal("Dominating", u2._Badge[0].Name); _connection.Close(); }
public _Habit FindByUserId(Guid userId, Guid habit_id) { string query = "select name, created_at from \"habit\" where user_id = @user_id AND id = @habit_id"; string name = ""; string[] days_off; DateTime createdDate; DateTime[] logs; int log_count = 0; int longest_streak = 0; int current_streak = 0; using (var cmd = new NpgsqlCommand(query, _connection, _transaction)){ cmd.Parameters.AddWithValue("user_id", userId); cmd.Parameters.AddWithValue("habit_id", habit_id); using (NpgsqlDataReader reader = cmd.ExecuteReader()){ if (reader.Read()) { name = reader["name"] as string; createdDate = reader["created_at"] as DateTime? ?? throw new Exception("DateTime error"); } else { return(null); } } } string query2 = "select days_off from \"daysoff\" where habit_id = @habit_id"; using (var cmd2 = new NpgsqlCommand(query2, _connection, _transaction)){ cmd2.Parameters.AddWithValue("habit_id", habit_id); using (NpgsqlDataReader reader = cmd2.ExecuteReader()){ if (reader.Read()) { days_off = reader["days_off"] as string[]; } else { return(null); } } } string query3 = "select logs, log_count, longest_streak, current_streak from \"log\" where habit_id = @habit_id"; using (var cmd3 = new NpgsqlCommand(query3, _connection, _transaction)){ cmd3.Parameters.AddWithValue("habit_id", habit_id); using (NpgsqlDataReader reader = cmd3.ExecuteReader()){ if (reader.Read()) { logs = reader["logs"] as DateTime[]; log_count = reader["log_count"] as int? ?? throw new Exception("Empty log_count"); longest_streak = reader["longest_streak"] as int? ?? throw new Exception("Empty longest_streak"); current_streak = reader["current_streak"] as int? ?? throw new Exception("Empty current_streak"); } else { return(null); } } } _Habit hbt = new _Habit(habit_id, name, days_off, userId, createdDate); hbt.Logs.SetTheLog(logs, log_count, longest_streak, current_streak); return(hbt); }
public List <_Habit> GetAllHabit(Guid user_id) { string query = "select id,name,created_at from \"habit\" where user_id = @user_id"; _Habit temporary; int i = 0; List <_Habit> hab = new List <_Habit>(); List <Guid> id = new List <Guid>(); List <string> name = new List <string>(); List <DateTime> createdDate = new List <DateTime>(); List <string[]> days_off = new List <string[]>(); List <DateTime[]> logs = new List <DateTime[]>(); List <int> log_count = new List <int>(); List <int> longest_streak = new List <int>(); List <int> current_streak = new List <int>(); using (var cmd = new NpgsqlCommand(query, _connection, _transaction)){ cmd.Parameters.AddWithValue("user_id", user_id); using (NpgsqlDataReader reader = cmd.ExecuteReader()){ while (reader.Read()) { id.Add(reader["id"] as Guid? ?? throw new Exception("ID Error")); name.Add(reader["name"] as string); createdDate.Add(reader["created_at"] as DateTime? ?? throw new Exception("DateTime error")); } } } string query2 = "select days_off from \"daysoff\" where habit_id = @habit_id"; using (var cmd2 = new NpgsqlCommand(query2, _connection, _transaction)){ while (i < id.Count) { cmd2.Parameters.AddWithValue("habit_id", id[i]); using (NpgsqlDataReader reader = cmd2.ExecuteReader()){ while (reader.Read()) { days_off.Add(reader["days_off"] as string[]); } } i++; } } i = 0; string query3 = "select logs,log_count,longest_streak,current_streak from \"log\" where habit_id = @habit_id"; using (var cmd3 = new NpgsqlCommand(query3, _connection, _transaction)){ while (i < id.Count) { cmd3.Parameters.AddWithValue("habit_id", id[i]); using (NpgsqlDataReader reader = cmd3.ExecuteReader()){ while (reader.Read()) { logs.Add(reader["logs"] as DateTime[]); log_count.Add(reader["log_count"] as int? ?? throw new Exception("Empty log_count")); longest_streak.Add(reader["longest_streak"] as int? ?? throw new Exception("Empty longest_streak")); current_streak.Add(reader["current_streak"] as int? ?? throw new Exception("Empty current_streak")); } } i++; } } i = 0; for (int j = 0; j < id.Count; j++) { temporary = new _Habit(id[j], name[j], days_off[j], user_id, createdDate[j]); temporary.Logs.SetTheLog(logs[j], log_count[j], longest_streak[j], current_streak[j]); hab.Add(temporary); } return(hab); }