public ActionResult Edit(TitleEditView editView, string submitbutton)
        {
            //if (ModelState.IsValid)
            //{
            //    return ("Good");
            //}

            if (editView.Title.Name == null)
            {
                TempData["EmptyName"] = ViewData;
                return(RedirectToAction("Edit", editView.Title.Id));
            }
            Title title;

            if (submitbutton == "Save")
            {
                title = dbNew.Titles.Find(editView.Title.Id);
            }
            else
            {
                title = editView.Title;
            }
            if (title.Name != editView.Title.Name && dbNew.Titles.FirstOrDefault(co => co.Name == editView.Title.Name) != null)
            {
                TempData["NameExist"] = ViewData;
                return(RedirectToAction("Edit", title.Id));
                //return View("~/Views/Shared/Error.cshtml");
                //ModelState.AddModelError("", "Title Already Exist");
                //return View(editView);
            }

            else
            {
                title.JobAreas.Clear();
                title.Name       = editView.Title.Name;
                title.JobLevelId = editView.Title.JobLevelId;
                if (editView.selectedJobAreas != null)
                {
                    foreach (var c in dbNew.JobAreas.Where(area => editView.selectedJobAreas.Contains(area.Id)))
                    {
                        title.JobAreas.Add(c);
                    }
                }

                if (submitbutton == "Save")
                {
                    dbNew.Entry(title).State = EntityState.Modified;
                }
                else
                {
                    dbNew.Titles.Add(title);
                }
                dbNew.SaveChanges();
                return(RedirectToAction("Index"));
            }
        }
示例#2
0
        public static void MergeCompanies(DBPlatform context, string primaryMerge, IEnumerable <int> mergedIds)
        {
            if (String.IsNullOrEmpty(primaryMerge) || !mergedIds.Any())
            {
                return;
            }
            var primaryCompany = context.Companies.FirstOrDefault(comp => comp.name == primaryMerge);

            if (primaryCompany == null)
            {
                primaryCompany = new Company {
                    name = primaryMerge
                };
                context.Companies.Add(primaryCompany);
                context.Entry(primaryCompany).State = EntityState.Added;
            }
            var resCompanies = context.Companies.Include(co => co.Contacts)
                               .Where(comp => mergedIds.Contains(comp.Id)).ToList();

            foreach (var comp in resCompanies)
            {
                if (comp.Contacts.Count > 0)
                {
                    foreach (var cont in comp.Contacts.ToList())
                    {
                        cont.Company = primaryCompany;
                        context.Entry(cont).State = EntityState.Modified;
                    }

                    context.Companies.Remove(comp);
                }
            }

            context.SaveChanges();
        }
示例#3
0
        public ActionResult Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                User user = null;
                using (DBPlatform db = new DBPlatform())
                {
                    user = db.Users.FirstOrDefault(u => u.login == model.login);
                }
                if (user == null)
                {
                    // создаем нового пользователя
                    using (DBPlatform db = new DBPlatform())
                    {
                        db.Users.Add(new User {
                            login = model.login, password = model.Password, Name = model.Name, Role = "User"
                        });
                        db.SaveChanges();

                        user = db.Users.Where(u => u.login == model.login && u.password == model.Password).FirstOrDefault();
                    }

                    // если пользователь удачно добавлен в бд
                    if (user != null)
                    {
                        var authTicket = new FormsAuthenticationTicket(
                            1,                           // version
                            user.login,                  // user name
                            DateTime.Now,                // created
                            DateTime.Now.AddMinutes(20), // expires
                            true,                        // persistent?
                            user.Role                    // can be used to store roles
                            );

                        string encryptedTicket = FormsAuthentication.Encrypt(authTicket);

                        var authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
                        if (authTicket.IsPersistent)
                        {
                            authCookie.Expires = authTicket.Expiration;
                        }
                        System.Web.HttpContext.Current.Response.Cookies.Add(authCookie);

                        /*FormsAuthentication.SetAuthCookie(model.login, true);*/
                        return(RedirectToAction("Index", "Home"));
                    }
                }
                else
                {
                    ModelState.AddModelError("", $"User with login '{model.login}' already exist");
                }
            }

            return(View(model));
        }
示例#4
0
 public ActionResult DeleteUser(int?id)
 {
     using (DBPlatform db = new DBPlatform())
     {
         var user = db.Users.Find(id);
         if (user != null)
         {
             db.Users.Remove(user);
             db.SaveChanges();
         }
     }
     return(RedirectToAction("Users"));
 }
示例#5
0
        public ActionResult Edit(ContactEditView editView)
        {
            var cont = db.Contacts.Find(editView.Id);

            if (cont == null)
            {
                return(HttpNotFound());
            }
            if (cont.email != editView.email && db.Contacts.FirstOrDefault(con => con.email == editView.email) != null)
            {
                ModelState.AddModelError("", "Email already Exist");
                editView.updateInfo(db.Companies, db.Titles, db.Countries);
                return(View(editView));
            }
            else
            {
                cont.getNewInfo(editView);
                db.Entry(cont).State = EntityState.Modified;
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }
        }
示例#6
0
 public ActionResult DemodeUser(int?id)
 {
     using (DBPlatform db = new DBPlatform())
     {
         var user = db.Users.Find(id);
         if (user != null)
         {
             if (user.Role == "Moderator")
             {
                 user.Role            = "User";
                 db.Entry(user).State = EntityState.Modified;
                 db.SaveChanges();
             }
         }
     }
     return(RedirectToAction("Users"));
 }
示例#7
0
        public ActionResult Industries(string newName)
        {
            if (!String.IsNullOrEmpty(newName))
            {
                var ind = dbNew.Industries.FirstOrDefault(i => i.Name == newName);
                if (ind == null)
                {
                    var umMapped = dbNew.IndustryAliases.FirstOrDefault(al => al.Name == newName);
                    if (umMapped != null)
                    {
                        dbNew.IndustryAliases.Remove(umMapped);
                    }
                    dbNew.Industries.Add(new Industry(newName));
                    dbNew.SaveChanges();
                }
            }

            ViewBag.unmappedAliases = dbNew.IndustryAliases.Where(a => a.Industry == null).ToList();
            return(View(dbNew.Industries.ToList()));
        }
        public ActionResult Edit(CompanyEditView editView, int[] selectedIndustries, string submitbutton)
        {
            if (editView.company.name == null)
            {
                TempData["EmptyName"] = ViewData;
                return(RedirectToAction("Edit", editView.company.Id));
            }
            if (!editView.company.isMinMaxGood())
            {
                TempData["MinMax"] = ViewData;
                return(RedirectToAction("Edit", editView.company.Id));
            }
            Company comp = new Company();

            if (submitbutton == "Save")
            {
                comp = dbNew.Companies.Find(editView.company.Id);
                if (comp == null)
                {
                    return(HttpNotFound());
                }
            }

            if (comp.name != editView.company.name && dbNew.Companies.FirstOrDefault(co => co.name == editView.company.name) != null)
            {
                TempData["NameExist"] = ViewData;
                return(RedirectToAction("Edit", comp.Id));
            }
            else
            {
                comp.Industries.Clear();
                comp.getCompanyChanges(editView.company);

                if (editView.company.PrimaryIndustryId == null && selectedIndustries != null)
                {
                    comp.PrimaryIndustryId = selectedIndustries[new Random().Next(0, selectedIndustries.Count())];
                }
                else if (editView.company.PrimaryIndustryId != null && selectedIndustries == null)
                {
                    comp.Industries.Add(dbNew.Industries.Find(editView.company.PrimaryIndustryId));
                }


                // comp.Industries.Clear();
                if (selectedIndustries != null)
                {
                    //получаем выбранные курсы
                    foreach (var c in dbNew.Industries.Where(ind => selectedIndustries.Contains(ind.Id)))
                    {
                        comp.Industries.Add(c);
                    }
                }


                if (submitbutton == "Save")
                {
                    dbNew.Entry(comp).State = EntityState.Modified;
                }
                else
                {
                    dbNew.Companies.Add(comp);
                }
                dbNew.SaveChanges();
                return(RedirectToAction("Index"));
            }
        }