public ActionResult Edit([Bind(Include = "Id,Tutor,Student,DayName,TimeOfDay")] TutorScheduleViewModel tutorSchedule) { if (ModelState.IsValid) { var modifiedSchedule = _db.TutorSchedules.Find(tutorSchedule.Id); if (modifiedSchedule != null) { using (var context = new SenecaContext()) { var sqlString = "SELECT Tutor_Id FROM TutorSchedule WHERE Id = " + tutorSchedule.Id; var tutorId = context.Database.SqlQuery <string>(sqlString).FirstOrDefault(); ApplicationUser tutor = _db.Users.Find(tutorId); modifiedSchedule.Tutor = tutor; sqlString = "SELECT Student_Id FROM TutorSchedule WHERE Id = " + tutorSchedule.Id; var studentId = context.Database.SqlQuery <int>(sqlString).FirstOrDefault(); Student student = _db.Students.Find(studentId); modifiedSchedule.Student = student; } modifiedSchedule.DayOfWeekIndex = GetDayOfWeekIndex(tutorSchedule.DayName); modifiedSchedule.MinutesPastMidnight = ConvertToMinutesPastMidnight(tutorSchedule.TimeOfDay); _db.Entry(modifiedSchedule).State = EntityState.Modified; _db.SaveChanges(); return(RedirectToAction("Index")); } } return(View(tutorSchedule)); }
public static Boolean UserIsInRole(string userId, string roleName) { var sqlString = "SELECT Id FROM AspNetRoles WHERE Name = '" + roleName + "'"; string roleId; using (var context = new SenecaContext()) { roleId = context.Database.SqlQuery <string>(sqlString).FirstOrDefault(); if (roleId == null) { return(false); } } sqlString = "SELECT UserId FROM AspNetUserRoles WHERE "; sqlString += "UserId = '" + userId + "' AND RoleId ='" + roleId + "'"; using (var context = new SenecaContext()) { var success = context.Database.SqlQuery <string>(sqlString).FirstOrDefault(); if (success != null) { return(true); } return(false); } }
public ActionResult Edit([Bind(Include = "Id,FirstName,LastName,DateOfBirth,Active,Location,ClientNote,NumberInHousehold")] HfedClient hfedClient) { if (ModelState.IsValid) { using (var context = new SenecaContext()) { var query = @"UPDATE HfedClient" + " SET FirstName=@FirstName,LastName=@LastName,DateOfBirth=@DateOfBirth," + "Active=@Active,ClientNote=@ClientNote,Location_Id=@Location_Id," + "NumberInHousehold=@NumberInHousehold" + " WHERE Id = " + hfedClient.Id; context.Database.ExecuteSqlCommand(query, new SqlParameter("@FirstName", hfedClient.FirstName), new SqlParameter("@LastName", hfedClient.LastName), new SqlParameter("@DateOfBirth", hfedClient.DateOfBirth.ToShortDateString()), new SqlParameter("@Active", hfedClient.Active ? 1 : 0), new SqlParameter("@ClientNote", hfedClient.ClientNote + ""), new SqlParameter("@Location_Id", hfedClient.Location.Id), new SqlParameter("@NumberInHousehold", hfedClient.NumberInHousehold)); } // This version not updating location change: TempData ["ClientLocationId"] = hfedClient.Location.Id; return(RedirectToAction("Index")); } return(View(hfedClient)); }
// GET: TutorSchedules public ActionResult Index() { var model = new List <TutorSchedule>(); foreach (var tutorSchedule in _db.TutorSchedules.ToList()) { using (var context = new SenecaContext()) { var sqlString = "SELECT Tutor_Id FROM TutorSchedule WHERE Id = " + tutorSchedule.Id; var tutorId = context.Database.SqlQuery <string>(sqlString).FirstOrDefault(); var tutor = _db.Users.Find(tutorId); sqlString = "SELECT Student_Id FROM TutorSchedule WHERE Id = " + tutorSchedule.Id; var studentId = context.Database.SqlQuery <int>(sqlString).FirstOrDefault(); var student = _db.Students.Find(studentId); TutorSchedule viewModel = new TutorSchedule() { Id = tutorSchedule.Id, Tutor = tutor, Student = student, DayName = GetDayOfWeekName(tutorSchedule.DayOfWeekIndex), TimeOfDay = ConvertToHhmm(tutorSchedule.MinutesPastMidnight) }; model.Add(viewModel); } } return(View(model)); }
public static async Task EmailHFEDScheduleChange(string userId, int scheduleId, int providerId, string recipientId) { var context = new SenecaContext(); var usr = context.Users.Find(userId); var recipient = context.Users.Find(recipientId); var schedDate = context.HfedSchedules.Where(i => i.Id == scheduleId).Select(i => i.Date).FirstOrDefault(); // returns provider = null: HfedSchedule sched = context.HfedSchedules.SingleOrDefault(i => i.Id == scheduleId); string providerName; using (context) { var sqlString = "SELECT Name FROM HfedProvider WHERE Id = " + providerId; providerName = context.Database.SqlQuery <string>(sqlString).FirstOrDefault(); } var client = new SendGridClient(Properties.Settings.Default.SendGridClient); var from = new EmailAddress("*****@*****.**", "Coordinator, HFED"); var subject = "HFED: Healthy Food Every Day"; var to = new EmailAddress(recipient.Email, recipient.FullName); var plainTextContent = "User " + usr.FullName + " has updated a " + schedDate.ToString("MM/dd/yyyy") + " food run from " + providerName + "." + Environment.NewLine + " Please do not reply to this email."; var msg = MailHelper.CreateSingleEmail(from, to, subject, plainTextContent, null); await client.SendEmailAsync(msg).ConfigureAwait(false); }
public ActionResult DeleteConfirmed(int id) { TutorSchedule tutorSchedule = _db.TutorSchedules.Find(id); using (var context = new SenecaContext()) { var sqlString = "SELECT Tutor_Id FROM TutorSchedule WHERE Id = " + id; var tutorId = context.Database.SqlQuery <string>(sqlString).FirstOrDefault(); if (tutorSchedule != null) { tutorSchedule.Tutor = _db.Users.Find(tutorId); tutorSchedule.DayName = GetDayOfWeekName(tutorSchedule.MinutesPastMidnight); tutorSchedule.TimeOfDay = ConvertToHhmm(tutorSchedule.MinutesPastMidnight); sqlString = "SELECT Student_Id FROM TutorSchedule WHERE Id = " + tutorSchedule.Id; var studentId = context.Database.SqlQuery <int>(sqlString).FirstOrDefault(); Student student = _db.Students.Find(studentId); tutorSchedule.Student = student; } } if (tutorSchedule?.Student != null) { string msgText = "On " + DateTime.Now.ToShortDateString() + " " + tutorSchedule.Tutor.FullName + " deleted " + tutorSchedule.Student.FirstName + " " + tutorSchedule.DayName + " " + tutorSchedule.TimeOfDay; Task <ActionResult> unused = new AccountController().EmailSheduleChange(tutorSchedule.Tutor, msgText); } _db.TutorSchedules.Remove(tutorSchedule ?? throw new InvalidOperationException()); _db.SaveChanges(); return(RedirectToAction("Index")); }
public ActionResult Create([Bind(Include = "Id,FirstName,LastName,DateOfBirth,Active,ClientNote,Location")] HfedClient hfedClient) { //EF adding blank Foreign Key records: use raw SQL using (var context = new SenecaContext()) { string cmdString = "INSERT INTO HfedClient ("; cmdString += "FirstName,LastName,DateOfBirth,Active,ClientNote,Location_Id)"; cmdString += " VALUES ("; cmdString += "'" + hfedClient.FirstName + "','" + hfedClient.LastName + "',"; cmdString += "'" + hfedClient.DateOfBirth + "',"; if (hfedClient.Active) { cmdString += "1,"; } else { cmdString += "0,"; } if (!hfedClient.ClientNote.IsNullOrEmpty()) { cmdString += "'" + hfedClient.ClientNote.Replace("'", "''") + "',"; } else { cmdString += "'',"; } cmdString += hfedClient.Location.Id + ")"; context.Database.ExecuteSqlCommand(cmdString); } TempData["ClientLocationId"] = hfedClient.Location.Id; return(RedirectToAction("Index")); }
private Boolean UserIsInRole(ApplicationUser user, string roleName) { var roleStore = new RoleStore <IdentityRole>(db); var roleMngr = new RoleManager <IdentityRole>(roleStore); var roles = roleMngr.Roles.ToList(); foreach (IdentityRole role in roles) { if (role.Name == roleName) { using (var context = new SenecaContext()) { string strSql = "SELECT RoleId FROM AspNetUserRoles WHERE "; strSql += "RoleId ='" + role.Id + "' AND UserId ='" + user.Id + "'"; string result = context.Database.SqlQuery <string>(strSql).FirstOrDefault(); if (result != null) { return(true); } } } } return(false); }
public ActionResult DriverSignUp([Bind(Include = "HfedScheds")] HfedScheduleViewModel schedules) { foreach (var sched in schedules.HfedScheds) { if (sched.SignUp) { using (var context = new SenecaContext()) { string cmdString = "UPDATE HfedSchedule SET "; cmdString += "Driver_Id='" + User.Identity.GetUserId() + "' "; cmdString += " WHERE Id=" + sched.Id; context.Database.ExecuteSqlCommand(cmdString); // Send email to driver //"You have signed up to deliver food from - to on date." } } if (sched.Cancel) { using (var context = new SenecaContext()) { string cmdString = "UPDATE HfedSchedule SET "; cmdString += "HfedDriverIds='',Driver_Id=null"; cmdString += " WHERE Id=" + sched.Id; context.Database.ExecuteSqlCommand(cmdString); // Send email to driver //"You have unchecked your delivery on date." } } } return(RedirectToAction("DriverSignUp")); }
public void TestSaveStudentTutorNote() { using (SenecaContext db = new SenecaContext()) { Debug.WriteLine(db.Database.Connection.ConnectionString); db.Database.Log = Console.Write; Student student = (from s in db.Students where s.FirstName == "Jayden" select s).Single(); ApplicationUser user = (from u in db.Users where u.UserName == "p" select u).Single(); TutorNote note = db.TutorNotes.Create(); note.Date = DateTime.Now; note.SessionNote = "xxx"; note.Student = student; note.ApplicationUser = user; db.TutorNotes.Add(note); db.SaveChanges(); TutorNote foundNote = (from n in db.TutorNotes where n.SessionNote == "xxx" select n).SingleOrDefault(); Assert.IsNotNull(foundNote); Assert.AreEqual(student, foundNote.Student); Assert.AreEqual(user, foundNote.ApplicationUser); Assert.IsTrue(student.TutorNotes.Contains(foundNote)); } }
// GET: TutorSchedules/Delete/5 public ActionResult Delete(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } TutorSchedule tutorSchedule = _db.TutorSchedules.Find(id); if (tutorSchedule == null) { return(HttpNotFound()); } using (var context = new SenecaContext()) { var sqlString = "SELECT Tutor_Id FROM TutorSchedule WHERE Id = " + tutorSchedule.Id; var tutorId = context.Database.SqlQuery <string>(sqlString).FirstOrDefault(); tutorSchedule.Tutor = _db.Users.Find(tutorId); sqlString = "SELECT Student_Id FROM TutorSchedule WHERE Id = " + tutorSchedule.Id; var studentId = context.Database.SqlQuery <int>(sqlString).FirstOrDefault(); tutorSchedule.Student = _db.Students.Find(studentId); } tutorSchedule.DayName = GetDayOfWeekName(tutorSchedule.DayOfWeekIndex); tutorSchedule.TimeOfDay = ConvertToHhmm(tutorSchedule.MinutesPastMidnight); return(View(tutorSchedule)); }
public void TestNullPrimaryTutor() { using (SenecaContext db = new SenecaContext()) { var student = db.Students.Find(6); student.PrimaryTutor = null; db.SaveChanges(); } }
public static async Task EmailDriverSignup(string userId, int scheduleId, int providerId, int locationId, string recipientId, bool signUp) { var context = new SenecaContext(); var usr = context.Users.Find(userId); var recipient = context.Users.Find(recipientId); var schedDate = context.HfedSchedules.Where(i => i.Id == scheduleId).Select(i => i.Date).FirstOrDefault(); var schedTime = context.HfedSchedules.Where(i => i.Id == scheduleId).Select(i => i.PickUpTime).FirstOrDefault(); // this returns provider & location null ==>: HfedSchedule sched = context.HfedSchedules.SingleOrDefault(i => i.Id == scheduleId); string providerName; string locationName; var sqlString = "SELECT Name FROM HfedProvider WHERE Id = " + providerId; providerName = context.Database.SqlQuery <string>(sqlString).FirstOrDefault(); sqlString = "SELECT Name FROM HfedLocation WHERE Id = " + locationId; locationName = context.Database.SqlQuery <string>(sqlString).FirstOrDefault(); var client = new SendGridClient(Properties.Settings.Default.SendGridClient); var from = new EmailAddress("*****@*****.**", "Coordinator, HFED"); var subject = "HFED: Healthy Food Every Day"; var to = new EmailAddress(recipient.Email, recipient.FullName); var firstLine = signUp ? "You have signed up " : "You have cancelled "; var plainTextContent = "Greetings " + usr.FirstName + "!" + Environment.NewLine + firstLine + "a food run on " + schedDate.ToString("MM/dd/yyyy") + " from " + providerName + " to " + locationName + ", pickup time: " + schedTime + Environment.NewLine + "Thank you for being part of this initiative. " + "Your care into action truly helps people have what they need to thrive!"; var msg = MailHelper.CreateSingleEmail(from, to, subject, plainTextContent, null); await client.SendEmailAsync(msg).ConfigureAwait(false); //var allUsers = new List<ApplicationUser>(); var allUsers = context.Users.ToList(); foreach (ApplicationUser user in allUsers) { if (UserIsInRole(user.Id, "ReceiveHfedScheduleChangeEmail")) { to = new EmailAddress(user.Email, user.FullName); plainTextContent = "COPY OF EMAIL TO " + usr.FullName + Environment.NewLine + plainTextContent; msg = MailHelper.CreateSingleEmail(from, to, subject, plainTextContent, null); await client.SendEmailAsync(msg).ConfigureAwait(false); } } }
public ActionResult Edit([Bind(Include = "Id,FirstName,LastName,DateOfBirth,Active,Location,ClientNote")] HfedClient hfedClient) { if (ModelState.IsValid) { using (var context = new SenecaContext()) { var sqlString = "UPDATE HfedClient SET "; if (hfedClient.FirstName != null) { sqlString += "FirstName = '" + hfedClient.FirstName + "',"; } if (hfedClient.LastName != null) { sqlString += "LastName = '" + hfedClient.LastName + "',"; } sqlString += "DateOfBirth = '" + hfedClient.DateOfBirth + "',"; if (hfedClient.Active) { sqlString += "Active = 1,"; } else { sqlString += "Active = 0,"; } if (hfedClient.ClientNote != null) { sqlString += "ClientNote = '" + hfedClient.ClientNote.Replace("'", "''") + "',"; } else { sqlString += "ClientNote='',"; } sqlString += "Location_Id = " + hfedClient.Location.Id; sqlString += " WHERE Id = " + hfedClient.Id; context.Database.ExecuteSqlCommand(sqlString); } // This version not updating location change: //db.Entry(hfedClient).State = EntityState.Modified; //db.SaveChanges(); TempData ["ClientLocationId"] = hfedClient.Location.Id; return(RedirectToAction("Index")); } return(View(hfedClient)); }
public ActionResult GetStudents(string id) { var allStudents = _db.Students.OrderBy(s => s.FirstName).ToList(); var students = new List <Student>(); // Don't include student(s) for whom this tutor is already PrimaryTutor // Or for whom this tutor is already Associate Tutor. foreach (Student student in allStudents) { // Check Associate Tutors for duplication: using (var context = new SenecaContext()) { var sqlString = "SELECT * FROM AssociateTutor WHERE Student_Id = " + student.Id; sqlString += " AND Tutor_Id = '" + id + "'"; AssociateTutor existingAssociateTutor = null; try { existingAssociateTutor = context.Database.SqlQuery <AssociateTutor>(sqlString).Single(); } catch (Exception ex) { var x = ex; existingAssociateTutor = null; } if (existingAssociateTutor == null) { if (student.PrimaryTutor == null) { students.Add(student); } else { if (student.PrimaryTutor.Id != id) { students.Add(student); } } } } } String json = JsonConvert.SerializeObject(students, Formatting.Indented); return(Content(json, "application/json")); }
// GET: HfedClients public ActionResult Index(int?locId) { var model = new HfedClientViewModel(); if (TempData["ClientLocationId"] != null) { locId = Convert.ToInt32(TempData["ClientLocationId"]); } // Returning from Create / Edit if (locId == 0 || locId == null) { model.HfedClients = db.HfedClients.OrderBy(l => l.LastName).ToList(); // select all Locations (default) } else { model.HfedClients = db.HfedClients.Where(l => l.Location.Id == locId).OrderBy(l => l.LastName).ToList(); } model.HfedLocations = db.HfedLocations.OrderBy(l => l.Name).ToList(); foreach (var loc in model.HfedLocations) { if (loc.Id == locId) { model.SelectedId = loc.Id; } } foreach (var hfedClient in model.HfedClients.ToList()) { using (var context = new SenecaContext()) { var sqlString = "SELECT Location_Id FROM HfedClient WHERE Id = " + hfedClient.Id; var locationId = context.Database.SqlQuery <Int32>(sqlString).FirstOrDefault(); var location = db.HfedLocations.Find(locationId); hfedClient.Location = location; hfedClient.FormattedBirthDate = hfedClient.DateOfBirth.ToString("MM/dd/yyyy"); hfedClient.NoteToolTip = hfedClient.ClientNote.Replace(" ", "\u00a0"); // (full length on mouseover) \u00a0 is the Unicode character for NO-BREAK-SPACE. var s = hfedClient.ClientNote; // For display, abbreviate to 10 characters: s = s.Length <= 10 ? s : s.Substring(0, 10) + "..."; hfedClient.ClientNote = s; } } return(View(model)); }
public ActionResult Index() { var userId = User.Identity.GetUserId(); ApplicationUser user = (from u in _db.Users where u.Id == userId select u).Single(); TutorScheduleViewModel viewModel = new TutorScheduleViewModel { Tutor = user }; viewModel.Tutor.TutorSchedules = new List <TutorSchedule>(); foreach (var tutorSchedule in _db.TutorSchedules.ToList()) { using (var context = new SenecaContext()) { var sqlString = "SELECT Tutor_Id FROM TutorSchedule WHERE Id = " + tutorSchedule.Id; var tutorId = context.Database.SqlQuery <string>(sqlString).FirstOrDefault(); var tutor = _db.Users.Find(tutorId); if (tutor.Id != userId) { continue; } sqlString = "SELECT Student_Id FROM TutorSchedule WHERE Id = " + tutorSchedule.Id; var studentId = context.Database.SqlQuery <int>(sqlString).FirstOrDefault(); var student = _db.Students.Find(studentId); TutorSchedule ts = new TutorSchedule { Id = tutorSchedule.Id, Tutor = tutor, Student = student, MinutesPastMidnight = tutorSchedule.MinutesPastMidnight, DayName = GetDayOfWeekName(tutorSchedule.DayOfWeekIndex), DayOfWeekIndex = tutorSchedule.DayOfWeekIndex, TimeOfDay = ConvertToHhmm(tutorSchedule.MinutesPastMidnight) }; viewModel.Tutor.TutorSchedules.Add(ts); } } return(View(viewModel)); }
// GET: AssociateTutors public ActionResult Index() { var model = new List <AssociateTutor>(); foreach (var tutor in _db.AssociateTutors.ToList()) { using (var context = new SenecaContext()) { var sqlString = "SELECT Tutor_Id FROM AssociateTutor WHERE Id = " + tutor.Id; var tutorId = context.Database.SqlQuery <string>(sqlString).FirstOrDefault(); tutor.Tutor = _db.Users.Find(tutorId); sqlString = "SELECT Student_Id FROM AssociateTutor WHERE Id = " + tutor.Id; var studentId = context.Database.SqlQuery <int>(sqlString).FirstOrDefault(); tutor.Student = _db.Students.Find(studentId); model.Add(tutor); } } return(View(model)); }
public ActionResult Edit([Bind(Include = "Id,Tutor,Student,DayName,TimeOfDay")] TutorScheduleViewModel tutorSchedule) { if (ModelState.IsValid) { var modifiedSchedule = _db.TutorSchedules.Find(tutorSchedule.Id); if (modifiedSchedule != null) { using (var context = new SenecaContext()) { var sqlString = "SELECT Tutor_Id FROM TutorSchedule WHERE Id = " + tutorSchedule.Id; var tutorId = context.Database.SqlQuery <string>(sqlString).FirstOrDefault(); ApplicationUser tutor = _db.Users.Find(tutorId); modifiedSchedule.Tutor = tutor; tutorSchedule.Tutor = tutor; sqlString = "SELECT Student_Id FROM TutorSchedule WHERE Id = " + tutorSchedule.Id; var studentId = context.Database.SqlQuery <int>(sqlString).FirstOrDefault(); Student student = _db.Students.Find(studentId); modifiedSchedule.Student = student; } modifiedSchedule.DayOfWeekIndex = GetDayOfWeekIndex(tutorSchedule.DayName); modifiedSchedule.MinutesPastMidnight = ConvertToMinutesPastMidnight(tutorSchedule.TimeOfDay); _db.Entry(modifiedSchedule).State = EntityState.Modified; _db.SaveChanges(); if (modifiedSchedule.Student != null) { string msgText = "On " + DateTime.Now.ToShortDateString() + " " + modifiedSchedule.Tutor.FullName + " changed " + modifiedSchedule.Student.FirstName + " to " + tutorSchedule.DayName + " " + tutorSchedule.TimeOfDay; Task <ActionResult> unused = new AccountController().EmailSheduleChange(modifiedSchedule.Tutor, msgText); } return(RedirectToAction("Index")); } } return(View(tutorSchedule)); }
public ActionResult Create([Bind(Include = "Id,FirstName,LastName,DateOfBirth,Active,ClientNote,Location,NumberInHousehold")] HfedClient hfedClient) { //EF adding blank Foreign Key records: use raw SQL using (var context = new SenecaContext()) { var query = @"INSERT INTO HfedClient" + " (FirstName,LastName,DateOfBirth,Active,ClientNote,Location_Id,NumberInHousehold)" + " VALUES " + "(@FirstName,@LastName,@DateOfBirth,@Active,@ClientNote,@Location_Id,@NumberInHousehold)"; context.Database.ExecuteSqlCommand(query, new SqlParameter("@FirstName", hfedClient.FirstName), new SqlParameter("@LastName", hfedClient.LastName), new SqlParameter("@DateOfBirth", hfedClient.DateOfBirth.ToShortDateString()), new SqlParameter("@Active", hfedClient.Active ? 1 : 0), new SqlParameter("@ClientNote", hfedClient.ClientNote + ""), new SqlParameter("@Location_Id", hfedClient.Location.Id), new SqlParameter("@NumberInHousehold", hfedClient.NumberInHousehold)); } // This version not updating location change: TempData["ClientLocationId"] = hfedClient.Location.Id; return(RedirectToAction("Index")); }
public ActionResult Edit([Bind(Include = "Id,MotherFather,FirstName,Address,HomePhone,CellPhone,Email,SelectedMotherFather,CaseManagerUser")] AddEditParentViewModel viewModel) { if (ModelState.IsValid) { var parent = _db.Parents.Find(viewModel.Id); if (parent != null) { parent.FirstName = viewModel.FirstName; parent.Address = viewModel.Address; parent.HomePhone = viewModel.HomePhone; parent.CellPhone = viewModel.CellPhone; parent.Email = viewModel.Email; parent.MotherFather = viewModel.SelectedMotherFather; if (string.IsNullOrEmpty(viewModel.CaseManagerUser.Id)) { //parent.CaseManagerUser = null; // this statement is not peformed by Entity Framework var sqlString = "UPDATE Parent Set CaseManagerUser_Id = NULL "; sqlString += "WHERE Id =" + viewModel.Id; using (var context = new SenecaContext()) { context.Database.ExecuteSqlCommand(sqlString); } } else { parent.CaseManagerUser = (from u in _db.Users where u.Id == viewModel.CaseManagerUser.Id select u).Single(); } _db.SaveChanges(); return(RedirectToAction("Index")); } } return(View(viewModel)); }
// GET: AssociateTutors/Delete/5 public ActionResult Delete(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } AssociateTutor associateTutor = _db.AssociateTutors.Find(id); if (associateTutor == null) { return(HttpNotFound()); } using (var context = new SenecaContext()) { var sqlString = "SELECT Tutor_Id FROM AssociateTutor WHERE Id = " + associateTutor.Id; var tutorId = context.Database.SqlQuery <string>(sqlString).FirstOrDefault(); associateTutor.Tutor = _db.Users.Find(tutorId); sqlString = "SELECT Student_Id FROM AssociateTutor WHERE Id = " + associateTutor.Id; var studentId = context.Database.SqlQuery <int>(sqlString).FirstOrDefault(); associateTutor.Student = _db.Students.Find(studentId); } return(View(associateTutor)); }
public ActionResult Edit([Bind(Include = "Id,FirstName,Gender,BirthDate,GradeLevel,SpecialClass,School,Parent,PrimaryTutor,Teacher,Active")] AddEditStudentViewModel viewModel) { if (ModelState.IsValid) { var student = _db.Students.Find(viewModel.Id); if (student != null) { student.FirstName = viewModel.FirstName; student.Gender = viewModel.Gender; student.BirthDate = viewModel.BirthDate; student.GradeLevel = viewModel.GradeLevel; student.SpecialClass = viewModel.SpecialClass; student.Active = viewModel.Active; if (viewModel.Parent != null) { student.Parent = (from p in _db.Parents where p.Id == viewModel.Parent.Id select p).Single(); } if (viewModel.School != null) { student.School = (from s in _db.Schools where s.Id == viewModel.School.Id select s).Single(); } if (viewModel.PrimaryTutor.Id != "0") { student.PrimaryTutor = (from t in _db.Users where t.Id == viewModel.PrimaryTutor.Id select t).Single(); } else { // student.PrimaryTutor = null; // this statement is not peformed by Entity Framework var sqlString = "UPDATE Student Set PrimaryTutor_Id = NULL "; sqlString += "WHERE Id =" + viewModel.Id; using (var context = new SenecaContext()) { context.Database.ExecuteSqlCommand(sqlString); } } if (viewModel.Teacher != null) { if (viewModel.Teacher.Id == 0) { // student.Teacher = null; // this statement is not peformed by Entity Framework var sqlString = "UPDATE Student Set Teacher_Id = NULL "; sqlString += "WHERE Id =" + viewModel.Id; using (var context = new SenecaContext()) { context.Database.ExecuteSqlCommand(sqlString); } } else { student.Teacher = (from t in _db.Teachers where t.Id == viewModel.Teacher.Id select t).Single(); } } } _db.SaveChanges(); return(RedirectToAction("Index")); } return(View(viewModel)); }
// GET: TutorSchedules/Edit/5 public ActionResult Edit(int?id) { TutorScheduleViewModel viewModel = null; var ts = _db.TutorSchedules.Find(id); if (ts != null) { string dayName = GetDayOfWeekName(ts.DayOfWeekIndex); string timeOfDay = ConvertToHhmm(ts.MinutesPastMidnight); var validTutorList = new List <ApplicationUser>(); using (var context = new SenecaContext()) { var sqlString = "SELECT Tutor_Id FROM TutorSchedule WHERE Id = " + id; var tutorId = context.Database.SqlQuery <string>(sqlString).FirstOrDefault(); ApplicationUser tutor = _db.Users.Find(tutorId); sqlString = "SELECT Student_Id FROM TutorSchedule WHERE Id = " + id; var studentId = context.Database.SqlQuery <int>(sqlString).FirstOrDefault(); Student student = _db.Students.Find(studentId); var tutors = _db.Users.OrderBy(u => u.LastName).ToList(); foreach (ApplicationUser user in tutors) { foreach (var role in user.Roles) { var identityRole = (from r in _db.Roles where (r.Id == role.RoleId) select r).Single(); if (identityRole.Name != "Tutor") { continue; } validTutorList.Add(user); } } var daysList = new List <string>() { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" }; var timesList = new List <string>() { "10:00", "10:15", "10:30", "10:45", "11:00", "11:15", "11:30", "11:45", "1:00", "1:15", "1:30", "1:45", "2:00", "2:15", "2:30", "2:45", "3:00", "3:15", "3:30", "3:45", "4:00", "4:15", "4:30", "4:45", "5:00", "5:15", "5:30", "TBD" }; TutorScheduleViewModel newTutorSchedule = new TutorScheduleViewModel() { Tutor = tutor, Tutors = validTutorList, Student = student, DayName = dayName, TimeOfDay = timeOfDay, DaysList = daysList, TimesList = timesList, }; viewModel = newTutorSchedule; } } return(View(viewModel)); }
public ActionResult DuplicateConfirmed() { // Get this month's schedules: var sDate = Convert.ToDateTime(Session["StartDate"]); var eDate = Convert.ToDateTime(Session["EndDate"]); var hfedSchedules = db.HfedSchedules.Where(s => s.Date >= sDate && s.Date <= eDate).OrderBy(s => s.Date).ToList(); foreach (HfedSchedule hfedSchedule in hfedSchedules) { string sqlString = "SELECT * FROM HfedSchedule WHERE Id = " + hfedSchedule.Id; var schedule = db.Database.SqlQuery <HfedScheduleViewModel>(sqlString).ToList(); // Gets mapped Foreign Keys sqlString = "SELECT * FROM HfedLocation WHERE Id = " + schedule[0].Location_Id; var location = db.Database.SqlQuery <HfedLocation>(sqlString).ToList(); hfedSchedule.Location = location[0]; hfedSchedule.PointPerson = db.Users.Find(schedule[0].PointPerson_Id); sqlString = "SELECT * FROM HfedProvider WHERE Id = " + schedule[0].Provider_Id; var provider = db.Database.SqlQuery <HfedProvider>(sqlString).ToList(); hfedSchedule.Provider = provider[0]; hfedSchedule.HfedDriverIds = null; // no drivers in duplicate // adjust date: var dt = hfedSchedule.Date.AddMonths(1); var monthStart = new DateTime(dt.Year, dt.Month, 1); var dow = hfedSchedule.Date.DayOfWeek; for (int i = -7; i < 8; i++) // Find 1st date within month with same dow { var newDate = dt.AddDays(i); if (newDate.DayOfWeek == dow && newDate >= monthStart) { hfedSchedule.Date = newDate; using (var context = new SenecaContext()) { string cmdString = "INSERT INTO HfedSchedule ("; cmdString += "Date,PickUpTime,ScheduleNote,Request,Complete,Households,"; cmdString += "Location_Id,PointPerson_Id,Provider_Id,HfedDriverIds,HfedClientIds)"; cmdString += " VALUES ("; cmdString += "'" + hfedSchedule.Date + "','" + hfedSchedule.PickUpTime + "',"; if (hfedSchedule.ScheduleNote != null) { cmdString += "'" + hfedSchedule.ScheduleNote.Replace("'", "''") + "',"; } else { cmdString += "'',"; } cmdString += "0,0,"; // Request & Complete = false cmdString += hfedSchedule.Households + ","; cmdString += hfedSchedule.Location.Id + ","; cmdString += "'" + hfedSchedule.PointPerson.Id + "'," + hfedSchedule.Provider.Id + ","; cmdString += "'" + hfedSchedule.HfedDriverIds + "',"; cmdString += "'" + hfedSchedule.HfedClientIds + "')"; context.Database.ExecuteSqlCommand(cmdString); } break; } } } // Set Start/End dates to view the newly created schedule: return(RedirectToAction("MonthNext")); }
public ActionResult DownloadExcelFile() { XLWorkbook workbook = new XLWorkbook(); IXLWorksheet ws = workbook.Worksheets.Add("TutoringSchedule"); ws.Cell(1, 1).SetValue("Tutoring Schedule"); ws.Cell(2, 1).SetValue("(" + DateTime.Now.ToShortDateString() + ")"); ws.Cell(3, 2).SetValue("Monday"); ws.Cell(3, 3).SetValue("Tuesday"); ws.Cell(3, 4).SetValue("Wednesday"); ws.Cell(3, 5).SetValue("Thursday"); ws.Cell(3, 6).SetValue("Friday"); ws.Cell(3, 7).SetValue("Saturday"); ws.Cell(3, 8).SetValue("Sunday"); ws.Row(3).Style.Font.Bold = true; int previousTime = -1; int previousDay = -1; int activeRow = 4; using (var context = new SenecaContext()) { var tutorSchedules = _db.TutorSchedules.OrderBy(t => t.MinutesPastMidnight).ThenBy(t => t.DayOfWeekIndex).ToList(); foreach (TutorSchedule tutorSchedule in tutorSchedules) { var sqlString = "SELECT Tutor_Id FROM TutorSchedule WHERE Id = " + tutorSchedule.Id; var tutorId = context.Database.SqlQuery <string>(sqlString).FirstOrDefault(); ApplicationUser tutor = _db.Users.Find(tutorId); sqlString = "SELECT Student_Id FROM TutorSchedule WHERE Id = " + tutorSchedule.Id; var studentId = context.Database.SqlQuery <int>(sqlString).FirstOrDefault(); Student student = _db.Students.Find(studentId); tutorSchedule.TimeOfDay = ConvertToHhmm(tutorSchedule.MinutesPastMidnight); if (previousDay != -1 && previousTime == tutorSchedule.MinutesPastMidnight && previousDay != tutorSchedule.DayOfWeekIndex) { activeRow -= 1; } ws.Cell(activeRow, 1).SetValue(tutorSchedule.TimeOfDay); if (student != null) { var tutorAndStudent = tutor.LastName + ": " + student.FirstName; ws.Cell(activeRow, tutorSchedule.DayOfWeekIndex + 2).SetValue(tutorAndStudent); } previousDay = tutorSchedule.DayOfWeekIndex; previousTime = tutorSchedule.MinutesPastMidnight; activeRow += 1; } } for (int i = 1; i < 9; i++) { ws.Column(i).AdjustToContents(); } for (int i = 2; i < activeRow; i++) { ws.Row(i).Style.Alignment.SetHorizontal(XLAlignmentHorizontalValues.Center); } ws.Row(3).Style.Alignment.SetHorizontal(XLAlignmentHorizontalValues.Center); activeRow += 1; ws.Cell(activeRow, 1).SetValue("Tutors"); ws.Cell(activeRow, 1).Style.Alignment.SetHorizontal(XLAlignmentHorizontalValues.Center); ws.Cell(activeRow, 1).Style.Font.Bold = true; var users = _db.Users.OrderBy(u => u.LastName).ToList(); var validTutorList = new List <ApplicationUser>(); foreach (ApplicationUser user in users) { foreach (var role in user.Roles) { var identityRole = (from r in _db.Roles where (r.Id == role.RoleId) select r).Single(); if (identityRole.Name != "Tutor") { continue; } validTutorList.Add(user); } } foreach (ApplicationUser tutor in validTutorList) { activeRow += 1; ws.Cell(activeRow, 1).SetValue(tutor.FullName); ws.Cell(activeRow, 2).SetValue(tutor.PhoneNumber); } MemoryStream ms = new MemoryStream(); workbook.SaveAs(ms); ms.Position = 0; return(new FileStreamResult(ms, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") { FileDownloadName = "TutoringSchedule.xlsx" }); }
public ActionResult Create([Bind(Include = "Id,Date,PickUpTime,Provider,Location,PointPerson," + "ScheduleNote,Driver,Request,Complete,Households,Approved," + "HfedDriversArray,HfedClientsArray,VolunteerHours")] HfedScheduleViewModel hfedSchedule) { if (hfedSchedule.PointPerson.Id == null || hfedSchedule.Location.Id == 0 || hfedSchedule.Provider.Id == 0) { hfedSchedule = GetDropDownData(hfedSchedule); // Reload dropdown lists if (hfedSchedule.PointPerson.Id == null) { hfedSchedule.ErrorMessage = "Point Person required"; } if (hfedSchedule.Location.Id == 0) { hfedSchedule.ErrorMessage = "Location required"; } if (hfedSchedule.Provider.Id == 0) { hfedSchedule.ErrorMessage = "Provider required"; } hfedSchedule = GetDropDownData(hfedSchedule); // Reload dropdown lists return(View(hfedSchedule)); // (for error functions) } hfedSchedule = CheckForDuplicateClient(hfedSchedule); if (!hfedSchedule.ErrorMessage.IsNullOrEmpty()) { return(View(hfedSchedule)); } if (hfedSchedule.HfedDriversArray.IsNullOrEmpty()) { hfedSchedule.HfedDriverIds = string.Empty; } else { hfedSchedule.HfedDriverIds = string.Join(",", hfedSchedule.HfedDriversArray); } if (hfedSchedule.HfedClientsArray.IsNullOrEmpty()) { hfedSchedule.HfedClientIds = String.Empty; } else { hfedSchedule.HfedClientIds = string.Join(",", hfedSchedule.HfedClientsArray); } //EF not adding blank Foreign Key records: use raw SQL using (var context = new SenecaContext()) { string cmdString = "INSERT INTO HfedSchedule ("; cmdString += "Date,PickUpTime,ScheduleNote,Request,Complete,"; cmdString += "Households,Approved,"; cmdString += "Location_Id,PointPerson_Id,Provider_Id,HfedDriverIds,HfedClientIds)"; cmdString += " VALUES ("; cmdString += "'" + hfedSchedule.Date + "','" + hfedSchedule.PickUpTime + "',"; if (hfedSchedule.ScheduleNote.IsNullOrEmpty()) { cmdString += "'',"; } else { cmdString += "'" + hfedSchedule.ScheduleNote.Replace("'", "''") + "',"; } cmdString += "0,0,"; // insert zeroes into Request & Complete if (hfedSchedule.Households == null) { cmdString += "0,"; } else { cmdString += hfedSchedule.Households + ","; } if (hfedSchedule.Approved) { cmdString += "1,"; } else { cmdString += "0,"; } cmdString += hfedSchedule.Location.Id + ","; cmdString += "'" + hfedSchedule.PointPerson.Id + "'," + hfedSchedule.Provider.Id + ","; cmdString += "'" + hfedSchedule.HfedDriverIds + "',"; cmdString += "'" + hfedSchedule.HfedClientIds + "')"; context.Database.ExecuteSqlCommand(cmdString); } return(RedirectToAction("Index")); }
private static string GetTentativeSchedule() // Get the recently copied Next Month schedule { string htmlContent = ""; using (var context = new SenecaContext()) { // Use month of latest schedule on file: var sqlString = "SELECT TOP 1 Date FROM HfedSchedule ORDER BY Date DESC"; var latestScheduleDate = context.Database.SqlQuery <DateTime>(sqlString).FirstOrDefault(); var month = latestScheduleDate.Month; var year = latestScheduleDate.Year; var start = new DateTime(year, month, 1); var end = new DateTime(start.Year, start.Month, DateTime.DaysInMonth(start.Year, start.Month)); var strEend = end.ToString("M/dd/yy"); htmlContent += "<table><tr style=\"font-weight:bold\">"; htmlContent += "<td>Date</td>"; htmlContent += "<td>Provider</td>"; htmlContent += "<td>Pick Up</td>"; htmlContent += "<td>Location</td>"; htmlContent += "<td>Households</td>"; htmlContent += "<td>Point</td>"; htmlContent += "<td>Driver</td>"; htmlContent += "</tr>"; sqlString = "SELECT * FROM HfedSchedule WHERE Date >= '" + start.ToString("MM/dd/yyyy") + "'"; sqlString += " AND Date <= '" + strEend + "' ORDER BY Date"; var deliveryRequests = context.Database.SqlQuery <HfedSchedule>(sqlString).ToList(); foreach (HfedSchedule request in deliveryRequests) { var dow = request.Date.ToString("ddd") + " "; htmlContent += "<tr><td>" + (dow + request.Date.ToString("MM/dd/yy")) + "</td>"; sqlString = "SELECT * FROM HfedSchedule WHERE Id = " + request.Id; var schedule = context.Database.SqlQuery <HfedScheduleViewModel>(sqlString).FirstOrDefault(); sqlString = "SELECT * FROM HfedProvider WHERE Id = " + schedule.Provider_Id; var provider = context.Database.SqlQuery <HfedProvider>(sqlString).FirstOrDefault(); htmlContent += "<td>" + provider.Name + "</td>"; htmlContent += "<td style=\"text-align:center\">" + request.PickUpTime + "</td>"; sqlString = "SELECT * FROM HfedLocation WHERE Id = " + schedule.Location_Id; var location = context.Database.SqlQuery <HfedLocation>(sqlString).FirstOrDefault(); htmlContent += "<td>" + location.Name + "</td>"; htmlContent += "<td style=\"text-align:center\">" + request.Households + "</td>"; sqlString = "SELECT * FROM AspNetUsers WHERE Id = '" + schedule.PointPerson_Id + "'"; var pointPerson = context.Database.SqlQuery <ApplicationUser> (sqlString).FirstOrDefault(); htmlContent += "<td>" + pointPerson.FirstName + "</td>"; string driverName = ""; if (schedule.Driver_Id != null) { sqlString = "SELECT * FROM AspNetUsers WHERE Id = '" + schedule.Driver_Id + "'"; var driver = context.Database.SqlQuery <ApplicationUser> (sqlString).FirstOrDefault(); if (driver != null) { driverName = driver.FirstName; } } htmlContent += "<td>" + driverName + "</td></tr>"; } htmlContent += "</table>"; } return(htmlContent); }
private static string GetReminderSchedule(DateTime reminderDate) { string htmlContent = ""; using (var context = new SenecaContext()) { var sqlString = "SELECT * FROM HfedSchedule WHERE Date = '" + reminderDate + "'"; var schedules = context.Database.SqlQuery <HfedScheduleViewModel>(sqlString).ToList(); foreach (HfedScheduleViewModel reminder in schedules) { htmlContent += "<table border=" + (char)34 + "1" + (char)34 + "><tr>"; sqlString = "SELECT * FROM HfedSchedule WHERE Id = " + reminder.Id; var schedule = context.Database.SqlQuery <HfedScheduleViewModel>(sqlString).FirstOrDefault(); htmlContent += "<td>" + reminder.Date.ToShortDateString() + "</td>"; sqlString = "SELECT * FROM HfedProvider WHERE Id = " + schedule.Provider_Id; var provider = context.Database.SqlQuery <HfedProvider>(sqlString).ToList(); htmlContent += "<td>" + provider[0].Name + "</td>"; sqlString = "SELECT * FROM HfedLocation WHERE Id = " + schedule.Location_Id; var location = context.Database.SqlQuery <HfedLocation>(sqlString).FirstOrDefault(); htmlContent += "<td>" + location.Name + "</td>"; htmlContent += "<td>" + reminder.PickUpTime + "</td>"; ApplicationUser pointPerson = context.Users.Find(schedule.PointPerson_Id); htmlContent += "<td>" + pointPerson.FirstName + "</td>"; ApplicationUser driver = new ApplicationUser(); if (!reminder.Driver_Id.IsNullOrEmpty()) { driver = context.Users.Find(reminder.Driver_Id); } htmlContent += "<td>" + driver.FirstName + "</td></tr></table>"; htmlContent += "<table><tr><td>Point Person:</td>"; htmlContent += "<td>" + pointPerson.FirstName + " " + pointPerson.LastName + "</td>"; htmlContent += "<td> " + pointPerson.PhoneNumber + "</td><td> " + pointPerson.Email + "</td></tr></table>"; htmlContent += "<table><tr><td>Pick Up:</td>"; htmlContent += "<td>" + provider[0].Name + " " + provider[0].Address + "</td>"; htmlContent += "<td> " + provider[0].MainPhone + "</td>"; if (!provider[0].ProviderNote.IsNullOrEmpty()) { htmlContent += "<td> " + provider[0].ProviderNote + "</td>"; } htmlContent += "</tr></table>"; htmlContent += "<table><tr><td>Drop Off</td>"; htmlContent += "<td>" + location.Name + " " + location.Address + "</td><td> " + location.MainPhone + "</td>"; if (!location.LocationNote.IsNullOrEmpty()) { htmlContent += "<td> " + location.LocationNote + " " + "</td>"; } htmlContent += "</tr></table>"; if (!reminder.ScheduleNote.IsNullOrEmpty()) { htmlContent += "<table><tr><td>Schedule Note:</td>"; htmlContent += "<td>" + reminder.ScheduleNote + "</td></tr></table>"; } if (!reminder.Driver_Id.IsNullOrEmpty()) { htmlContent += "<table><tr><td>Driver:</td>"; htmlContent += "<td>" + driver.FullName + " " + driver.PhoneNumber + " " + driver.Email + "</td></tr></table>"; } if (!reminder.HfedClientIds.IsNullOrEmpty()) { htmlContent += "<table><tr><td>Clients:</td>"; var clientIdArray = reminder.HfedClientIds.Split(',').ToArray(); for (int i = 0; i < clientIdArray.Length; i++) { htmlContent += "<td>"; var client = context.HfedClients.Find(Convert.ToInt32(clientIdArray[i])); if (client != null) { htmlContent += client.FullName; if (i < clientIdArray.Length - 1) { htmlContent += ", "; } } } htmlContent += "</td></tr></table>"; } } } return(htmlContent); }
private static string GetDriverSchedule(bool withDrivers, DateTime startDate, DateTime endDate) { using (var context = new SenecaContext()) { string text = "<table border=" + (char)34 + "1" + (char)34 + ">"; var sqlString = "SELECT * FROM HfedSchedule WHERE Date >='" + startDate + "' AND Date <='" + endDate + "' Order By Date"; var deliveryRequests = context.Database.SqlQuery <HfedScheduleViewModel>(sqlString).ToList(); foreach (HfedScheduleViewModel request in deliveryRequests) { text += "<tr><td>" + request.Date.ToShortDateString() + "</td>"; sqlString = "SELECT * FROM HfedSchedule WHERE Id = " + request.Id; var schedule = context.Database.SqlQuery <HfedScheduleViewModel>(sqlString).FirstOrDefault(); sqlString = "SELECT * FROM HfedProvider WHERE Id = " + schedule.Provider_Id; var provider = context.Database.SqlQuery <HfedProvider>(sqlString).ToList(); text += "<td>" + provider[0].Name + "</td>"; sqlString = "SELECT * FROM HfedLocation WHERE Id = " + schedule.Location_Id; var location = context.Database.SqlQuery <HfedLocation>(sqlString).ToList(); text += "<td>" + location[0].Name + "</td>"; text += "<td>" + request.PickUpTime + "</td>"; ApplicationUser pointPerson = context.Users.Find(schedule.PointPerson_Id); text += "<td>" + pointPerson.FirstName + "</td>"; text += "<td bgcolor=" + (char)34 + "#FFFF00" + (char)34 + ">"; if (withDrivers && !request.Driver_Id.IsNullOrEmpty()) { ApplicationUser driver = context.Users.Find(request.Driver_Id); string driverName; if (driver == null) { driverName = " " + " " + " "; } else { driverName = driver.FirstName; } text += driverName + "</td>"; var s = request.ScheduleNote; // For display, abbreviate to 64 characters: s = s.Length <= 64 ? s : s.Substring(0, 64) + "..."; text += "<td>" + s + "</td>"; } else { text += "   </td>"; if (withDrivers) { var s = request.ScheduleNote; // For display, abbreviate to 64 characters: s = s.Length <= 64 ? s : s.Substring(0, 64) + "..."; text += "<td>" + s + "</td>"; } } } text += "<tr></table><br />"; return(text); } }