public ActionResult EditPlaceInfo(Place place) { if (ModelState.IsValid) { Place model = db.Places.Find(place.Id); if (model.SubCategoryId != place.SubCategoryId) { SubCategory sc = db.SubCategories.Find(model.SubCategoryId); sc.NumberOfPlace -= 1; db.Entry(sc).State = EntityState.Modified; db.SaveChanges(); sc = db.SubCategories.Find(place.SubCategoryId); sc.NumberOfPlace += 1; db.Entry(sc).State = EntityState.Modified; db.SaveChanges(); } if (!model.Address.Equals(place.Address)) { Coordinate coordinate = CoordinateExtension.GetCoordinates(model.Address); if (coordinate != null) { coordinate.PlaceId = place.Id; db.Entry(coordinate).State = EntityState.Modified; db.SaveChanges(); } else { coordinate = new Coordinate { PlaceId = place.Id, Latitude = 16.0466743, Longitude = 108.206706 }; db.Entry(coordinate).State = EntityState.Modified; db.SaveChanges(); } } model.Name = place.Name; model.Address = place.Address; model.SubCategoryId = place.SubCategoryId; model.DistrictId = place.DistrictId; model.PhoneNumber = place.PhoneNumber; model.Description = place.Description; model.StartTime = place.StartTime; model.EndTime = place.EndTime; db.Entry(model).State = EntityState.Modified; db.SaveChanges(); foreach (var tag in db.AppliedTagPlaces.Where(t => t.PlaceId == place.Id)) { db.AppliedTagPlaces.Remove(tag); } db.SaveChanges(); if (place.SelectedTagIds != null) { foreach (var tagId in place.SelectedTagIds) { AppliedTagPlace appliedTag = new AppliedTagPlace { PlaceId = place.Id, TagId = tagId, AppliedDate = DateTime.Now }; db.AppliedTagPlaces.Add(appliedTag); } db.SaveChanges(); } return Json(new { success = true }); } return Json(new { success = false, id = place.Id }); }
public ActionResult Create(Place model) { if (ModelState.IsValid) { model.IsApproved = true; //model.IsApproved = false; //if (Roles.IsUserInRole(User.Identity.Name, "Administrator")) //{ // model.IsApproved = true; //} Place place = new Place() { Name = model.Name, Address = model.Address, SubCategoryId = model.SubCategoryId, DistrictId = model.DistrictId, UserId = User.Identity.GetUserId<int>(), PhoneNumber = model.PhoneNumber, Description = model.Description, StartTime = model.StartTime, EndTime = model.EndTime, CreateDate = DateTime.Now, IsActive = true, IsApproved = model.IsApproved, RateValue = 0, View = 0, Virtualtourist = model.Virtualtourist }; db.Places.Add(place); db.SaveChanges(); Coordinate coordinate = CoordinateExtension.GetCoordinates(model.Address); if (coordinate != null) { coordinate.PlaceId = place.Id; db.Coordinates.Add(coordinate); db.SaveChanges(); } else { coordinate = new Coordinate { PlaceId = place.Id, Latitude = 16.0466743, Longitude = 108.206706 }; } if (model.SelectedTagIds != null && model.SelectedTagIds.Count > 0) { foreach (var tag in model.SelectedTagIds) { AppliedTagPlace appliedTag = new AppliedTagPlace() { PlaceId = place.Id, TagId = tag, AppliedDate = place.CreateDate }; db.AppliedTagPlaces.Add(appliedTag); } db.SaveChanges(); } if (model.Items.Count > 0) { foreach (var item in model.Items) { Item addedItem = new Item() { Name = item.Name, Price = item.Price, PlaceId = place.Id, isActive = true }; db.Items.Add(addedItem); } db.SaveChanges(); } foreach (string fileName in Request.Files) { HttpPostedFileBase file = Request.Files[fileName]; //Save file content goes here List<string> list = ImageUploadExtension.renameUploadFile(file, "Resize"); if (list != null) { Image img = new Image() { ImagePath = list[0], ImageSmallPath = list[1], PlaceId = place.Id, }; db.Images.Add(img); } } db.SaveChanges(); SubCategory sub = db.SubCategories.Find(model.SubCategoryId); if (sub != null) { sub.NumberOfPlace += 1; db.Entry(sub).State = EntityState.Modified; db.SaveChanges(); } return Json(new { isSuccess = true, id = place.Id }); } return Json(new { isSuccess = false }); }