示例#1
0
        public ActionResult Create(CreateAdModel model)
        {
            if (this.ModelState.IsValid)
            {
                var sellerId = this.User
                               .Identity
                               .GetUserId();

                var ad = new Ad
                {
                    Title       = model.Title,
                    Category    = model.Category,
                    Discription = model.Discription,
                    Price       = model.Price,
                    Town        = model.Town,
                    ImgURL      = model.ImgUrl,
                    SellerId    = sellerId,
                    Date        = DateTime.Now
                };


                var db = new SellPlaceDbContext();
                db.Ads.Add(ad);
                db.SaveChanges();

                return(RedirectToAction("Details", new { id = ad.Id }));
            }

            return(View(model));
        }
示例#2
0
        public ActionResult Cart(int adId, decimal totalPrice)
        {
            var db = new SellPlaceDbContext();

            var ad = db.Ads
                     .Where(a => a.Id == adId)
                     .FirstOrDefault();

            var userId = this.User.Identity.GetUserId();

            if (ad == null || ad.IsSold || ad.SellerId == userId)
            {
                return(HttpNotFound());
            }

            var cart = new Cart
            {
                AdId       = adId,
                TotalPrice = totalPrice,
                UserId     = userId,
                BuyOn      = DateTime.Now
            };

            ad.IsSold = true;

            db.Carts.Add(cart);
            db.SaveChanges();

            return(RedirectToAction("Details", new { id = ad.Id }));
        }
示例#3
0
        public ActionResult Edit(EditAdModel model)
        {
            if (ModelState.IsValid)
            {
                using (var db = new SellPlaceDbContext())
                {
                    var ad = db.Ads.Find(model.Id);

                    if (ad == null || !IsAutorized(ad))
                    {
                        return(HttpNotFound());
                    }

                    ad.Title       = model.Title;
                    ad.Category    = model.Category;
                    ad.Discription = model.Discription;
                    ad.Price       = model.Price;
                    ad.ImgURL      = model.ImgUrl;

                    db.SaveChanges();
                }

                return(RedirectToAction("Details", new { id = model.Id }));
            }

            return(View(model));
        }
示例#4
0
        public ActionResult Edit(string id, UserViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                using (var db = new SellPlaceDbContext())
                {
                    var user = db.Users.FirstOrDefault(u => u.Id == id);

                    if (id == null)
                    {
                        return(HttpNotFound());
                    }

                    if (!string.IsNullOrEmpty(viewModel.Password))
                    {
                        var hasher         = new PasswordHasher();
                        var passwordHasher = hasher.HashPassword(viewModel.Password);
                        user.PasswordHash = passwordHasher;
                    }

                    user.Email    = viewModel.Email;
                    user.FullName = viewModel.FullName;
                    this.SetUserRoles(viewModel, user, db);

                    db.Entry(user).State = EntityState.Modified;
                    db.SaveChanges();
                }
            }

            return(RedirectToAction("List"));
        }
示例#5
0
        public ActionResult ConfirmDelete(string id)
        {
            if (id == null)
            {
                return(HttpNotFound());
            }

            using (var db = new SellPlaceDbContext())
            {
                var user = db.Users.FirstOrDefault(u => u.Id == id);

                var userAds = db.Ads
                              .Where(a => a.Seller.Id == user.Id);

                foreach (var item in userAds)
                {
                    db.Ads.Remove(item);
                }

                db.Users.Remove(user);
                db.SaveChanges();

                return(RedirectToAction("List"));
            }
        }
示例#6
0
        public ActionResult ConfirmDelete(int id)
        {
            using (var db = new SellPlaceDbContext())
            {
                var ad = db.Ads
                         .Where(a => a.Id == id)
                         .FirstOrDefault();

                if (ad == null || !IsAutorized(ad))
                {
                    return(HttpNotFound());
                }

                db.Ads.Remove(ad);
                db.SaveChanges();

                return(RedirectToAction("AllAds"));
            }
        }