public static void DeleteStepGoal(int id) { ActivityTracker.Track("Deleted a Step Goal", (int)UserActionEnum.Deleted); // Get the user id thats currently logged in string userId = HttpContext.Current.Session["UserId"].ToString(); var user = db.HPSUsers.Select(u => u) .Where(uid => uid.UserId == userId) .SingleOrDefault(); try { // Get the distance goal to be deleted StepGoal stepGoal = db.StepGoals.Find(id); db.StepGoals.Remove(stepGoal); // Save changes db.SaveChanges(); // Set the notification notificationMessage = "Step Goal was successfully deleted."; notificationStyle = "text-success"; notification = true; } catch (DataException dx) { // Set the notification notificationMessage = "Step Goal could not be deleted at this time. Please try again later or inform an Administrator."; notificationStyle = "text-danger"; notification = true; // Write error to log file Log File Writer LogFile.WriteToFile("FitBitManager.aspx.cs", "DeleteStepGoal", dx, user.AspNetUser.UserName + " tried to delete a Step Goal.", "HPSErrorLog.txt"); } catch (Exception ex) { // Set the notification notificationMessage = "Step Goal could not be deleted at this time. Please try again later or inform an Administrator."; notificationStyle = "text-danger"; notification = true; // Write error to log file Log File Writer LogFile.WriteToFile("FitBitManager.aspx.cs", "DeleteStepGoal", ex, user.AspNetUser.UserName + " tried to delete a Step Goal.", "HPSErrorLog.txt"); } }
protected void btnSetNewGoal_Click(object sender, EventArgs e) { ActivityTracker.Track("Created a New Goal", (int)UserActionEnum.Created); try { if (ddlGoalType.SelectedValue == "Steps") { StepGoal sg = new StepGoal(); sg.GoalSteps = Convert.ToInt32(txtGoal.Text); sg.GoalStartDate = Convert.ToDateTime(txtGoalStartDate.Text) + new TimeSpan(0, 0, 0); sg.GoalEndDate = Convert.ToDateTime(txtGoalEndDate.Text) + new TimeSpan(0, 0, 0); sg.UserId = Session["UserId"].ToString(); db.StepGoals.Add(sg); db.SaveChanges(); // Display message lblCRUDMessage.Text = "New Steps Goal saved."; lblCRUDMessage.CssClass = "text-success"; } else if (ddlGoalType.SelectedValue == "Distance") { DistanceGoal dg = new DistanceGoal(); dg.GoalDistance = Convert.ToDecimal(txtGoal.Text); dg.GoalStartDate = Convert.ToDateTime(txtGoalStartDate.Text) + new TimeSpan(0, 0, 0); dg.GoalEndDate = Convert.ToDateTime(txtGoalEndDate.Text) + new TimeSpan(0, 0, 0); dg.UserId = Session["UserId"].ToString(); db.DistanceGoals.Add(dg); db.SaveChanges(); // Display message lblCRUDMessage.Text = "New Distance Goal saved."; lblCRUDMessage.CssClass = "text-success"; } else if (ddlGoalType.SelectedValue == "Minutes") { MinuteGoal mg = new MinuteGoal(); mg.GoalMinute = Convert.ToDecimal(txtGoal.Text); mg.GoalStartDate = Convert.ToDateTime(txtGoalStartDate.Text) + new TimeSpan(0, 0, 0); mg.GoalEndDate = Convert.ToDateTime(txtGoalEndDate.Text) + new TimeSpan(0, 0, 0); mg.UserId = Session["UserId"].ToString(); db.MinuteGoals.Add(mg); db.SaveChanges(); // Display message lblCRUDMessage.Text = "New Minutes Goal saved."; lblCRUDMessage.CssClass = "text-success"; } // Reset copntrols txtGoal.Text = ""; txtGoalEndDate.Text = ""; txtGoalStartDate.Text = ""; ddlGoalType.SelectedValue = "-1"; } catch (DataException dx) { lblCRUDMessage.Text = "Goal could not be saved at this time. Please try again later or inform an Administrator."; lblCRUDMessage.CssClass = "text-danger"; LogFile.WriteToFile("FitBitManager.aspx.cs", "btnSetNewGoal_Click", dx, User.Identity.Name + " tried to set a new goal.", "HPSErrorLog.txt"); } catch (Exception ex) { lblCRUDMessage.Text = "Goal could not be saved at this time. Please try again later or inform an Administrator."; lblCRUDMessage.CssClass = "text-danger"; LogFile.WriteToFile("FitBitManager.aspx.cs", "btnSetNewGoal_Click", ex, User.Identity.Name + " tried to set a new goal.", "HPSErrorLog.txt"); } // Rebuild the chart and step goals DrawChart(this.Page, 7, "Steps"); GetStepGoals(); GetDistanceGoals(); GetMinuteGoals(); // Build tables for viewing all goals string userId = Session["UserId"].ToString(); TableBuilder.BuildStepGoalsTable(tblStepGoals, userId); TableBuilder.BuildDistanceGoalsTable(tblDistanceGoals, userId); TableBuilder.BuildMinuteGoalsTable(tblMinuteGoals, userId); }