/// <summary> /// Add new or update exist customer /// </summary> /// <param name="orderModel"></param> /// <returns></returns> public static Boolean UpdateCustomer(CustomerModel customerModel) { using (var db = new FasConDBEntities()) { var customer = db.Customers.Where(c => c.CustomerId == customerModel.CustomerId).FirstOrDefault(); if (customer == null) { customer = new Customer { CustomerId = Guid.NewGuid(), Name = customerModel.Name, UpdatedDate = DateTime.Now, UpdatedUserId = customerModel.UpdatedUserId, CreatedDate = DateTime.Now, CreatedUserId = customerModel.CreatedUserId, StoreId = customerModel.StoreId }; db.Customers.Add(customer); } else { customer.Name = customerModel.Name; customer.UpdatedDate = DateTime.Now; customer.UpdatedUserId = customerModel.UpdatedUserId; customerModel.Deleted = customerModel.Deleted; customer.StoreId = customerModel.StoreId; } db.SaveChanges(); } return true; }
public static Boolean UpdateAddress(AddressModel addressModel) { using (var db = new FasConDBEntities()) { var address = db.Addresses.Where(p => p.AddressId == addressModel.AddressId).FirstOrDefault(); bool isNew = false; if (address == null) { isNew = true; address = new Address(); address.AddressId = Guid.NewGuid(); } address.FullName = addressModel.FullName; address.Email = addressModel.Email; address.Company = addressModel.Company; address.DistrictId = addressModel.DistrictId; address.StateProvinceId = addressModel.StateProvinceId; address.Street = addressModel.Street; address.PhoneNumber = addressModel.PhoneNumber; address.FaxNumber = addressModel.FaxNumber; address.CreatedOnUtc = addressModel.CreatedOnUtc; if(isNew) db.Addresses.Add(address); db.SaveChanges(); } return true; }
public static List<StateProvinceModel> GetAllStates() { using (var db = new FasConDBEntities()) { var products = db.StateProvinces.ToList().OrderBy(p => p.Name); return products.Select(p => new StateProvinceModel { StateProvinceId=p.StateProvinceId,Name=p.Name}).ToList(); } }
public static List<ProductModel> Products() { using (var db = new FasConDBEntities()) { var products = db.Products.ToList().OrderBy(p=>p.ProductName); return products.Select(p => new ProductModel {ProductID=p.ProductID,ProductName = p.ProductName,ShortDescription=p.ShortDescription,ProductThumb=p.ProductThumb,Price=p.Price }).ToList(); } }
public static List<CustomerModel> Customers() { List<CustomerModel> rs = new List<CustomerModel>(); using (var db = new FasConDBEntities()) { rs = db.Customers.Where(c => c.Deleted == false).Select(c => new CustomerModel { CustomerId = c.CustomerId, Name = c.Name }).ToList(); } return rs; }
public static Boolean UpdateOrder(OrderModel orderModel) { using (var db = new FasConDBEntities()) { var order = db.Orders.Where(c => c.OrderId == orderModel.OrderId).FirstOrDefault(); if (order == null) { order = new Order { OrderId = orderModel.OrderId, OrderNumber = orderModel.OrderNumber, StoreId = orderModel.StoreId, CustomerId = orderModel.CustomerId, ShippingAddressId = orderModel.ShippingAddressId, OrderStatusId = orderModel.OrderStatusId, PaymentStatusId = orderModel.PaymentStatusId, VatNumber = orderModel.VatNumber, OrderSubtotal = orderModel.OrderSubtotal, OrderTax = orderModel.OrderTax, OrderTotal = orderModel.OrderTotal, CustomerIp = orderModel.CustomerIp, PaidDateUtc = orderModel.PaidDateUtc, ShippingMethod = orderModel.ShippingMethod, Deleted = orderModel.Deleted, CreatedOnUtc = orderModel.CreatedOnUtc, UpdatedOnUtc = orderModel.UpdatedOnUtc, CustomerOrderComment = orderModel.CustomerOrderComment }; db.Orders.Add(order); } else { order.OrderId = orderModel.OrderId; order.OrderNumber = orderModel.OrderNumber; order.StoreId = orderModel.StoreId; order.CustomerId = orderModel.CustomerId; order.ShippingAddressId = orderModel.ShippingAddressId; order.OrderStatusId = orderModel.OrderStatusId; order.PaymentStatusId = orderModel.PaymentStatusId; order.VatNumber = orderModel.VatNumber; order.OrderSubtotal = orderModel.OrderSubtotal; order.OrderTax = orderModel.OrderTax; order.OrderTotal = orderModel.OrderTotal; order.CustomerIp = orderModel.CustomerIp; order.PaidDateUtc = orderModel.PaidDateUtc; order.ShippingMethod = orderModel.ShippingMethod; order.Deleted = orderModel.Deleted; order.CreatedOnUtc = orderModel.CreatedOnUtc; order.UpdatedOnUtc = orderModel.UpdatedOnUtc; order.CustomerOrderComment = orderModel.CustomerOrderComment; } db.SaveChanges(); } return true; }
public static CustomerModel Customer(Guid customerId) { CustomerModel rs = new CustomerModel(); using (var db = new FasConDBEntities()) { rs = db.Customers.Where(c => c.Deleted == false && c.CustomerId == customerId).Select(c => new CustomerModel { CustomerId = c.CustomerId, Name = c.Name }).FirstOrDefault(); } return rs; }
public static List<DistrictModel> FindCitiesById(int stateId) { List<DistrictModel> rs=new List<DistrictModel>(); using (var db = new FasConDBEntities()) { var p = db.Districts.Where(pr => pr.StateProvinceId == stateId); rs = p.Select(o => new DistrictModel {DistrictId=o.DistrictId,Name=o.Name }).ToList(); return rs; } }
public static ProductModel FindProductById(Guid ProductId) { using (var db = new FasConDBEntities()) { var p = db.Products.Where(pr => pr.ProductID == ProductId).FirstOrDefault(); if (p == null) return null; //return Mapper.Map<ProductModel>(product); return ( new ProductModel { CategoryID=p.CategoryID, ProductID = p.ProductID, ProductName = p.ProductName, ShortDescription = p.ShortDescription, ProductThumb = p.ProductThumb, Price = p.Price,FullDescription=p.FullDescription }); } }
public static void GenerateSampleData() { using (var db = new FasConDBEntities()) { for (int i = 0; i < 10; i++) { var cat = new Category { CategoryId = Guid.NewGuid(), Name= "Danh mục " + i.ToString() , FullName = "Danh mục " + i.ToString(), Alias="danh-muc-"+i.ToString() }; try { db.Categories.Add(cat); db.SaveChanges(); for (int j = 10; j < 100; j++) { db.Products.Add(new Product { ProductID = Guid.NewGuid(), ProductName = "Sản phẩm " + i + j, Alias="san-pham-"+i+j, ProductThumb="http://sandichvu.com.vn/storage/app/uploads/public/556/889/db6/thumb_74_250x250_0_0_crop.jpg", Category = cat, ProductSKU="SKU "+i+j, ShortDescription = "ShortDescription" +i+j, Price=100000 }); } db.SaveChanges(); } catch (DbEntityValidationException e) { foreach (var eve in e.EntityValidationErrors) { Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State); foreach (var ve in eve.ValidationErrors) { Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage); } } throw; } } } }
public static void GenerateCustomer() { using (var db = new FasConDBEntities()) { db.Customers.Add(new Customer { CustomerId = Guid.NewGuid(), Name = "huu loi" }); db.SaveChanges(); } }
public static Boolean DeleteCustomer(CustomerModel customerModel) { using (var db = new FasConDBEntities()) { var order = db.Orders.Where(c => c.CustomerId == customerModel.CustomerId).FirstOrDefault(); if (order == null) { var customer = db.Customers.Where(c => c.CustomerId == customerModel.CustomerId).FirstOrDefault(); if (customer != null) db.Customers.Remove(customer); db.SaveChanges(); } } return true; }