/// <summary> /// This is the method for creating a new UserAccount. It takes first name, last name, phone number, address, city, state, zip, and country as parameters. /// </summary> public static UserAccount CreateUserAccount(string firstName, string lastName, string email) { using (var db = new LanguageExchangeModel()) { var user = new UserAccount(); user.UserFirstName = firstName; user.UserLastName = lastName; user.UserEmail = email; db.UserAccounts.Add(user); db.SaveChanges(); SystemEmail("Welcome to Xchange (http://languagexchange.azurewebsites.net/)! Don't forget to fill out your profile so you can start connecting with other language learners!", email); return user; } }
public static void CheckForAll(string email) { using (var db = new LanguageExchangeModel()) { var user = db.UserAccounts.Where(u => u.UserEmail == email).FirstOrDefault(); if (user == null) { return; } var userId = user.UserIdNumber; var pref = db.UserPreferences.Where(i => i.UserId == userId).FirstOrDefault(); if (pref == null) { pref = new UserPreferences(); pref.UserPreferencesName = user.UserFirstName + " " + user.UserLastName; pref.UserId = userId; pref.UserDescription = "Something interesting about me!"; pref.LastLogin = DateTime.Now; db.UserPreferences.Add(pref); } var calendar = db.AppointmentCalendars.Where(c => c.UserId == userId).FirstOrDefault(); if (calendar == null) { calendar = new AppointmentCalendar(); calendar.UserId = userId; db.AppointmentCalendars.Add(calendar); } var inbox = db.EmailInboxes.Where(i => i.UserId == userId).FirstOrDefault(); if (inbox == null) { inbox = new EmailInbox(); inbox.UserId = userId; db.EmailInboxes.Add(inbox); } var tracking = db.UserTrackingDetails.Where(t => t.UserId == userId).FirstOrDefault(); if (tracking == null) { tracking = new UserTrackingDetails(); tracking.UserId = userId; db.UserTrackingDetails.Add(tracking); } db.SaveChanges(); } }
public static void MakeAppointment (string email1, int id, string learnOrTeach, Appointments updatedAppointment) { var db = new LanguageExchangeModel(); var user1 = db.UserAccounts.Where(u => u.UserEmail == email1).FirstOrDefault(); var userId1 = user1.UserIdNumber; var user2 = db.UserAccounts.Where(u => u.UserIdNumber == id).FirstOrDefault(); var userId2 = user2.UserIdNumber; var user2email = user2.UserEmail; if (userId1 == userId2) { return; } var calendar1 = db.AppointmentCalendars.Where(c => c.UserId == userId1).FirstOrDefault(); if (calendar1 == null) { throw new ArgumentException("Appointment Calendar not valid for the user"); } var calendar2 = db.AppointmentCalendars.Where(c => c.UserId == userId2).FirstOrDefault(); if (calendar2 == null) { throw new ArgumentException("Appointment Calendar not valid for the user"); } var appointment = new Appointments(); if (updatedAppointment.AppointmentStartTime < DateTime.Now) { return; } appointment.AppointmentStartTime = updatedAppointment.AppointmentStartTime; appointment.AppointmentEndTime = updatedAppointment.AppointmentEndTime; appointment.AppointmentLanguage = updatedAppointment.AppointmentLanguage; appointment.UserId1 = calendar1.UserId; appointment.UserId2 = calendar2.UserId; appointment.TimeZone = updatedAppointment.TimeZone; if (learnOrTeach == "Learning") { appointment.AppointmentLearner = userId1; appointment.AppointmentTeacher = userId2; } if (learnOrTeach == "Teaching") { appointment.AppointmentLearner = userId2; appointment.AppointmentTeacher = userId1; } appointment.AppointmentPerson1 = user1.UserFirstName + " " + user1.UserLastName; appointment.AppointmentPerson2 = user2.UserFirstName + " " + user2.UserLastName; appointment.AppointmentConfirmedBySender = true; appointment.AppointmentConfirmedByReceiver = false; calendar1.Appointments.Add(appointment); calendar2.Appointments.Add(appointment); SystemEmail("You have a new appointment request on Xchange (http://languagexchange.azurewebsites.net/). Login to view and confirm.", user2email); db.Appointments.Add(appointment); db.SaveChanges(); }
public static void DeleteAppt(int apptId, string email) { using (var db = new LanguageExchangeModel()) { var user = db.UserAccounts.Where(u => u.UserEmail == email).FirstOrDefault(); var userId = user.UserIdNumber; var calendar = db.AppointmentCalendars.Where(c => c.UserId == userId).FirstOrDefault(); var appt = calendar.Appointments.Where(a => a.AppointmentId == apptId).FirstOrDefault(); if (appt == null) { return; } appt.Cancelled = true; if (appt.WhoCancelled == 0) { appt.WhoCancelled = userId; } if (appt.UserId1 == userId) { appt.UserId1 = -1; } if (appt.UserId2 == userId) { appt.UserId2 = -1; } if (appt.UserId1 == -1 && appt.UserId2 == -1) { db.Appointments.Remove(appt); } db.SaveChanges(); } }
public static void DeleteMessage(int messageId, string email) { using (var db = new LanguageExchangeModel()) { var user = db.UserAccounts.Where(u => u.UserEmail == email).FirstOrDefault(); var userId = user.UserIdNumber; var inbox = db.EmailInboxes.Where(i => i.UserId == userId).FirstOrDefault(); var message = inbox.Messages.Where(m => m.MessageId == messageId).FirstOrDefault(); if (message == null) { return; } if (message.InboxId1 == userId) { message.InboxId1 = -1; } if (message.InboxId2 == userId) { message.InboxId2 = -1; } if (message.InboxId1 == -1 && message.InboxId2 == -1) { db.Messages.Remove(message); } db.SaveChanges(); } }
public static void DeletePrefs (UserPreferences prefs) { using (var db = new LanguageExchangeModel()) { db.UserPreferences.Remove(prefs); db.SaveChanges(); } }
public static void MessageRead(int id) { var db = new LanguageExchangeModel(); var message = db.Messages.Where(i => i.MessageId == id).FirstOrDefault(); message.Read = true; db.SaveChanges(); }
public static UserPreferences ShowUserPreferences (int userId) { var db = new LanguageExchangeModel(); var user = db.UserAccounts.Where(u => u.UserIdNumber == userId).FirstOrDefault(); var pref = db.UserPreferences.Where(i => i.UserId == userId).FirstOrDefault(); if (pref == null) { pref = new UserPreferences(); pref.UserPreferencesName = user.UserFirstName + " " + user.UserLastName; pref.UserId = userId; db.UserPreferences.Add(pref); db.SaveChanges(); } return pref; }
public static void sendMessage(Messages UpdatedMessage) { using (var db = new LanguageExchangeModel()) { var message = db.Messages.Where(m => m.MessageId == UpdatedMessage.MessageId).FirstOrDefault(); if (message == null) { return; } var originalMessage = message; message.MessageHeader = UpdatedMessage.MessageHeader; message.MessageContent = UpdatedMessage.MessageContent; message.MessageTimeStamp = DateTime.Now; db.Entry(originalMessage).CurrentValues.SetValues(message); db.SaveChanges(); } }
public static void CreateMessage(int id, string email, Messages UpdatedMessage) { var db = new LanguageExchangeModel(); var userFrom = db.UserAccounts.Where(u => u.UserEmail == email).FirstOrDefault(); var userFromId = userFrom.UserIdNumber; var userTo = db.UserAccounts.Where(u => u.UserIdNumber == id).FirstOrDefault(); var userToId = userTo.UserIdNumber; var user2email = userTo.UserEmail; var inboxFrom = db.EmailInboxes.Where(i => i.UserId == userFromId).FirstOrDefault(); if (inboxFrom == null) { inboxFrom = new EmailInbox(); inboxFrom.UserId = userFromId; db.EmailInboxes.Add(inboxFrom); db.SaveChanges(); } var inboxTo = db.EmailInboxes.Where(i => i.UserId == userToId).FirstOrDefault(); if (inboxTo == null) { inboxTo = new EmailInbox(); inboxTo.UserId = userToId; db.EmailInboxes.Add(inboxTo); db.SaveChanges(); } var message = new Messages(); message.InboxId1 = userFromId; message.InboxId2 = userToId; var userFromPrefs = db.UserPreferences.Where(p => p.UserId == userFromId).FirstOrDefault(); if (userFromPrefs == null) { message.MessageFrom = userFrom.UserFirstName; } message.MessageFrom = userFromPrefs.UserPreferencesName; var userToPrefs = db.UserPreferences.Where(p => p.UserId == userToId).FirstOrDefault(); if (userToPrefs == null) { message.MessageTo = userTo.UserFirstName; } message.MessageTo = userToPrefs.UserPreferencesName; message.MessageHeader = UpdatedMessage.MessageHeader; message.MessageContent = UpdatedMessage.MessageContent; message.MessageTimeStamp = DateTime.Now; inboxFrom.Messages.Add(message); inboxTo.Messages.Add(message); db.Messages.Add(message); SystemEmail("You have a new message on Xchange (http://languagexchange.azurewebsites.net/). Login to view.", user2email); db.SaveChanges(); }
public static EmailInbox ShowEmailInbox(string email) { var db = new LanguageExchangeModel(); var user = db.UserAccounts.Where(u => u.UserEmail == email).FirstOrDefault(); var userId = user.UserIdNumber; var inbox = db.EmailInboxes.Where(i => i.UserId == userId).FirstOrDefault(); if (inbox == null) { inbox = new EmailInbox(); inbox.UserId = userId; db.EmailInboxes.Add(inbox); db.SaveChanges(); } return inbox; }
public static AppointmentCalendar ShowAppointmentCalendar(string email) { var db = new LanguageExchangeModel(); var user = db.UserAccounts.Where(u => u.UserEmail == email).FirstOrDefault(); var userId = user.UserIdNumber; var calendar = db.AppointmentCalendars.Where(c => c.UserId == userId).FirstOrDefault(); if (calendar == null) { calendar = new AppointmentCalendar(); calendar.UserId = userId; db.AppointmentCalendars.Add(calendar); db.SaveChanges(); } return calendar; }
public static UserTrackingDetails ShowTrackingDetails(string email) { var db = new LanguageExchangeModel(); var userAccount = db.UserAccounts.Where(u => u.UserEmail == email).FirstOrDefault(); var userId = userAccount.UserIdNumber; var user = db.UserTrackingDetails.Where(i => i.UserId == userId).FirstOrDefault(); if (user == null) { user = new UserTrackingDetails(); user.UserId = userId; db.UserTrackingDetails.Add(user); db.SaveChanges(); } return user; }
public static void UpdateTrackingDetails(string email) { using (var db = new LanguageExchangeModel()) { var userAccount = db.UserAccounts.Where(u => u.UserEmail == email).FirstOrDefault(); if (userAccount == null) { return; } var userId = userAccount.UserIdNumber; var user = db.UserTrackingDetails.Where(u => u.UserId == userId).FirstOrDefault(); if (user == null) { return; } var calendar = db.AppointmentCalendars.Where(c => c.UserId == userId).FirstOrDefault(); if (calendar == null) { return; } var hoursTaught = 0.0; var hoursLearned = 0.0; foreach (var appt in calendar.Appointments) { if (appt == null) { return; } if (IsAppointmentPast(appt.AppointmentEndTime) == true && appt.AppointmentTeacher == userId) { var apptLength = AppointmentLength(appt.AppointmentStartTime, appt.AppointmentEndTime); hoursTaught += apptLength; } else if (IsAppointmentPast(appt.AppointmentEndTime) == true && appt.AppointmentLearner == userId) { var apptLength = AppointmentLength(appt.AppointmentStartTime, appt.AppointmentEndTime); hoursLearned += apptLength; } } user.UserHoursTaught = Math.Round(hoursTaught); user.UserHoursLearned = Math.Round(hoursLearned); var teacherTotal = 0.0; var teacherCount = 0; var learnerTotal = 0.0; var learnerCount = 0; foreach (var appt in calendar.Appointments) { if (appt.TeacherRating > 0) { teacherTotal += appt.TeacherRating; teacherCount++; } if (appt.LearnerRating > 0) { learnerTotal += appt.LearnerRating; learnerCount++; } } var learnerRating = 0.0; var teacherRating = 0.0; if (teacherCount != 0) { teacherRating = teacherTotal / teacherCount; } if (learnerCount != 0) { learnerRating = learnerTotal / learnerCount; } user.UserTeacherRating = teacherRating; user.UserLearnerRating = learnerRating; db.SaveChanges(); } }
public static void ConfirmAppointment (int id) { var db = new LanguageExchangeModel(); var appointment = db.Appointments.Where(a => a.AppointmentId == id).FirstOrDefault(); appointment.AppointmentConfirmedByReceiver = true; db.SaveChanges(); }
public static IEnumerable<Messages> ShowInboxMessages (string email) { var db = new LanguageExchangeModel(); var user = db.UserAccounts.Where(u => u.UserEmail == email).FirstOrDefault(); var userId = user.UserIdNumber; var inbox = db.EmailInboxes.Where(i => i.UserId == userId).FirstOrDefault(); if (inbox == null) { inbox = new EmailInbox(); inbox.UserId = userId; } var messages = inbox.Messages.Where(m => m.InboxId2 == userId); if (messages == null) { var message = new Messages(); message.InboxId1 = 1; message.InboxId2 = userId; message.MessageHeader = "Welcome to Xchange!"; message.MessageContent = "Welcome to Xchange! We're glad you're here. Fill out your profile details and search for other language learners to get started."; message.MessageTimeStamp = DateTime.Now; message.MessageTo = user.UserFirstName; message.MessageFrom = "Xchange Team"; inbox.Messages.Add(message); db.Messages.Add(message); db.SaveChanges(); } var sortedMessages = messages.OrderByDescending(x => x.MessageTimeStamp); return sortedMessages; }
public static void UpdateUserPreferences(string email, UserPreferences updatedPreferences) { using (var db = new LanguageExchangeModel()) { var user = db.UserAccounts.Where(u => u.UserEmail == email).FirstOrDefault(); var pref = db.UserPreferences.Where(p => p.UserId == user.UserIdNumber).FirstOrDefault(); if (pref == null) { return; } var originalPrefs = pref; pref.UserPrimaryTeachingLanguage = updatedPreferences.UserPrimaryTeachingLanguage; pref.UserPrimaryLearningLanguage = updatedPreferences.UserPrimaryLearningLanguage; pref.UserOtherTeachingLanguage = updatedPreferences.UserOtherTeachingLanguage; pref.UserOtherLearningLanguage = updatedPreferences.UserOtherLearningLanguage; pref.UserSkype = updatedPreferences.UserSkype; pref.UserPrefersTalkOrText = updatedPreferences.UserPrefersTalkOrText; pref.UserTimeZone = updatedPreferences.UserTimeZone; pref.UserPhoto = updatedPreferences.UserPhoto; pref.UserDescription = updatedPreferences.UserDescription; pref.UserPreferencesName = updatedPreferences.UserPreferencesName; pref.UserCountry = updatedPreferences.UserCountry; db.Entry(originalPrefs).CurrentValues.SetValues(pref); db.SaveChanges(); } }
public static void Rate(string email, int rating, int apptId) { using (var db = new LanguageExchangeModel()) { var user = db.UserAccounts.Where(u => u.UserEmail == email).FirstOrDefault(); var userId = user.UserIdNumber; var appt = db.Appointments.Where(a => a.AppointmentId == apptId).FirstOrDefault(); if (appt.AppointmentTeacher == userId && rating > 0 && rating < 5) { appt.LearnerRating = rating; } if (appt.AppointmentLearner == userId && rating > 0 && rating < 5) { appt.TeacherRating = rating; } db.SaveChanges(); } }
public static UserPreferences ShowMyUserPreferences(string email) { var db = new LanguageExchangeModel(); var userAccount = db.UserAccounts.Where(u => u.UserEmail == email).FirstOrDefault(); var userId = userAccount.UserIdNumber; var pref = db.UserPreferences.Where(i => i.UserId == userId).FirstOrDefault(); if (pref == null) { pref = new UserPreferences(); pref.UserPreferencesName = userAccount.UserFirstName + " " + userAccount.UserLastName; pref.UserId = userId; pref.UserDescription = "Something interesting about me!"; pref.LastLogin = DateTime.Now; db.UserPreferences.Add(pref); db.SaveChanges(); } return pref; }
public static void UpdateLastLoggedIn (string email) { using (var db = new LanguageExchangeModel()) { var user = db.UserAccounts.Where(u => u.UserEmail == email).FirstOrDefault(); if (user == null) { return; } var userId = user.UserIdNumber; var prefs = db.UserPreferences.Where(p => p.UserId == userId).FirstOrDefault(); if (prefs == null) { prefs = new UserPreferences(); prefs.UserId = userId; } prefs.LastLogin = DateTime.Now; db.SaveChanges(); } }