// this class will be frequently referenced // it should be stateless, don't put any fields here // All method in this class is self contained. // Add as many as methods as you wish here. // Don't delete any method here! // // public Boolean addAdminAccount(string email, string password) { using (Youth_Center_DB_Conn db = new Youth_Center_DB_Conn()) { try { var duplicatedAccount = from user in db.ProjectLaucherSets where user.emailAddr == email select user; if (!duplicatedAccount.Any()) { ProjectLaucherSet_AdminSet admin = new ProjectLaucherSet_AdminSet(); admin.ProjectLaucherSet.emailAddr = email; admin.ProjectLaucherSet.role = "Admin"; admin.password = password; db.ProjectLaucherSet_AdminSet.Add(admin); db.SaveChanges(); return true; } else return false; } catch (Exception ex) { Debug.WriteLine(ex.StackTrace); return false; } } }
// // public Boolean editActivity(int activityId, DateTime startTime, DateTime endTime, string location, string description) { using (Youth_Center_DB_Conn db = new Youth_Center_DB_Conn()) { try { var activityList = from ac in db.ActivitySets where ac.Id == activityId select ac; if (activityList.Any()) { ActivitySet activity = activityList.First(); activity.startTime = startTime; activity.endTime = endTime; activity.location = location; activity.description = description; db.SaveChanges(); return true; } } catch (Exception ex) { Debug.WriteLine(ex.StackTrace); } return false; } }
// // public Boolean deleteProject(int projectId) { using (Youth_Center_DB_Conn db = new Youth_Center_DB_Conn()) { try { var projects = from p in db.ProjectSets where p.Id == projectId select p; if (projects.Any()) { ProjectSet project = projects.First(); List<ActivitySet> activityList = project.ActivitySets.ToList(); foreach (ActivitySet ac in activityList) { this.deleteActivity(ac.Id); } db.Contacts.Remove(project.Contact); db.SaveChanges(); project.TargetGroupSets.Clear(); project.ActivitySets.Clear(); project.Volunteers.Clear(); db.ProjectSets.Remove(project); db.SaveChanges(); return true; } } catch (Exception ex) { Debug.WriteLine(ex.StackTrace); } return false; } }
// // public Boolean deleteJob(int jobId) { using (Youth_Center_DB_Conn db = new Youth_Center_DB_Conn()) { try { List<JobSet> jobList = (from j in db.JobSets where j.Id == jobId select j).ToList(); if (jobList.Any()) { JobSet job = jobList.First(); db.JobSets.Remove(job); db.SaveChanges(); return true; } else return false; } catch (Exception ex) { Debug.WriteLine(ex.StackTrace); return false; } } }
// // public Boolean deleteActivity(int activityId) { using (Youth_Center_DB_Conn db = new Youth_Center_DB_Conn()) { try { var activityList = from ac in db.ActivitySets where ac.Id == activityId select ac; if (activityList.Any()) { ActivitySet activity = activityList.First(); List<JobSet> jobList = activity.JobSets.ToList(); foreach (JobSet j in jobList) { db.JobSets.Remove(j); } db.ActivitySets.Remove(activity); db.SaveChanges(); return true; } } catch (Exception ex) { Debug.WriteLine(ex.StackTrace); } return false; } }
// // public Boolean eidtContact(int projectId, string name, string email, string phone, string organization, string description) { using (Youth_Center_DB_Conn db = new Youth_Center_DB_Conn()) { try { var contactList = from c in db.Contacts where c.ProjectSet.Id == projectId select c; if (contactList.Any()) { Contact contact = contactList.First(); contact.name = name; contact.emailAddr = email; contact.phoneNumber = phone; contact.organization = organization; contact.organizationDescription = description; db.SaveChanges(); return true; } } catch (Exception ex) { Debug.WriteLine(ex.StackTrace); } return false; } }
// // public int resetPassword(string email, string oldPassword, string newPassword) { using (Youth_Center_DB_Conn db = new Youth_Center_DB_Conn()) { try { var users = from u in db.ProjectLaucherSets where u.emailAddr == email select u; if (users.Any()) { ProjectLaucherSet user = users.First(); if (user.role == "External_User") { if (user.ProjectLaucherSet_ExtenalUser.password == oldPassword) { user.ProjectLaucherSet_ExtenalUser.password = newPassword; db.SaveChanges(); // account password has been resetted return 1; } else // account password donest match return 0; } else if (user.role == "???") { } } else // account doest exist return -1; } catch (Exception ex) { Debug.WriteLine(ex.StackTrace); } return -1; } }
// // public int createExternalAccount(string email, string password, string name, string phone, string organization, string description) { using (Youth_Center_DB_Conn db = new Youth_Center_DB_Conn()) { try { var duplicatedEmail = from p in db.ProjectLaucherSets where p.emailAddr.ToUpper().Equals(email.ToUpper()) select p; if (!duplicatedEmail.Any()) { ProjectLaucherSet laucher = new ProjectLaucherSet(); laucher.emailAddr = email; laucher.role = "External_User"; ProjectLaucherSet_ExtenalUser externalUser = new ProjectLaucherSet_ExtenalUser(); externalUser.name = name; externalUser.password = password; externalUser.phoneNumber = phone; externalUser.organization = organization; externalUser.organizationDescription = description; externalUser.ProjectLaucherSet = laucher; externalUser.isDisabled = true; db.ProjectLaucherSets.Add(laucher); externalUser = db.ProjectLaucherSet_ExtenalUser.Add(externalUser); db.SaveChanges(); return externalUser.Id; } } catch (Exception ex) { Debug.WriteLine(ex.StackTrace); } return -1; } }
// // public Boolean createJob(string scopeName, string detail, int number, int activityId) { using (Youth_Center_DB_Conn db = new Youth_Center_DB_Conn()) { try { List<ActivitySet> activityList = (from a in db.ActivitySets where a.Id == activityId select a).ToList(); if (activityList.Any()) { var duplicatedJobScope = from j in db.JobSets where j.ActivitySetId == activityId && j.JobScopeSet.scopeName == scopeName select j; if (!duplicatedJobScope.Any()) { ActivitySet activity = activityList.First(); JobScopeSet jobScope = (from js in db.JobScopeSets where js.scopeName == scopeName select js).ToList().First(); JobSet job = new JobSet(); job.JobScopeSet = jobScope; job.ActivitySet = activity; job.jobDetail = detail; job.labors = number; db.JobSets.Add(job); db.SaveChanges(); return true; } else { Debug.WriteLine("current activity already has the job scope"); return false; } } else { Debug.WriteLine("cannot find current project"); return false; } } catch (Exception ex) { Debug.WriteLine(ex.StackTrace); return false; } } }
// // public int createActivity(int projectId, DateTime startTime, DateTime endTime, string location, string description) { using(Youth_Center_DB_Conn db = new Youth_Center_DB_Conn()) { try { var projects = from p in db.ProjectSets where p.Id == projectId select p; if (projects.Any()) { ActivitySet activity = new ActivitySet(); activity.startTime = startTime; activity.endTime = endTime; activity.location = location; activity.description = description; activity.ProjectSet = projects.First(); activity = db.ActivitySets.Add(activity); db.SaveChanges(); return activity.Id; } } catch (Exception ex) { Debug.WriteLine(ex.StackTrace); } return -1; } }
// // public int createContact(int projectId, string name, string email, string phone, string organization, string description) { using (Youth_Center_DB_Conn db = new Youth_Center_DB_Conn()) { try { var projectList = from p in db.ProjectSets where p.Id == projectId select p; if (projectList.Any()) { Contact contact = new Contact(); contact.name = name; contact.emailAddr = email; contact.phoneNumber = phone; contact.organization = organization; contact.organizationDescription = description; contact.ProjectSet = projectList.First(); contact = db.Contacts.Add(contact); db.SaveChanges(); return contact.Id; } } catch (Exception ex) { Debug.WriteLine(ex.StackTrace); } return -1; } }
public Boolean submitProject(int projectId) { using (Youth_Center_DB_Conn db = new Youth_Center_DB_Conn()) { try { ProjectSet project = (from p in db.ProjectSets where p.Id == projectId select p).First(); project.status = "beforeApproved"; project.createDate = DateTime.Now; db.SaveChanges(); return true; } catch (Exception ex) { Debug.WriteLine(ex.StackTrace); } return false; } }
// // public Boolean StudentPersonalInfo(int userId, string faculty, List<string> jobScopeList, List<string> targets) { using (Youth_Center_DB_Conn db = new Youth_Center_DB_Conn()) { try { var students = from user in db.ProjectLaucherSets where user.Id == userId select user.ProjectLaucherSet_Student; if (students.Any()) { ProjectLaucherSet_Student student = students.First(); student.department = faculty; List<JobScopeSet> jobScopes = (from js in db.JobScopeSets where jobScopeList.Contains(js.scopeName) select js).ToList(); student.JobScopeSets.Clear(); student.JobScopeSets = jobScopes; List<TargetGroupSet> targetGroups = (from t in db.TargetGroupSets where targets.Contains(t.targetGroup) select t).ToList(); student.TargetGroupSets.Clear(); student.TargetGroupSets = targetGroups; student.hasPersonalInfo = true; db.SaveChanges(); return true; } } catch (Exception ex) { Debug.WriteLine(ex.StackTrace); } return false; } }
// // public string resetPassword(string email) { using (Youth_Center_DB_Conn db = new Youth_Center_DB_Conn()) { try { var users = from u in db.ProjectLaucherSets where u.emailAddr == email select u; if (users.Any()) { ProjectLaucherSet user = users.First(); if (user.role == "External_User") { int hashcode1 = DateTime.Now.GetHashCode(); int hashcode2 = email.GetHashCode(); string newPassword = Convert.ToString(hashcode1 + hashcode2); user.ProjectLaucherSet_ExtenalUser.password = newPassword; db.SaveChanges(); return newPassword; } else if (user.role == "???") { } } } catch (Exception ex) { Debug.WriteLine(ex.StackTrace); } return null; } }
// // public Boolean editJob(int jobId, string scopeName, int larbor, string detail) { using (Youth_Center_DB_Conn db = new Youth_Center_DB_Conn()) { try { List<JobSet> jobList = (from j in db.JobSets where j.Id == jobId select j).ToList(); JobScopeSet jobScope = (from js in db.JobScopeSets where js.scopeName == scopeName select js).First(); if (jobList.Any()) { JobSet job = jobList.First(); job.JobScopeId = jobScope.Id; job.JobScopeSet = jobScope; job.jobDetail = detail; job.labors = larbor; db.SaveChanges(); return true; } else return false; } catch (Exception ex) { Debug.WriteLine(ex.StackTrace); return false; } } }
// // public Int32 createProjectBase(int userId, string name, string description, DateTime startDate, DateTime endDate, string frequence, List<string> targ_strs) { using(Youth_Center_DB_Conn db = new Youth_Center_DB_Conn()) { try { List<TargetGroupSet> targ = (from t in db.TargetGroupSets where targ_strs.Contains(t.targetGroup) select t).ToList(); ProjectSet project = new ProjectSet(); project.projectName = name; project.projectDescription = description; project.TargetGroupSets = targ; project.startDate = startDate; project.endDate = endDate; if (frequence != "regular") project.isRegular = false; else project.isRegular = true; project.status = "beforeSubmitted"; //"beforeApproved"; var lauchers = from pl in db.ProjectLaucherSets where pl.Id == userId select pl; if(lauchers.Any()) { project.ProjectLaucherSet = lauchers.First(); project = db.ProjectSets.Add(project); db.SaveChanges(); return project.Id; } } catch(Exception ex) { Debug.WriteLine(ex.StackTrace); } return -1; } }
// // public int editProjectBase(int currentProjectId, string name, string description, DateTime startDate, DateTime endDate, string frequence, List<string> targ_strs) { using (Youth_Center_DB_Conn db = new Youth_Center_DB_Conn()) { try { List<TargetGroupSet> targ = (from t in db.TargetGroupSets where targ_strs.Contains(t.targetGroup) select t).ToList(); var projectList = from p in db.ProjectSets where p.Id == currentProjectId select p; if (projectList.Any()) { ProjectSet project = projectList.First(); project.projectName = name; project.projectDescription = description; project.TargetGroupSets.Clear(); project.TargetGroupSets = targ; if (frequence != "regular") project.isRegular = false; else project.isRegular = true; project.startDate = startDate; project.endDate = endDate; project.status = "beforeSubmitted"; db.SaveChanges(); return project.Id; } } catch (Exception ex) { Debug.WriteLine(ex.StackTrace); } return -1; } }
// not local student account, create one and return userId // local student account exist, return userId public int createStudentAccount(String email) { using (Youth_Center_DB_Conn db = new Youth_Center_DB_Conn()) { try { var dupicatedAccount = from user in db.ProjectLaucherSets where user.emailAddr.ToUpper().Equals(email.ToUpper()) select user; if (!dupicatedAccount.Any()) { ProjectLaucherSet user = new ProjectLaucherSet(); user.emailAddr = email; user.role = "Student"; ProjectLaucherSet_Student student = new ProjectLaucherSet_Student(); student.hasPersonalInfo = false; student.canPost = true; student.ProjectLaucherSet = user; db.ProjectLaucherSet_Student.Add(student); db.SaveChanges(); return user.Id; } else { return dupicatedAccount.First().Id; } } catch (Exception ex) { Debug.WriteLine(ex.StackTrace); } return -1; } }
// // public int addProjectView(int projectId) { using (Youth_Center_DB_Conn db = new Youth_Center_DB_Conn()) { try { var projects = from p in db.ProjectSets where p.Id == projectId select p; if (projects.Any()) { projects.First().viewTimes += 1; db.SaveChanges(); return projects.First().viewTimes; } } catch (Exception ex) { Debug.WriteLine(ex.StackTrace); } return -1; } }
// // public Boolean addStudentInfomation(int userId, string name) { using(Youth_Center_DB_Conn db = new Youth_Center_DB_Conn()) { try { var students = from user in db.ProjectLaucherSets where user.Id == userId select user.ProjectLaucherSet_Student; if (students.Any()) { students.First().name = name; db.SaveChanges(); return true; } } catch(Exception ex) { Debug.WriteLine(ex.StackTrace); } return false; } }