// GET: ItemForRent/Delete/5
        public ActionResult Delete(int id)
        {
            Items_for_rent item  = null;
            Car_for_rent   car   = null;
            House_for_rent house = null;

            try { item = Db.Items_for_rent.Single(i => i.item_id == id); }
            catch (Exception e) { }

            try { car = Db.Car_for_rent.Single(i => i.car_id == id); }
            catch (Exception e) { }

            try { house = Db.House_for_rent.Single(i => i.house_id == id); }
            catch (Exception e) { }

            if (item != null)
            {
                Rent rent = Db.Rents.Single(s => s.rent_id == item.rent_id);
                List <Comment_for_rent>      itemComments    = Db.Comment_for_rent.Where(c => c.rent_id == item.rent_id).ToList();
                List <Likes_for_rent_item>   likesComments   = Db.Likes_for_rent_item.Where(c => c.rent_id == item.rent_id).ToList();
                List <Dislike_for_rent_item> dislikeComments = Db.Dislike_for_rent_item.Where(c => c.rent_id == item.rent_id).ToList();

                foreach (var comment in itemComments)
                {
                    Db.Comment_for_rent.Remove(comment);
                }
                foreach (var like in likesComments)
                {
                    Db.Likes_for_rent_item.Remove(like);
                }
                foreach (var dislike in dislikeComments)
                {
                    Db.Dislike_for_rent_item.Remove(dislike);
                }
                Db.Rents.Remove(rent);
                Db.Items_for_rent.Remove(item);
            }
            else if (house != null)
            {
                Rent rent = Db.Rents.Single(s => s.rent_id == house.rent_id);
                Db.Rents.Remove(rent);
                Db.House_for_rent.Remove(house);
            }
            else if (car != null)
            {
                Rent rent = Db.Rents.Single(s => s.rent_id == car.rent_id);
                Db.Rents.Remove(rent);
                Db.Car_for_rent.Remove(car);
            }

            Db.SaveChanges();
            return(RedirectToAction("FindItemForRent", "ItemForRent"));
        }
Exemplo n.º 2
0
        public ActionResult ViewComment(int id, int type)
        {
            try
            {
                CommentForRentViewModel cm = new CommentForRentViewModel();
                User user = (User)Session["user"];
                if (type == 1)
                {
                    Items_for_rent item = Db.Items_for_rent.Single(i => i.rent_id == id);
                    cm.item = item;
                    cm.User = user;
                    Comment_for_rent comment = new Comment_for_rent();
                    cm.comment = comment;
                }
                else if (type == 2)
                {
                    Car_for_rent item = Db.Car_for_rent.Single(i => i.rent_id == id);
                    cm.item = item;
                    cm.User = user;
                    Comment_for_rent comment = new Comment_for_rent();
                    cm.comment = comment;
                }
                else
                {
                    House_for_rent item = Db.House_for_rent.Single(i => i.rent_id == id);
                    cm.item = item;
                    cm.User = user;
                }
//
                return(View(cm));
            }
            catch (Exception e)
            {
                return(RedirectToAction("Login", "User"));
            }
        }