Exemplo n.º 1
0
 public static object Login(string name, string password)
 {
     using (Entities e = new Entities())
     {
         try
         {
             user user = e.users.FirstOrDefault(u => u.user_name == name && u.user_password == password);
             if (user != null)
             {
                 if (user.status == 1)
                 {
                     var te = e.teachers.FirstOrDefault(t => t.userId == user.user_id);
                     return(TeacherCasting.TeacherToDTO(te));
                 }
                 if (user.status == 2)
                 {
                     return(StudentCasting.StudentToDTO(e.students.FirstOrDefault(s => s.userId == user.user_id)));
                 }
             }
             throw new Exception("UserName or Password are not correct");
         }
         catch (Exception ex)
         {
             throw ex;
         }
     }
 }
Exemplo n.º 2
0
        public void UpdateTeacher(TeacherDTO t)
        {
            using (Entities e = new Entities())
            {
                teacher teacher = e.teachers.FirstOrDefault(te => te.userId == t.userId);
                //todo updete
                teacher = TeacherCasting.TeacherToDAL(t);



                e.SaveChanges();
            }
        }
Exemplo n.º 3
0
 //public List<TeacherDTO> GetTeachers()
 //{
 //    var teachers = e.users.OfType<teacher>().ToList();
 //    return TeacherCasting.TeachersToDTO(teachers);
 //}
 //public TeacherDTO GetTeacher(string name, string password)
 //{
 //    var teachers = e.users.OfType<teacher>().ToList();
 //    return TeacherCasting.TeacherToDTO(teachers.FirstOrDefault(t => t.user_name == name && t.user_password == password));
 //}
 public static TeacherDTO GetTeacherByUserId(int userId)
 {
     using (Entities e = new Entities())
     {
         try
         {
             var teacher = e.teachers.FirstOrDefault(t => t.userId == userId);
             if (teacher != null)
             {
                 return(TeacherCasting.TeacherToDTO(teacher));
             }
             throw new Exception("UserId is not exists");
         }
         catch (Exception ex)
         {
             throw ex;
         }
     }
 }
Exemplo n.º 4
0
 public static Object AddUser <T>(T user)
 {
     using (Entities e = new Entities())
     {
         try
         {
             if (user is TeacherDTO teacher)
             {
                 if (e.users.FirstOrDefault(t => t.user_name == teacher.user.user_name || t.user_id_number == teacher.user.user_id_number) != null)
                 {
                     throw new Exception("user name is unique");
                 }
                 else
                 {
                     e.users.Add(UserCasting.UserToDAL(teacher.user));
                     var t = e.teachers.Add(TeacherCasting.TeacherToDAL(teacher));
                     e.SaveChanges();
                     return(TeacherCasting.TeacherToDTO(t));
                 }
             }
             else if (user is StudentDTO student)
             {
                 if (e.users.FirstOrDefault(t => t.user_name == student.user.user_name || t.user_id_number == student.user.user_id_number) != null)
                 {
                     throw new Exception("user name is unique");
                 }
                 else
                 {
                     e.users.Add(UserCasting.UserToDAL(student.user));
                     var s = e.students.Add(StudentCasting.StudentToDAL(student));
                     e.SaveChanges();
                     return(StudentCasting.StudentToDTO(s));
                 }
             }
             return(null);
         }
         catch (Exception ex)
         {
             throw ex;
         }
     }
 }