ngày 31-03-2016
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Services;
/// <summary> /// Summary description for mywebservice /// </summary> [WebService(Namespace = "http://tranduythanh.com/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. // [System.Web.Script.Services.ScriptService] public class mywebservice : System.Web.Services.WebService {
ProductManagerDataContext db = null; public mywebservice () {
//Uncomment the following line if using designed components //InitializeComponent(); db = new ProductManagerDataContext(); }
[WebMethod] public string HelloWorld() { return "Hello World"; } //Hàm đếm xem có bao nhiêu danh mục trong bảng catalog [WebMethod] public int CountCatalog() { return db.Catalogs.Count(); } //2- Hàm trả về danh sách Catalog [WebMethod] public List<Catalog> getListCatalog() { List<Catalog> listCate = db.Catalogs.ToList(); foreach (Catalog c in listCate) c.Products.Clear(); return listCate; } //3- Hàm trả về thông tin của 1 Catalog theo Id [WebMethod] public Catalog getCatalog(string id) { Catalog c=db.Catalogs.FirstOrDefault(x => x.CateId == id); c.Products.Clear(); return c; } //4- Hàm trả về danh sách Product [WebMethod] public List<Product> getListProduct() { List<Product> listPro = db.Products.ToList(); foreach (Product p in listPro) p.Catalog = null; return listPro; } //5- Hàm trả về danh sách Product theo Catalog Id [WebMethod] public List<Product> getListProductByCatalogId(string id) { List<Product> listPro=db.Products.Where(x => x.CateId == id).ToList(); foreach(Product p in listPro) p.Catalog=null; return listPro; } //6- Hàm trả về thông tin của một Product theo Id [WebMethod] public Product getProduct(string id) { Product p = db.Products.FirstOrDefault(x => x.ProductId == id); p.Catalog = null; return p; } //7- Hàm xóa Catalog theo ID [WebMethod] public bool deleteCatalog(string id) { try { Catalog cate = getCatalog(id); db.Catalogs.DeleteOnSubmit(cate); db.SubmitChanges(); } catch (Exception ex) { return false; } return true; } //8- Hàm xóa Product theo ID. [WebMethod] public bool deleteProduct(string id) { try { Product p = getProduct(id); db.Products.DeleteOnSubmit(p); db.SubmitChanges(); } catch (Exception ex) { return false; } return true; } //9- Xuất tổng tiền của các mặt hàng [WebMethod] public double getToTalMoney() { return db.Products.Sum(x => x.TotalMoney).Value; } } |