Esempio n. 1
0
        public ActionResult Delete(int id)
        {
            try
            {
                UserDao     uDao        = new UserDao();
                AddressDao  aDao        = new AddressDao();
                FitnessUser fitnessUser = uDao.GetById(id);
                Address     address     = new Address();
                address = fitnessUser.Address;

                //pokud se jedná o trenéra, je třeba smazat jeho termíny, které vede
                if (fitnessUser.Role.Name == "Trenér")
                {
                    TermDao      tDao  = new TermDao();
                    IList <Term> terms = tDao.GetTermsByTrainer(fitnessUser);

                    foreach (Term t in terms)
                    {
                        tDao.Delete(t);
                    }
                }//pokud se jedna o zakaznika, je treba smazat rezervace, na ktere je zapsan
                else if (fitnessUser.Role.Name == "Zákazník")
                {
                    ReservationDao      rDao         = new ReservationDao();
                    IList <Reservation> reservations = rDao.GetAllReservationsByUser(fitnessUser);

                    foreach (Reservation r in reservations)
                    {
                        rDao.Delete(r);
                    }
                }

                if (fitnessUser.SmallImageName != null)
                {
                    System.IO.File.Delete(Server.MapPath("~/Uploads/FitnessUser/" + fitnessUser.SmallImageName));
                }
                if (fitnessUser.BigImageName != null)
                {
                    System.IO.File.Delete(Server.MapPath("~/Uploads/FitnessUser/" + fitnessUser.BigImageName));
                }

                uDao.Delete(fitnessUser);
                aDao.Delete(address);


                TempData["succes"] = "Uživatelský účet byl odstraněn.";

                if (fitnessUser.Login == User.Identity.Name)
                {
                    return(RedirectToAction("Logout", "Home"));
                }
                return(RedirectToAction("Index", "Home"));
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }