public static bool Create_Appointment(string title, string date, string time, string description) { //This creates the appointment for the user bool success = true; try { using (var dbContext = new usersinfoEntities()) { userAppointmentsInfo userAppointment = new userAppointmentsInfo(); userAppointment.appointment_CreationDate = System.DateTime.UtcNow.Date; userAppointment.appointment_Date = DateTime.Parse(date); userAppointment.appointment_Description = description; userAppointment.appointment_Time = TimeSpan.Parse(time); userAppointment.userid = (int)HttpContext.Current.Session["userid"]; userAppointment.appointment_Title = title; dbContext.userAppointmentsInfo.Add(userAppointment); dbContext.SaveChanges(); } } catch (Exception ex) { success = false; } return(success); }
protected void Page_Load(object sender, EventArgs e) { int userid = (int)Session["userid"]; //int userid=1; if (Request.QueryString["apty"] != null) { string appointmentType = Request.QueryString["apty"].ToString(); using (var dbContext = new usersinfoEntities()) { Response.Clear(); Response.ContentType = "application/json; charset=utf-8"; var settings = new JsonSerializerSettings { DateFormatString = "yyyy-MM-dd", DateTimeZoneHandling = DateTimeZoneHandling.Utc }; string json = ""; if (appointmentType == "up") { //display upcoming appointments var userAppointments = dbContext.userAppointmentsInfo.Where(apps => apps.userid == userid && DbFunctions.TruncateTime(apps.appointment_Date) >= DbFunctions.TruncateTime(System.DateTime.Now)).ToList(); //var userAppointments1 = userAppointments.ForEach(time => DateTime.Parse(time.appointment_Time.ToString()).ToShortTimeString()); for (int i = 0; i < userAppointments.Count; i++) { //TimeSpan sp = TimeSpan.Parse(DateTime.Parse(userAppointments[i].appointment_Time.ToString()).ToShortTimeString()); } if (userAppointments != null) { json = JsonConvert.SerializeObject(userAppointments, settings); } } else { //display past appointments var userAppointments = dbContext.userAppointmentsInfo.Where(apps => apps.userid == userid && DbFunctions.TruncateTime(apps.appointment_Date) < DbFunctions.TruncateTime(System.DateTime.Now)).ToList(); if (userAppointments != null) { json = JsonConvert.SerializeObject(userAppointments, settings); } } Response.Write(json); Response.Flush(); Response.End(); } } else { string operation = Request.QueryString["op"].ToString(); Stream req = Request.InputStream; req.Seek(0, System.IO.SeekOrigin.Begin); string json = new StreamReader(req).ReadToEnd(); Appointment ap = JsonConvert.DeserializeObject <Appointment>(json); if (operation == "create") { try { using (var dbContext = new usersinfoEntities()) { userAppointmentsInfo userAppointment = new userAppointmentsInfo(); userAppointment.appointment_CreationDate = System.DateTime.UtcNow.Date; userAppointment.appointment_Date = ap.date; userAppointment.appointment_Description = ap.description; userAppointment.appointment_Time = ap.time; userAppointment.userid = (int)HttpContext.Current.Session["userid"]; userAppointment.appointment_Title = ap.title; dbContext.userAppointmentsInfo.Add(userAppointment); dbContext.SaveChanges(); Response.Write("{\"sucess\":\"true\"}"); Response.End(); } } catch (Exception ex) { //Response.Write("\"sucess\":\"false\""); } } else if (operation == "edit") { try { using (var dbContext = new usersinfoEntities()) { var appointment = dbContext.userAppointmentsInfo.SingleOrDefault(app => app.appointmentid == ap.appointmentId); appointment.appointment_Date = ap.date; appointment.appointment_Time = ap.time; dbContext.SaveChanges(); Response.Write("{\"sucess\":\"true\"}"); Response.End(); } } catch (Exception ex) { //Response.Write("\"sucess\":\"false\""); } } else if (operation == "delete") { try { using (var dbContext = new usersinfoEntities()) { var appointment = dbContext.userAppointmentsInfo.SingleOrDefault(app => app.appointmentid == ap.appointmentId); dbContext.userAppointmentsInfo.Remove(appointment); dbContext.SaveChanges(); Response.Write("{\"sucess\":\"true\"}"); Response.End(); } } catch (Exception ex) { //Response.Write("\"sucess\":\"false\""); } } else if (operation == "logout") { FormsAuthentication.SignOut(); FormsAuthentication.RedirectToLoginPage(); } } }