public bool isRoomFree(Reservation reservation)
        {
            using (ai_databaseEntities de = new ai_databaseEntities())
            {
                var v = de.Reservation.Where(a => a.roomId.Equals(reservation.roomId)).ToList();

                foreach (Reservation reserv in v)
                {
                    if (reserv.Id != reservation.Id)
                    {
                        bool result    = false;
                        int  SScompare = reservation.dateFrom.CompareTo(reserv.dateFrom);
                        int  EScompare = reservation.dateTo.CompareTo(reserv.dateFrom);
                        int  SEcompare = reservation.dateFrom.CompareTo(reserv.dateTo);
                        if (SScompare < 0 && EScompare <= 0)
                        {
                            result = true;
                        }
                        if (SEcompare >= 0)
                        {
                            result = true;
                        }
                        if (!result)
                        {
                            return(result);
                        }
                    }
                }
                return(true);
            }
        }
        public ActionResult ViewReservations()
        {
            List <Reservation> reservations;

            using (ai_databaseEntities de = new ai_databaseEntities())
            {
                reservations = de.Reservation.ToList();
                foreach (Reservation item in reservations)
                {
                    item.SetRoomName();
                    item.SetSubjectName();
                    item.SetAddress();
                }
                if (de.User.Where(x => x.Id.Equals(LoggedOnUser.loggedOnUserID)).FirstOrDefault().isTeacher)
                {
                    ViewBag.type      = "teacher";
                    ViewBag.teacherId = LoggedOnUser.loggedOnUserID;
                }
                else
                {
                    ViewBag.type = "student";
                }
            }
            List <Reservation> sortedRes = reservations.OrderBy(o => o.dateFrom).ToList();

            return(View(sortedRes));
        }
        public ActionResult Edit(int id = 0)
        {
            ai_databaseEntities entity            = new ai_databaseEntities();
            Reservation         editedReservation = null;

            using (ai_databaseEntities de = new ai_databaseEntities())
            {
                editedReservation = de.Reservation.Where(x => x.Id.Equals(id)).FirstOrDefault();
            }
            #region set Reservation Date and time

            editedReservation.reservationDate = editedReservation.dateFrom;
            DateTime start = new DateTime(editedReservation.dateFrom.Year, editedReservation.dateFrom.Month, editedReservation.dateFrom.Day, editedReservation.dateFrom.Hour, editedReservation.dateFrom.Minute, 0);
            DateTime end   = new DateTime(editedReservation.dateTo.Year, editedReservation.dateTo.Month, editedReservation.dateTo.Day, editedReservation.dateTo.Hour, editedReservation.dateTo.Minute, 0);
            editedReservation.startTime = start;
            editedReservation.endTime   = end;

            #endregion
            var        getAllRoomsList = entity.Room.ToList();
            SelectList allRooms        = new SelectList(getAllRoomsList, "Id", "Name");
            ViewBag.getRooms = allRooms;

            var        getSubjects = entity.Subject.ToList();
            SelectList allSubjects = new SelectList(getSubjects, "Id", "Name");
            ViewBag.getSubjects = allSubjects;

            return(View(editedReservation));
        }
        public ActionResult AddReservation()
        {
            ai_databaseEntities entity = new ai_databaseEntities();
            var        getAllRoomsList = entity.Room.ToList();
            SelectList allRooms        = new SelectList(getAllRoomsList, "Id", "Name");

            ViewBag.getRooms = allRooms;

            var        getSubjects = entity.Subject.ToList();
            SelectList allSubjects = new SelectList(getSubjects, "Id", "Name");

            ViewBag.getSubjects = allSubjects;
            List <Reservation> reservations;

            using (ai_databaseEntities de = new ai_databaseEntities())
            {
                reservations = de.Reservation.ToList();
                if (de.User.Where(x => x.Id.Equals(LoggedOnUser.loggedOnUserID)).FirstOrDefault().isTeacher)
                {
                    ViewBag.type = "teacher";
                }
                else
                {
                    ViewBag.type = "student";
                }
            }
            return(View());
        }
 public bool IsEmailUsed(string email)
 {
     using (ai_databaseEntities de = new ai_databaseEntities())
     {
         var v = de.User.Where(a => a.email == email).FirstOrDefault();
         return(v != null);
     }
 }
 public bool IsLoginUsed(string login)
 {
     using (ai_databaseEntities de = new ai_databaseEntities())
     {
         var v = de.User.Where(a => a.login == login).FirstOrDefault();
         return(v != null);
     }
 }
        public ActionResult Login(UserLogin model, string ReturnUrl = "")
        {
            string message = "";

            using (ai_databaseEntities dc = new ai_databaseEntities())
            {
                var v = dc.User.Where(a => a.login == model.login).FirstOrDefault();
                if (v != null)
                {
                    if (!v.isEmailVerified)
                    {
                        ViewBag.Message = "Please verify your email first";
                        return(View());
                    }
                    TempData["loggedOnUserId"] = v.Id;
                    if (string.Compare(Crypto.Hash(model.password), v.password) == 0)
                    {
                        int    timeout   = model.rememberMe ? 525600 : 20;
                        var    ticket    = new FormsAuthenticationTicket(model.login, model.rememberMe, timeout);
                        string encrypted = FormsAuthentication.Encrypt(ticket);
                        var    cookie    = new HttpCookie(FormsAuthentication.FormsCookieName, encrypted);
                        cookie.Expires  = DateTime.Now.AddMinutes(timeout);
                        cookie.HttpOnly = true;
                        Response.Cookies.Add(cookie);


                        if (Url.IsLocalUrl(ReturnUrl))
                        {
                            return(Redirect(ReturnUrl));
                        }
                        else
                        {
                            if (v.isTeacher)
                            {
                                return(RedirectToAction("IndexTeacher", "Home"));
                            }
                            else
                            {
                                return(RedirectToAction("IndexStudent", "Home"));
                            }
                        }
                    }
                    else
                    {
                        message = "Invalid credential provided";
                    }
                }
                else
                {
                    message = "Invalid credential provided";
                }
            }
            ViewBag.Message = message;
            return(View());
        }
 public ActionResult Delete(Reservation model)
 {
     using (ai_databaseEntities de = new ai_databaseEntities())
     {
         Reservation delRes = de.Reservation.Where(x => x.Id.Equals(model.Id)).FirstOrDefault();
         if (delRes != null)
         {
             de.Reservation.Remove(delRes);
             de.SaveChanges();
         }
     }
     return(RedirectToAction("ViewReservations", "Reservation"));
 }
        public ActionResult VerifyAccount(string id)
        {
            bool status = false;

            using (ai_databaseEntities de = new ai_databaseEntities())
            {
                de.Configuration.ValidateOnSaveEnabled = false;
                var v = de.User.Where(a => a.ActivationCode == new Guid(id)).FirstOrDefault();
                if (v != null)
                {
                    v.isEmailVerified = true;
                    de.SaveChanges();
                    status = true;
                }
                else
                {
                    ViewBag.Message = "Invalid Request";
                }
            }
            ViewBag.Status = true;
            return(View());
        }
        public ActionResult IndexTeacher()
        {
            if (LoggedOnUser.loggedOnUserID == -1 || TempData["loggedOnUserId"] != null)
            {
                LoggedOnUser.loggedOnUserID = (int)TempData["loggedOnUserId"];
            }
            List <Reservation> reservations;

            using (ai_databaseEntities de = new ai_databaseEntities())
            {
                reservations = de.Reservation.ToList();
                if (de.User.Where(x => x.Id.Equals(LoggedOnUser.loggedOnUserID)).FirstOrDefault().isTeacher)
                {
                    ViewBag.type = "teacher";
                }
                else
                {
                    ViewBag.type = "student";
                }
            }
            return(View());
        }
        public ActionResult AddReservation(Reservation model)
        {
            string message = "";
            bool   status  = false;;

            #region Create new object of reservation and assign properties

            Reservation res = new Reservation();
            res.dateFrom = model.reservationDate.Add(model.startTime.TimeOfDay);
            res.dateTo   = model.reservationDate.Add(model.endTime.TimeOfDay);
            if (model.description == null)
            {
                res.description = "No description";
            }
            else
            {
                res.description = model.description;
            }
            res.roomId    = model.roomId;
            res.subjectId = model.subjectId;
            res.teacherId = LoggedOnUser.loggedOnUserID;

            #endregion

            #region Check if Room and Subject were selected

            if (model.roomId == 0 || model.subjectId == 0)
            {
                message = "Please provide correct room and subject!";
                TempData["resCreationMessage"] = message;
                TempData["resCreationStatus"]  = status;
                return(RedirectToAction("IndexTeacher", "Home"));
            }

            #endregion

            #region Check if selected date is future

            if (res.dateFrom.CompareTo(DateTime.Now) <= 0)
            {
                message = "You must select future date!";
                TempData["resCreationMessage"] = message;
                TempData["resCreationStatus"]  = status;
                return(RedirectToAction("IndexTeacher", "Home"));
            }

            #endregion

            #region Check if start hours are before end hours

            if (res.dateFrom.CompareTo(res.dateTo) > 0)
            {
                message = "Start time must be before end time!";
                TempData["resCreationMessage"] = message;
                TempData["resCreationStatus"]  = status;
                return(RedirectToAction("IndexTeacher", "Home"));
            }

            #endregion

            #region Check if room is free at selected time

            if (!isRoomFree(res))
            {
                message = "Selected room is reserved at this time!";
                TempData["resCreationMessage"] = message;
                TempData["resCreationStatus"]  = status;
                return(RedirectToAction("IndexTeacher", "Home"));
            }

            #endregion

            #region Check if user(teacher) does not have any other classes at this time

            if (!isTeacherFree(res))
            {
                message = "You have classes at this time!";
                TempData["resCreationMessage"] = message;
                TempData["resCreationStatus"]  = status;
                return(RedirectToAction("IndexTeacher", "Home"));
            }

            #endregion


            #region Add reservation to db

            using (ai_databaseEntities de = new ai_databaseEntities())
            {
                res.Room    = de.Room.Where(x => x.Id.Equals(res.roomId)).FirstOrDefault();
                res.Subject = de.Subject.Where(x => x.Id.Equals(res.subjectId)).FirstOrDefault();
                try
                {
                    de.Reservation.Add(res);
                    de.SaveChanges();
                }
                catch (DbEntityValidationException dbEx)
                {
                    message = dbEx.Message;
                    status  = false;
                    return(RedirectToAction("IndexTeacher", "Home"));
                }
            }

            #endregion

            status  = true;
            message = "Reservation succesfully created";
            TempData["resCreationMessage"] = message;
            TempData["resCreationStatus"]  = status;
            return(RedirectToAction("IndexTeacher", "Home"));
        }
        public ActionResult Edit(Reservation model)
        {
            string message = "";
            bool   status  = false;

            #region Create new object of reservation and assign properties

            model.dateFrom = model.reservationDate.Add(model.startTime.TimeOfDay);
            model.dateTo   = model.reservationDate.Add(model.endTime.TimeOfDay);
            if (model.description == null)
            {
                model.description = "No description";
            }
            model.roomId    = model.roomId;
            model.subjectId = model.subjectId;
            model.teacherId = LoggedOnUser.loggedOnUserID;

            #endregion

            #region Check if Room and Subject were selected

            if (model.roomId == 0 || model.subjectId == 0)
            {
                message = "Please provide correct room and subject!";
                TempData["resEditMessage"] = message;
                TempData["resEditStatus"]  = status;
                return(RedirectToAction("ViewReservations", "Reservation"));
            }

            #endregion

            #region Check if selected date is future

            if (model.dateFrom.CompareTo(DateTime.Now) <= 0)
            {
                message = "You must select future date!";
                TempData["resEditMessage"] = message;
                TempData["resEditStatus"]  = status;
                return(RedirectToAction("ViewReservations", "Reservation"));
            }

            #endregion

            #region Check if start hours are before end hours

            if (model.dateFrom.CompareTo(model.dateTo) > 0)
            {
                message = "Start time must be before end time!";
                TempData["resEditMessage"] = message;
                TempData["resEditStatus"]  = status;
                return(RedirectToAction("ViewReservations", "Reservation"));
            }

            #endregion

            #region Check if room is free at selected time

            if (!isRoomFree(model))
            {
                message = "Selected room is reserved at this time!";
                TempData["resEditMessage"] = message;
                TempData["resEditStatus"]  = status;
                return(RedirectToAction("ViewReservations", "Reservation"));
            }

            #endregion

            #region Check if user(teacher) does not have any other classes at this time

            if (!isTeacherFree(model))
            {
                message = "You have classes at this time!";
                TempData["resEditMessage"] = message;
                TempData["resEditStatus"]  = status;
                return(RedirectToAction("ViewReservations", "Reservation"));
            }

            #endregion


            #region Edit reservation

            using (ai_databaseEntities de = new ai_databaseEntities())
            {
                model.Room            = de.Room.Where(x => x.Id.Equals(model.roomId)).FirstOrDefault();
                model.Subject         = de.Subject.Where(x => x.Id.Equals(model.subjectId)).FirstOrDefault();
                de.Entry(model).State = System.Data.Entity.EntityState.Modified;
                de.SaveChanges();
            }

            #endregion

            status  = true;
            message = "Reservation succesfully edited";
            TempData["resEditMessage"] = message;
            TempData["resEditStatus"]  = status;

            return(RedirectToAction("ViewReservations", "Reservation"));
        }
        public ActionResult Registration([Bind(Exclude = "IsEmailVerified,ActivationCode")] User user)
        {
            bool   Status  = false;
            string message = String.Empty;

            //Model validation
            if (ModelState.IsValid)
            {
                #region  Login is already used

                var loginExists = IsLoginUsed(user.login);
                if (loginExists)
                {
                    ModelState.AddModelError("LoginExists", "Login already exist");
                    return(View(user));
                }

                #endregion

                #region  Email is already used

                var emailExists = IsEmailUsed(user.email);
                if (emailExists)
                {
                    ModelState.AddModelError("EmailExists", "Email already exist");
                    return(View(user));
                }

                #endregion

                #region Generate Activation Code

                user.ActivationCode = Guid.NewGuid();

                #endregion

                #region Password Hashing

                user.password        = Crypto.Hash(user.password);
                user.ConfirmPassword = Crypto.Hash(user.ConfirmPassword);

                #endregion

                user.isEmailVerified = false;
                user.isTeacher       = false;

                #region Save data to database

                using (ai_databaseEntities de = new ai_databaseEntities())
                {
                    de.User.Add(user);
                    de.SaveChanges();

                    #region Send email to user

                    SendVerificationLinkEmail(user.email, user.ActivationCode.ToString());
                    message = "Registration successfully done. Account activation link has been sent to your email:" + user.email;
                    Status  = true;
                    #endregion
                }

                #endregion
            }
            else
            {
                message = "Invalid request";
            }
            ViewBag.Message = message;
            ViewBag.Status  = Status;
            return(View(user));
        }