Beispiel #1
0
 [HttpPost]                                     // Aşagıdaki metod post ile çalışacak
 public JsonResult Add(CategoryViewModel model) // Textboxlara girilen deger ajax ile buradaki model e gonderiliyor. // Burada her isinde de kullanacagımız aynı propertyler olduğu için model i Category tipinde de alsak olurdu.
 {
     try
     {
         var db = new NorthwindSabahEntities();
         db.Categories.Add(new Category()    // Gelen model nesnesini yeni bir kategori olarak veritabanına ekliyoruz.
         {
             CategoryName = model.CategoryName,
             Description  = model.Description
         });
         db.SaveChanges();
         return(Json(new ResponseData()  // En son Responsedata mızı mesaj ve success kısmını degiştirerek buradan geri donduruyoruz.
         {
             message = $"{model.CategoryName} ismindeki kategori başarıyla eklendi",
             success = true,
         }, JsonRequestBehavior.AllowGet));
     }
     catch (Exception ex)
     {
         return(Json(new ResponseData()
         {
             message = $"Bir hata oluştu {ex.Message}",
             success = false,
         }, JsonRequestBehavior.AllowGet));
     }
 }
Beispiel #2
0
 public JsonResult Update(Category model)    // Ajax tan gelen data buradaki model parametresine atandı. model in parametresi ile o nesneyi sql de bulduk ve model in propertylerini o nesnenin propertylerine atadık.
 {
     try
     {
         var db  = new NorthwindSabahEntities();
         var cat = db.Categories.Find(model.CategoryID);
         if (cat == null)    // Eger null ise sadece mesaj donduruyoruz.
         {
             return(Json(new ResponseData()
             {
                 message = $"Kategori bulunamadı",
                 success = false
             }, JsonRequestBehavior.AllowGet));
         }
         // Atama işlemlerini yapıp değişiklikleri kaydettik.
         cat.Description  = model.Description;
         cat.CategoryName = model.CategoryName;
         db.SaveChanges();
         return(Json(new ResponseData()
         {
             message = $"{cat.CategoryName} ismindeki kategori başarıyla guncellendi",
             success = true
         }, JsonRequestBehavior.AllowGet));
     }
     catch (Exception ex)
     {
         return(Json(new ResponseData()
         {
             message = $"Bir hata oluştu {ex.Message}",
             success = false
         }, JsonRequestBehavior.AllowGet));
     }
 }
Beispiel #3
0
 public JsonResult GetAllCategories()
 {
     // ResponseData kullanmamızın sebebi gelen ve giden jsonımızın bir standartının olması. İçinde belli propeertyler var onları javascripte karıstırmamak için belli bir standart belirledikgetirmek için yazdığımız JsonResult metodu.
     // Tüm kategorileri
     try
     {
         var db   = new NorthwindSabahEntities();
         var data = db.Categories.Select(x => new CategoryViewModel()
         {
             CategoryName = x.CategoryName,
             Description  = x.Description,
             CategoryID   = x.CategoryID,
             ProductCount = x.Products.Count
         });
         return(Json(new ResponseData()
         {
             success = true,
             data = data
         }, JsonRequestBehavior.AllowGet));
     }
     catch (Exception ex)
     {
         return(Json(new ResponseData()
         {
             success = false,
             message = $"Bir hata oluştu {ex.Message}"
         }, JsonRequestBehavior.AllowGet));
     }
 }
Beispiel #4
0
        public ActionResult Update(Employee model)
        {
            try
            {
                var db   = new NorthwindSabahEntities();
                var data = db.Employees.Find(model.EmployeeID);

                if (data == null)
                {
                    return(RedirectToAction("Index"));
                }
                data.FirstName = model.FirstName;
                data.LastName  = model.LastName;
                data.HomePhone = model.HomePhone;
                data.Address   = model.Address;
                db.SaveChanges();
                ViewBag.Message = "<span class='text text-success'>Update Successfuly</span>";
                return(RedirectToAction("Index"));
            }
            catch (Exception ex)
            {
                ViewBag.Message = $"<span class='text text-danger'>Update Error {ex.Message}</span>";
                return(View(model));
            }
        }
Beispiel #5
0
        public JsonResult Delete(int id)    // Ajax tan gelen id buradaki id parametresine atandı ve o id deki categori bulunup silindi.
        {
            try
            {
                var db  = new NorthwindSabahEntities();
                var cat = db.Categories.Find(id);
                db.Categories.Remove(cat);
                db.SaveChanges();
                return(Json(new ResponseData()
                {
                    message = $"{cat.CategoryName} ismindeki kategori başarıyla silindi",
                    success = true
                }, JsonRequestBehavior.AllowGet));
            }
            catch (Exception ex)
            {
                return(Json(new ResponseData()
                {
                    message = $"Kategori silme işleminde hata {ex.Message}",
                    success = false
                }, JsonRequestBehavior.AllowGet));

                throw;
            }
        }
Beispiel #6
0
 public JsonResult Add2(ProductViewModel model)
 {
     try
     {
         var db = new NorthwindSabahEntities();
         db.Products.Add(new Product()
         {
             ProductName     = model.ProductName,
             CategoryID      = model.CategoryID,
             UnitPrice       = model.UnitPrice,
             UnitsInStock    = model.UnitsInStock,
             AddedDate       = model.AddedDate,
             Discontinued    = model.Discontinued,
             QuantityPerUnit = model.QuantityPerUnit,
             ReorderLevel    = model.ReorderLevel,
             SupplierID      = model.SupplierID,
             UnitsOnOrder    = model.UnitsOnOrder
         });
         db.SaveChanges();
         return(Json(new ResponseData()
         {
             message = $"{model.ProductName} ismindeki urun başarıyla eklendi",
             success = true,
         }, JsonRequestBehavior.AllowGet));
     }
     catch (Exception ex)
     {
         return(Json(new ResponseData()
         {
             message = $"Bir hata oluştu {ex.Message}",
             success = false,
         }, JsonRequestBehavior.AllowGet));
     }
 }
Beispiel #7
0
        public JsonResult Rapor1()
        {
            var db    = new NorthwindSabahEntities();
            var sorgu = from dbCategory in db.Categories
                        join dbProduct in db.Products on dbCategory.CategoryID equals dbProduct.CategoryID
                        join dbOrderDetails in db.Order_Details on dbProduct.ProductID equals dbOrderDetails.ProductID
                        group new
            {
                dbCategory,
                dbOrderDetails
            } by new
            {
                dbCategory.CategoryName
            } into gb
                select new
            {
                gb.Key.CategoryName,
                Total = gb.Sum(x => x.dbOrderDetails.Quantity)
            };
            var data = sorgu.ToList();

            return(Json(new ResponseData()
            {
                message = $"{data.Count} adet kayıt bulundu",
                success = true,
                data = data
            }, JsonRequestBehavior.AllowGet));
        }
Beispiel #8
0
        public ActionResult Update(Employee employee)
        {
            try
            {
                var db   = new NorthwindSabahEntities();
                var data = db.Employees.Find(employee.EmployeeID);

                if (data == null)
                {
                    return(RedirectToAction("Index"));
                }

                data.FirstName = employee.FirstName;
                data.LastName  = employee.LastName;
                data.BirthDate = employee.BirthDate;
                data.HomePhone = employee.HomePhone;
                data.Address   = employee.Address;
                db.SaveChanges();
                ViewBag.Message = "<span class='text text-success'>Başarıyla güncellendi.</span>";
                return(View(data));
            }
            catch (Exception ex)
            {
                ViewBag.Message = $"<span class='text text-danger'>Update Error {ex.Message}</span>";
                return(View(employee));
            }
        }
Beispiel #9
0
        public ActionResult Index()
        {
            var data = new NorthwindSabahEntities()
                       .Employees
                       .OrderBy(x => x.EmployeeID)
                       .ToList();

            return(View(data));
        }
Beispiel #10
0
        // GET: Category
        // Adres cubuguna yazdığımız actionlar tetiklenir ve sonucunda viewlar gelir.View içerisinde tanımladıgımız actionlar ekrandan tetiklenince buradaki tetiklenen action çalışır.Ve tekrardan view gelir.
        public ActionResult Index() // İlk önce index actionları tetiklenir.
        {
            var data = new NorthwindSabahEntities()
                       .Categories
                       .OrderBy(x => x.CategoryName)
                       .ToList();

            return(View(data));
        }
        // get: category
        //bir sayfayı oluşturmak için action ihtiyaç var
        //action res. aşağıda tanımlı.
        //method overloading yok çünkü kafası karışıyor. parametreleri | ile yollladığı için
        public ActionResult Index()
        {
            var data = new NorthwindSabahEntities()
                       .Categories
                       .OrderBy(x => x.CategoryName)
                       .ToList();

            return(View(data));
        }
Beispiel #12
0
        public JsonResult Employees()
        {
            var employees = new NorthwindSabahEntities().Employees.Select(x => new
            {
                x.FirstName,
                x.EmployeeID,
                x.LastName
            }).ToList();

            return(Json(employees, JsonRequestBehavior.AllowGet));
        }
Beispiel #13
0
        public JsonResult Products()
        {
            var products = new NorthwindSabahEntities().Products.Select(x => new
            {
                x.ProductName,
                x.ProductID,
                x.UnitsInStock
            }).ToList();

            return(Json(products, JsonRequestBehavior.AllowGet));
        }
Beispiel #14
0
        public JsonResult Categories()
        {
            var categoriler = new NorthwindSabahEntities().Categories.Select(x => new
            {
                x.CategoryName,
                x.CategoryID,
                x.Description,
                ProductCount = x.Products.Count
            }).ToList();                                             // Kategori nesnemizi oluşturduk ve görünüm için select yaptık. Json oluştururken view model kullanmalıyız yoksa hata alabiliriz.Onun için select yapıyrouz.

            return(Json(categoriler, JsonRequestBehavior.AllowGet)); // Donusumu burada oluşturduk.
        }
        public JsonResult Categories()
        {
            var categoriler = new NorthwindSabahEntities().Categories.Select(x => new
            {
                x.CategoryName,
                x.CategoryID,
                x.Description,
                ProductCount = x.Products.Count
            }).ToList();

            return(Json(categoriler, JsonRequestBehavior.AllowGet));
        }
Beispiel #16
0
 public ActionResult Ekle(Employee employee)
 {
     try
     {
         var db = new NorthwindSabahEntities();
         db.Employees.Add(employee);
         db.SaveChanges();
     }
     catch (Exception ex)
     {
         return(RedirectToAction("Index"));
     }
     return(RedirectToAction("Index"));
 }
Beispiel #17
0
        public ActionResult Detail(int?id)  // View den id geliyor
        {
            if (id == null)
            {
                return(RedirectToAction("Index"));                             // Eger null sa Index sayfasına geri donuyor.
            }
            var data = new NorthwindSabahEntities().Categories.Find(id.Value); // Null değilse gelen id deki kategoriyi getiriyor.

            if (data == null)
            {
                RedirectToAction("Index"); // Eger data da null gelirse Index sayfasına geri donuyor.
            }
            return(View(data));            // Gelen datayı dondürüyor.
        }
Beispiel #18
0
 public ActionResult Add(Products products)
 {
     try
     {
         var db = new NorthwindSabahEntities();
         db.Products.Add(products);
         db.SaveChanges();
     }
     catch (Exception ex)
     {
         return(RedirectToAction("Index"));
     }
     return(RedirectToAction("Index"));
 }
Beispiel #19
0
 [HttpPost]                                 // Annotation ekledik.HtpPost ile çalışacaksın dedik.
 public ActionResult Add(Category category) // Paremetre olarak; (string CategoryName,string Description)
 {
     try
     {
         var db = new NorthwindSabahEntities();
         db.Categories.Add(category);
         db.SaveChanges();
     }
     catch (Exception ex)
     {
         return(RedirectToAction("Index"));
     }
     return(RedirectToAction("Index"));
 }
 public ActionResult Add(Category category)
 {
     try
     {
         var db = new NorthwindSabahEntities();
         db.Categories.Add(category);
         db.SaveChanges();
     }
     catch (Exception ex)
     {
         return(RedirectToAction("Index"));
     }
     return(RedirectToAction("Index"));
 }
Beispiel #21
0
 public JsonResult GetAllProducts(string key)
 {
     try
     {
         var db    = new NorthwindSabahEntities();
         var query = db.Products.AsQueryable(); //AsQueryable() yazmazsak query degğişkenimiz dbset tipinde oluyor. Bİz aynı sorgu üzerinden arama kosulunu yazmak istediğimiz ve devamında da kullanmak istediğimiz için bu değişkenimizi AsQueryable ile Queryable tipine donuşturduk. Böylece aynı değişkeni her şekilde kullanabildik. if e girsede girmesede iften sonraki satırda bu değişkenin queryable tipinde olması lazım.
         if (!string.IsNullOrEmpty(key))        // Eger key parametresi boş gelmezse arama sartını gercekleştirecek.
         {
             key   = key.ToLower();
             query = query.Where(x => x.ProductName.ToLower().Contains(key) ||
                                 x.Category.CategoryName.ToLower().Contains(key) ||
                                 x.Supplier.CompanyName.ToLower().Contains(key));
         }
         var data = query.OrderBy(x => x.ProductName)
                    .ToList()
                    .Select(x => new ProductViewModel()
         {
             CategoryName       = x.Category?.CategoryName,  // CategoryName nullable bir alan. Bu ifadeyi çğırdığımızda bize null gelme durumu olabilir.Bunun için ?. kullandık.Null gelirse bu satırı işlemeyecek. Ama bu lambda expressinda null propagationı yapamıyor.Çünkü biz burada sadece sorguyu hazırlıyoruz ve bu sorgu tolist demeden sql e atılmış oluyor.Böyle bir ifedenin karşılıgıda sql de olmadığı için hata veriyor. Bunu önlemek için selectten once tolist yazdık.Boylece liste ram e geldi ve burada artık c# komutları çalışacak. ve ifadenin C# ta karşılığı oldugu için hata da ortadan kalkacak.
             AddedDate          = x.AddedDate,
             CategoryID         = x.CategoryID,
             ProductName        = x.ProductName,
             UnitsInStock       = x.UnitsInStock,
             UnitPrice          = x.UnitPrice,
             ProductID          = x.ProductID,
             AddedDateFormatted = $"{x.AddedDate:g}",        // Formattlanmış halini buradan gonderiyoruz.Ekranda da bunu gösterecegiz.
             Discontinued       = x.Discontinued,
             QuantityPerUnit    = x.QuantityPerUnit,
             ReorderLevel       = x.ReorderLevel,
             SupplierID         = x.SupplierID,
             SupplierName       = x.Supplier?.CompanyName,
             UnitPriceFormatted = $"{x.UnitPrice:c2}",       // Formattlanmış halini buradan gonderiyoruz.Ekranda da bunu gösterecegiz.
             UnitsOnOrder       = x.UnitsOnOrder
         })
                    .ToList();
         return(Json(new ResponseData()
         {
             success = true,
             data = data
         }, JsonRequestBehavior.AllowGet));
     }
     catch (Exception ex)
     {
         return(Json(new ResponseData()
         {
             success = false,
             message = $"Bir hata olustu {ex.Message}"
         }, JsonRequestBehavior.AllowGet));
     }
 }
Beispiel #22
0
        public ActionResult Detay(int?id)
        {
            if (id == null)
            {
                return(RedirectToAction("Index"));
            }

            var data = new NorthwindSabahEntities().Employees.Find(id.Value);

            if (id == null)
            {
                return(RedirectToAction("Index"));
            }
            return(View(data));
        }
        public int MakeOrder(CartViewModel cartViewModel)
        {
            NorthwindSabahEntities db = new NorthwindSabahEntities();

            using (var tran = db.Database.BeginTransaction())
            {
                try
                {
                    var order = new Order()
                    {
                        CustomerID   = cartViewModel.CustomerID,
                        EmployeeID   = cartViewModel.EmployeeID,
                        ShipVia      = cartViewModel.ShipVia,
                        Freight      = cartViewModel.Freight,
                        ShipAddress  = cartViewModel.Address,
                        RequiredDate = cartViewModel.RequiredDate.Date,
                        OrderDate    = cartViewModel.OrderDate
                    };
                    db.Orders.Add(order);
                    db.SaveChanges();

                    foreach (var item in cartViewModel.CartModel)
                    {
                        if (item.ProductID == 1)
                        {
                            throw  new Exception("Bir hata olustu");
                        }
                        db.Order_Details.Add(new Order_Detail()
                        {
                            OrderID   = order.OrderID,
                            ProductID = item.ProductID,
                            UnitPrice = item.UnitPrice,
                            Quantity  = item.Quantity,
                            Discount  = item.Discount
                        });
                    }

                    db.SaveChanges();
                    tran.Commit();
                    return(order.OrderID);
                }
                catch (Exception ex)
                {
                    tran.Rollback();
                    throw ex;
                }
            }
        }
        public ActionResult Detail(int?id)
        {
            //id çubuğundan null yelmesin diye null kontrolü
            if (id == null)
            {
                return(RedirectToAction("Index"));
            }

            var data = new NorthwindSabahEntities().Categories.Find(id.Value);

            if (data == null)
            {
                RedirectToAction("Index");
            }
            return(View(data));
        }
Beispiel #25
0
        public ActionResult Delete(int?id)
        {
            var db = new NorthwindSabahEntities();

            try
            {
                var category = db.Categories.Find(id.GetValueOrDefault()); // Eger degeri varsa değerini alacak yoksa de default degerini alacak. Categorimizi getirdik
                if (category == null)
                {
                    return(RedirectToAction("Index")); // nullsa Index sayfasına git
                }
                db.Categories.Remove(category);        // Gelen kategoriyi sil
                db.SaveChanges();
            }
            catch (Exception ex)
            {
            }
            return(RedirectToAction("Index"));
        }
Beispiel #26
0
 public JsonResult GetAllProducts2(int?id)
 {
     try
     {
         var db   = new NorthwindSabahEntities();
         var data = db.Products.Where(x => x.CategoryID == id)
                    .OrderBy(x => x.ProductName)
                    .ToList()
                    .Select(x => new ProductViewModel()
         {
             CategoryName       = x.Category?.CategoryName,
             AddedDate          = x.AddedDate,
             CategoryID         = x.CategoryID,
             ProductName        = x.ProductName,
             UnitsInStock       = x.UnitsInStock,
             UnitPrice          = x.UnitPrice,
             ProductID          = x.ProductID,
             AddedDateFormatted = $"{x.AddedDate:g}",
             Discontinued       = x.Discontinued,
             QuantityPerUnit    = x.QuantityPerUnit,
             ReorderLevel       = x.ReorderLevel,
             SupplierID         = x.SupplierID,
             SupplierName       = x.Supplier?.CompanyName,
             UnitPriceFormatted = $"{x.UnitPrice:c2}",
             UnitsOnOrder       = x.UnitsOnOrder
         })
                    .ToList();
         return(Json(new ResponseData()
         {
             success = true,
             data = data
         }, JsonRequestBehavior.AllowGet));
     }
     catch (Exception ex)
     {
         return(Json(new ResponseData()
         {
             success = false,
             message = $"Bir hata oluştu {ex.Message}"
         }, JsonRequestBehavior.AllowGet));
     }
 }
Beispiel #27
0
 public ActionResult Guncelle(int?id)
 {
     if (id == null)
     {
         return(RedirectToAction("Index", "Employee"));
     }
     try
     {
         var data = new NorthwindSabahEntities().Employees.Find(id.Value);
         if (data == null)
         {
             return(RedirectToAction("Index", "Employee"));
         }
         return(View(data));
     }
     catch (Exception ex)
     {
         return(RedirectToAction("Index", "Employee", $"{ex.Message}"));
     }
 }
        public ActionResult Delete(int?id)
        {
            var db = new NorthwindSabahEntities();

            try
            {
                var category = db.Categories.Find(id.GetValueOrDefault());
                if (category == null)
                {
                    return(RedirectToAction("Index"));
                }

                db.Categories.Remove(category);
                db.SaveChanges();
            }
            catch (Exception ex)
            {
            }
            return(RedirectToAction("Index"));
        }
Beispiel #29
0
 public ActionResult Update(int?id)
 {
     if (id == null)
     {
         return(RedirectToAction("Index"));            //("Index","Home") Sadece tek bir parametre yazdıgımızda bulundugu controllerdaki view a gider. İkinci parametre hangi controllerdaki view a gitmemiz gerektiği
     }
     try
     {
         var data = new NorthwindSabahEntities().Categories.Find(id.Value); // Gelen id den secilen kategoriyi buluyor.
         if (data == null)                                                  // Data null ise index sayfasına geri gönderiyor.
         {
             return(RedirectToAction("Index"));
         }
         return(View(data));  //Data geldiyse, category nesnemizin bilgilerini Update adlı sayfaya gönderiyoruz.Ve artık o sayfa görüntüleniyor.
     }
     catch (Exception)
     {
         return(RedirectToAction("Index"));
     }
 }
 public ActionResult Update(int?id)
 {
     if (id == null)
     {
         return(RedirectToAction("Index", "Category"));
     }
     try
     {
         var data = new NorthwindSabahEntities().Categories.Find(id.Value);
         if (data == null)
         {
             return(RedirectToAction("Index", "Category"));
         }
         return(View(data));
     }
     catch (Exception ex)
     {
         return(RedirectToAction("Index", "Category"));
     }
 }