Esempio n. 1
0
        private string SignInAdmin(LoginLogicModel model)
        {
            using (var context = new AASDBContext())
            {
                var admin = context.Admins.SingleOrDefault(a => a.Username == model.Username);
                if (admin == null)
                {
                    throw new LoginErrorException();
                }

                if (string.IsNullOrWhiteSpace(admin.Salt))
                {
                    admin.Salt     = PasswordHashProvider.GenerateSalt();
                    admin.Password = PasswordHashProvider.ComputePasswordHash(admin.Password.Trim(), admin.Salt);
                    context.SaveChanges();
                }

                var hash = PasswordHashProvider.ComputePasswordHash(model.Password, admin.Salt);

                if (admin.Password != hash)
                {
                    throw new LoginErrorException();
                }

                return(ComposeToken(model.Username, UserType.AdminUserType));
            }
        }
Esempio n. 2
0
        private string SignInStudent(LoginLogicModel model)
        {
            using (var context = new AASDBContext())
            {
                var student = context.Students.SingleOrDefault(a => a.Email == model.Username);
                if (student == null)
                {
                    throw new LoginErrorException();
                }

                if (string.IsNullOrWhiteSpace(student.Salt))
                {
                    student.Salt     = PasswordHashProvider.GenerateSalt();
                    student.Password = PasswordHashProvider.ComputePasswordHash(student.Password.Trim(), student.Salt);
                    context.SaveChanges();
                }

                var hash = PasswordHashProvider.ComputePasswordHash(model.Password, student.Salt);

                if (student.Password != hash)
                {
                    throw new LoginErrorException();
                }

                return(ComposeToken(model.Username, UserType.StudentUserType));
            }
        }
Esempio n. 3
0
        public bool CheckQrCode(int studentId, int classId, string qrCode)
        {
            using (var context = new AASDBContext())
            {
                var result = false;
                var ticket = context.Tickets.SingleOrDefault(t => t.StudentId == studentId && t.ClassId == classId);
                if (ticket != null)
                {
                    result = ticket.Verify(qrCode);
                    if (result)
                    {
                        var classObj = context.Classes.Include("ClassSessions").FirstOrDefault(c => c.Id == classId);
                        var now      = DateTime.Now;
                        var dateNow  = new DateTime(now.Year, now.Month, now.Day, 0, 0, 0);
                        if (dateNow < classObj.StartDate || dateNow > classObj.EndDate)
                        {
                            return(false);
                        }
                        var currentWeekDay = dateNow.DayOfWeek.ToString().Substring(0, 3).ToLower();
                        var session        = classObj.ClassSessions.FirstOrDefault(s => s.Weekday.ToLower() == currentWeekDay);
                        if (session != null)
                        {
                            var currentTime = new TimeSpan(now.Hour, now.Minute, now.Second);
                            var startTime   = session.StartTime;
                            var endTime     = (classObj.ExcusedTime != null) ? startTime + classObj.ExcusedTime.Value : session.EndTime;
                            if (session.StartTime > currentTime || session.EndTime < currentTime)
                            {
                                return(false);
                            }

                            var dateTimeOffsetNow = DateTimeOffset.Now;

                            var sessionId = session.Id;
                            var record    = context.AttendanceRecords.FirstOrDefault(r => r.SessionId == sessionId &&
                                                                                     r.CheckinTime <= dateTimeOffsetNow);
                            if (record == null)
                            {
                                record = new AttendanceRecord
                                {
                                    CheckinTime = dateTimeOffsetNow,
                                    RecordDate  = DateTime.Now,
                                    SessionId   = session.Id,
                                    StudentId   = studentId,
                                    TicketId    = ticket.Id
                                };
                                context.AttendanceRecords.Add(record);
                                context.SaveChanges();
                            }
                        }
                        else
                        {
                            return(false);
                        }
                    }
                }

                return(result);
            }
        }
Esempio n. 4
0
 public void DeleteAdmin(string username)
 {
     using (var context = new AASDBContext())
     {
         var admin = GetAdmin(username);
         admin.Status = AdminStatus.Deleted;
         context.SaveChanges();
     }
 }
Esempio n. 5
0
 public int AddClass(Class classObj)
 {
     using (var context = new AASDBContext())
     {
         context.Classes.Add(classObj);
         context.SaveChanges();
         return(classObj.Id);
     }
 }
Esempio n. 6
0
 public int AddStudent(Student student)
 {
     using (var context = new AASDBContext())
     {
         student.Status = StudentStatus.Active;
         context.Students.Add(student);
         context.SaveChanges();
         return(student.Id);
     }
 }
Esempio n. 7
0
        public void UpdateClass(Class classObj)
        {
            using (var context = new AASDBContext())
            {
                if (!context.Classes.Any(c => c.Id == classObj.Id))
                {
                    throw new NotFoundException();
                }

                var existing = context.Classes.Include("ClassSessions").Single(c => c.Id == classObj.Id);

                existing.Name          = classObj.Name;
                existing.ProfessorName = classObj.ProfessorName;
                existing.StartDate     = classObj.StartDate;
                existing.EndDate       = classObj.EndDate;
                existing.ExcusedTime   = classObj.ExcusedTime;

                var newSessionList = classObj.ClassSessions.ToList();

                foreach (var session in existing.ClassSessions.ToArray())
                {
                    var newSession = newSessionList.SingleOrDefault(s => s.Id == session.Id);

                    if (newSession != null)
                    {
                        session.EndTime   = newSession.EndTime;
                        session.StartTime = newSession.StartTime;
                        session.Weekday   = newSession.Weekday;
                        session.Room      = newSession.Room;
                        newSessionList.Remove(newSession);
                    }
                    else
                    {
                        context.ClassSessions.Remove(session);
                    }
                }

                foreach (var newSession in newSessionList)
                {
                    //This code cause "Multiplicity constraint violated" exception
                    //existing.ClassSessions.Add(newSession);
                    existing.ClassSessions.Add(new ClassSession
                    {
                        Class     = existing,
                        ClassId   = existing.Id,
                        EndTime   = newSession.EndTime,
                        StartTime = newSession.StartTime,
                        Room      = newSession.Room,
                        Weekday   = newSession.Weekday
                    });
                }

                context.SaveChanges();
            }
        }
Esempio n. 8
0
        public void UpdateAdmin(Admin admin)
        {
            using (var context = new AASDBContext())
            {
                var existing = GetAdmin(admin.Username);

                if (existing == null)
                {
                    throw new NotFoundException();
                }

                existing.AdminName = admin.AdminName;

                context.SaveChanges();
            }
        }
Esempio n. 9
0
 public void DeleteClass(int id)
 {
     using (var context = new AASDBContext())
     {
         var classObj = context.Classes.FirstOrDefault(c => c.Id == id);
         if (classObj != null)
         {
             classObj.IsArchived = true;
             context.SaveChanges();
         }
         else
         {
             throw new NotFoundException();
         }
     }
 }
Esempio n. 10
0
 public void DropClass(int studentId, int classId)
 {
     using (var context = new AASDBContext())
     {
         var ticket = context.Tickets.SingleOrDefault(t => t.StudentId == studentId && t.ClassId == classId);
         if (ticket != null)
         {
             var tid     = ticket.Id;
             var records = context.AttendanceRecords.Where(r => r.TicketId == tid);
             foreach (var r in records)
             {
                 context.AttendanceRecords.Remove(r);
             }
             context.Tickets.Remove(ticket);
             context.SaveChanges();
         }
     }
 }
Esempio n. 11
0
        public void AddAdmin(Admin admin)
        {
            using (var context = new AASDBContext())
            {
                admin.Status = AdminStatus.Active;
                var userName = admin.Username;
                var existing = context.Admins.Where(a => a.Status < AdminStatus.TrumCuoi)
                               .SingleOrDefault(a => a.Username == userName);
                if (existing != null)
                {
                    existing.AdminName = admin.AdminName;
                    existing.Status    = admin.Status;
                    existing.Salt      = admin.Salt;
                    existing.Password  = admin.Password;
                }
                else
                {
                    context.Admins.Add(admin);
                }

                context.SaveChanges();
            }
        }
Esempio n. 12
0
        public Ticket RegisterClass(int studentId, int classId)
        {
            using (var context = new AASDBContext())
            {
                var ticket = context.Tickets.SingleOrDefault(t => t.StudentId == studentId && t.ClassId == classId);
                if (ticket != null)
                {
                    return(ticket);
                }

                ticket = new Ticket
                {
                    StudentId = studentId,
                    ClassId   = classId
                };

                ticket.GenerateQrCode();

                context.Tickets.Add(ticket);
                context.SaveChanges();

                return(ticket);
            }
        }