public static void LogBeforeSave(Habit habit, TypeUpdate typeUpdate, HabitField field, object newValue, string logDescription, MSSQLLocalDBEntities context) { var habitLog = new HabitLog { ChangeDescription = logDescription, DoneDate = DateTime.Now, HabitId = habit.Id }; switch (field) { case HabitField.Name: habitLog.OldValue = habit.Name; break; case HabitField.DoneDate: habitLog.OldValue = habit.DoneDate?.ToString(); break; case HabitField.NewRecord: break; case HabitField.DeleteRecord: break; default: habitLog.OldValue = ""; break; } habitLog.NewValue = newValue.ToString(); context.HabitLogs.Add(habitLog); context.SaveChanges(); }
protected void Page_Load(object sender, EventArgs e) { Context1 = new MSSQLLocalDBEntities(); var id = Request.QueryString["Id"]; var action = Request.QueryString["action"]; if (!string.IsNullOrEmpty(id)) { var habit = Context1.Habits.First(p => p.Id.ToString() == id); if (action == "done") { habit.Done = true; habit.DoneDate = DateTime.Now; } else { habit.Done = false; } Context1.SaveChanges(); } if (!IsPostBack) { Habits1 = Context1.Habits.ToList <Habit>().OrderBy(h => h.HabitTime).ToList(); } }
protected void SaveDescription_Click(object sender, EventArgs e) { LogManager.LogBeforeSave(Habit1, LogManager.TypeUpdate.DoneUpdate, LogManager.HabitField.DoneDate, (Habit1.DoneDate == null ? DateTime.MinValue : Habit1.DoneDate), logDescription.Text, _context); Habit1.Done = true; Habit1.DoneDate = DateTime.Now; Habit1.HabitLogCount = Habit1.HabitLogCount + 1; _context.SaveChanges(); Response.Redirect("Default.aspx"); }
protected void saveHabit_Click(object sender, EventArgs e) { var newHabit = new Habit { Name = habitName.Text, Image = habitImage.Text, Description = description.Text, HabitTime = (int.Parse(habitTime.Text.Split(':')[0])) * 60 + (int.Parse(habitTime.Text.Split(':')[1])), HabitLogCount = 0 }; _context1.Habits.Add(newHabit); _context1.SaveChanges(); Response.Redirect("HabitShow.aspx?Id=" + newHabit.Id); }