Пример #1
0
        public ActionResult Create(SupplierVIewModel supplier)
        {
            if (ModelState.IsValid)
            {
                string json        = JsonConvert.SerializeObject(supplier);
                var    buffer      = System.Text.Encoding.UTF8.GetBytes(json);
                var    byteContent = new ByteArrayContent(buffer);
                byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");

                string              apiEndPoint = ApiURL + "api/SupplierApi/";
                HttpClient          client      = new HttpClient();
                HttpResponseMessage response    = client.PostAsync(apiEndPoint, byteContent).Result;

                string result  = response.Content.ReadAsStringAsync().Result.ToString();
                int    success = int.Parse(result);
                if (success == 1)
                {
                    return(RedirectToAction("Index"));
                }
                else
                {
                    return(View(supplier));
                }
            }

            return(View(supplier));
        }
Пример #2
0
 public ActionResult Edit(SupplierVIewModel supplier)
 {
     if (ModelState.IsValid)
     {
         service.UpdateSupplier(supplier);
         return(RedirectToAction("Index"));
     }
     return(View(supplier));
 }
 public int Put(SupplierVIewModel supplier)
 {
     try
     {
         service.UpdateSupplier(supplier);
         return(1);
     }
     catch (Exception)
     {
         return(0);
     }
 }
 public int Post(SupplierVIewModel supplier)
 {
     try
     {
         service.AddNewSupplier(supplier);
         return(1);
     }
     catch (Exception)
     {
         return(0);
     }
 }
Пример #5
0
        // GET: Suppliers/Delete/5
        public ActionResult Delete(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            int idx = id.HasValue ? id.Value : 0;
            SupplierVIewModel supplierView = service.GetSupplierById(idx);

            if (supplierView == null)
            {
                return(HttpNotFound());
            }
            return(View(supplierView));
        }
Пример #6
0
        public void UpdateSupplier(SupplierVIewModel supplierVIew)
        {
            using (ShopDBEntities db = new ShopDBEntities())
            {
                Supplier supplier = new Supplier();
                supplier.Id           = supplierVIew.Id;
                supplier.CompanyName  = supplierVIew.CompanyName;
                supplier.ContactName  = supplierVIew.ContactName;
                supplier.ContactTitle = supplierVIew.ContactTitle;
                supplier.City         = supplierVIew.City;
                supplier.Country      = supplierVIew.Country;
                supplier.Phone        = supplierVIew.Phone;
                supplier.Fax          = supplierVIew.Fax;

                db.Entry(supplier).State = EntityState.Modified;
                db.SaveChanges();
            }
        }
Пример #7
0
        public void AddNewSupplier(SupplierVIewModel supplierVIew)
        {
            using (ShopDBEntities db = new ShopDBEntities())
            {
                Supplier supplier = new Supplier();
                supplier.Id           = supplierVIew.Id;
                supplier.CompanyName  = supplierVIew.CompanyName;
                supplier.ContactName  = supplierVIew.ContactName;
                supplier.ContactTitle = supplierVIew.ContactTitle;
                supplier.City         = supplierVIew.City;
                supplier.Country      = supplierVIew.Country;
                supplier.Phone        = supplierVIew.Phone;
                supplier.Fax          = supplierVIew.Fax;

                db.Suppliers.Add(supplier);
                db.SaveChanges();
            }
        }
Пример #8
0
        public SupplierVIewModel GetSupplierById(int id)
        {
            using (ShopDBEntities db = new ShopDBEntities())
            {
                Supplier          supplier     = db.Suppliers.Find(id);
                SupplierVIewModel supplierVIew = new SupplierVIewModel();
                supplierVIew.Id           = supplier.Id;
                supplierVIew.CompanyName  = supplier.CompanyName;
                supplierVIew.ContactName  = supplier.ContactName;
                supplierVIew.ContactTitle = supplier.ContactTitle;
                supplierVIew.City         = supplier.City;
                supplierVIew.Country      = supplier.Country;
                supplierVIew.Phone        = supplier.Phone;
                supplierVIew.Fax          = supplier.Fax;

                List <ProductViewModel> Listproduct = new List <ProductViewModel>();
                var products = db.Products.Where(m => m.SupplierId == supplierVIew.Id).ToList();

                foreach (var item in products)
                {
                    ProductViewModel productView = new ProductViewModel();
                    productView.Id             = item.Id;
                    productView.ProductName    = item.ProductName;
                    productView.SupplierId     = item.SupplierId;
                    productView.CompanyName    = item.Supplier.CompanyName;
                    productView.UnitPrice      = item.UnitPrice;
                    productView.Package        = item.Package;
                    productView.IsDiscontinued = item.IsDiscontinued;

                    Listproduct.Add(productView);
                }
                supplierVIew.listproduct = Listproduct;



                return(supplierVIew);
            }
        }
Пример #9
0
        public List <SupplierVIewModel> GetAllSupplier()
        {
            using (ShopDBEntities db = new ShopDBEntities())
            {
                List <SupplierVIewModel> supplierVIews = new List <SupplierVIewModel>();
                foreach (var item in db.Suppliers.ToList())
                {
                    SupplierVIewModel vIewModel = new SupplierVIewModel();
                    vIewModel.Id           = item.Id;
                    vIewModel.CompanyName  = item.CompanyName;
                    vIewModel.ContactName  = item.ContactName;
                    vIewModel.ContactTitle = item.ContactTitle;
                    vIewModel.City         = item.City;
                    vIewModel.Country      = item.Country;
                    vIewModel.Phone        = item.Phone;
                    vIewModel.Fax          = item.Fax;

                    supplierVIews.Add(vIewModel);
                }

                return(supplierVIews);
            }
        }