Ejemplo n.º 1
0
        public ActionResult CreateCustomer(Customer customer, string TypeAcc, string phoneNumber, string password)
        {
            var TypeList = db.Types.Select(p => p.Type_Name).ToList();

            ViewBag.roleList = TypeList;

            if (ModelState.IsValid)
            {
                if (Request.Files.Count > 0)
                {
                    var file = Request.Files[Request.Files.Count - 1];

                    if (file != null)
                    {
                        string pic = Path.GetFileName(file.FileName);

                        string extensionFileName = CommonFunction.getExtensionFileName(pic);

                        pic = CommonFunction.hashSHA256(pic) + extensionFileName;

                        string path = Path.Combine(Server.MapPath(Constants.CUS_IMG_URL_ADD), pic);
                        customer.Avatar_URL = pic;

                        file.SaveAs(path);
                    }
                }
                else
                {
                    customer.Avatar_URL = Constants.CUS_IMG_NOAVATAR;
                }

                customer.Account.PasswordHash = HashPwdTool.GeneratePassword(password);

                PhoneNumber phNum = new PhoneNumber();
                phNum.PhoneNumber1 = phoneNumber;
                customer.PhoneNumbers.Add(phNum);

                Role_Account ra = new Role_Account();
                ra.Role_ID = 3;
                customer.Account.Role_Account.Add(ra);

                int type = db.Types.Where(p => p.Type_Name == TypeAcc).Select(r => r.Type_ID).SingleOrDefault();
                RealEstateWebsite.Models.Type t = db.Types.Find(type);
                customer.Type = db.Types.Find(t.Type_ID);

                db.Customers.Add(customer);

                db.SaveChanges();
                return(RedirectToAction("Viewcustomer", "Customer"));
            }
            return(View());
        }
Ejemplo n.º 2
0
        public ActionResult SaveCustomer(Customer customer, string TypeAcc, string phoneNumber)
        {
            var TypeList = db.Types.Select(p => p.Type_Name).ToList();

            ViewBag.roleList = TypeList;
            try
            {
                if (ModelState.IsValid)
                {
                    if (Request.Files.Count > 0)
                    {
                        var file = Request.Files[Request.Files.Count - 1];

                        if (file != null)
                        {
                            string pic = Path.GetFileName(file.FileName);

                            string extensionFileName = CommonFunction.getExtensionFileName(pic);

                            pic = CommonFunction.hashSHA256(pic) + extensionFileName;

                            string path = Path.Combine(Server.MapPath(Constants.CUS_IMG_URL_ADD), pic);
                            customer.Avatar_URL = pic;

                            file.SaveAs(path);
                        }
                    }
                    else
                    {
                        customer.Avatar_URL = Constants.CUS_IMG_NOAVATAR;
                    }



                    Customer customerDB = db.Customers.Find(customer.Customer_ID);
                    customerDB.Firstname = customer.Firstname;
                    customerDB.LastName  = customer.LastName;

                    int type = db.Types.Where(p => p.Type_Name == TypeAcc).Select(r => r.Type_ID).SingleOrDefault();
                    RealEstateWebsite.Models.Type t = db.Types.Find(type);
                    customerDB.Type = db.Types.Find(t.Type_ID);

                    if (phoneNumber != "")
                    {
                        if (phoneNumber != null)
                        {
                            if ((customerDB.PhoneNumbers.LastOrDefault() != null) && (customerDB.PhoneNumbers.LastOrDefault().PhoneNumber1 != phoneNumber))
                            {
                                customerDB.PhoneNumbers.Remove(customerDB.PhoneNumbers.Last());

                                PhoneNumber pn = new PhoneNumber();
                                pn.PhoneNumber1 = phoneNumber;
                                pn.ModifiedDate = DateTime.Now;

                                customerDB.PhoneNumbers.Add(pn);
                            }
                        }
                    }
                    customerDB.Email   = customer.Email;
                    customerDB.Address = customer.Address;

                    db.SaveChanges();

                    TempData["SaveOK"] = "OK";

                    return(RedirectToAction("Viewcustomer", "Customer"));
                }
            }
            catch
            {
                ModelState.AddModelError("", "Save Failed! Try again later");
            }
            return(View());
        }