예제 #1
0
        public int AddCity(City City)
        {
            if (City.CityID == 0)
            {
                City = context.City.Add(City);
            }
            else
            {
                context.Entry(City).State = EntityState.Modified;
            }
            context.SaveChanges();
            context.Entry(City).Reload();

            return City.CityID;
        }
예제 #2
0
        public ActionResult Create(viewModels.CreateCityViewModel Model)
        {
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                try
                {
                    Country country = repository
                        .Country
                        .FirstOrDefault(c => c.CountryID == Model.CountryID);
                    if (country == null)
                    {
                        ModelState.AddModelError("Country", "Страна не существует в базе");
                        return View(Model);
                    }

                    City city = repository
                        .City
                        .FirstOrDefault(c => c.Name == Model.City && c.Area == Model.Area && c.Region == Model.Region && c.CountryID == country.CountryID);
                    if (city != null)
                    {
                        ModelState.AddModelError("City", "Город (населенный пункт) с таким именем уже существует");
                        return View(Model);
                    }

                    city = new City()
                        {
                            Name = Model.City,
                            Area = Model.Area,
                            Region = Model.Region,
                            CountryID = country.CountryID
                        };

                    int userID = WebSecurity.CurrentUserId;
                    if (userID != 1)
                    {
                        UserProfile user = repository.Users.FirstOrDefault(u => u.UserId == userID);
                        //institution.UsersCanModify = new List<UserProfile>();
                        //institution.UsersCanModify.Add(user);
                    }
                    int cityID = repository.AddCity(city);

                    logger.Info("User " + WebSecurity.GetUserId(User.Identity.Name) + " \"" + User.Identity.Name +
                        "\" create city \"" + cityID + "\"");
                    TempData["SuccessMessage"] = "Город (населенный пункт) успешно создан!";
                    return RedirectToAction("Create");
                }
                catch (MembershipCreateUserException ex)
                {
                    logger.Warn("Error occured on user " + WebSecurity.GetUserId(User.Identity.Name) +
                        " \"" + User.Identity.Name + "\" city creating: ", ex);
                    TempData["ErrorMessage"] = "Произошла ошибка при создании города (населенного пункта)";
                }
            }

            return View(Model);
        }
예제 #3
0
        public void DeleteCity(City City)
        {
            var users = context
                .UserProfiles
                .Where(u => u.CityID == City.CityID);

            foreach (var item in users)
            {
                item.CityID = null;
                context.Entry(item).State = EntityState.Modified;
            }

            context.Entry(City).State = EntityState.Deleted;

            context.SaveChanges();

            return;
        }