public ActionResult _NewMarkerModalPartial() { var auth = DependencyResolver.Current.GetService <IAuthProvider>(); var userGuid = auth.UserGuid; var repo = DependencyResolver.Current.GetService <IRepository>(); var model = new NewMarkerModel(); ViewBag.Cities = repo.GetCities().Where(c => c.Id != 0).ToList(); ViewBag.Categories = repo.GetMarkerCategories(); ViewBag.Discounts = repo.GetDiscounts(); ViewBag.Statuses = repo.GetStatuses(userGuid); ViewBag.WeekDays = repo.GetWeekDays(); return(PartialView("Partial/_NewMarkerModalPartial", model)); }
public bool AddNewMarker(NewMarkerModel model, string openTimesString, string closeTimesString, HttpPostedFileBase markerPhoto, HttpPostedFileBase markerLogo, List <HttpPostedFileBase> markerPhotos, float lat, float lng) { if (Math.Abs(model.Lat) <= 0) { model.Lat = lat; } if (Math.Abs(model.Lng) <= 0) { model.Lng = lng; } var openTimes = JsonConvert.DeserializeObject <List <WorkTimeDay> >(openTimesString); var closeTimes = JsonConvert.DeserializeObject <List <WorkTimeDay> >(closeTimesString); var repo = DependencyResolver.Current.GetService <IRepository>(); var auth = DependencyResolver.Current.GetService <IAuthProvider>(); var userGuid = auth.UserGuid; if (markerPhoto != null) { FileProvider.DeleteFile(model.Photo); var filePath = FileProvider.SaveMarkerPhoto(markerPhoto); model.Photo = filePath; } if (markerLogo != null) { FileProvider.DeleteFile(model.Logo); var filePath = FileProvider.SaveMarkerLogo(markerLogo); model.Logo = filePath; } if (markerPhotos != null && markerPhotos.Any()) { model.marker_photos = new List <marker_photos>(); foreach (var photo in markerPhotos) { var filePath = FileProvider.SaveMarkerPhoto(photo); model.marker_photos.Add(new marker_photos { Photo = filePath }); } } if (string.IsNullOrEmpty(model.Name) && !string.IsNullOrEmpty(model.NameEn)) { model.Name = model.NameEn; } if (string.IsNullOrEmpty(model.Introduction) && !string.IsNullOrEmpty(model.IntroductionEn)) { model.Introduction = model.IntroductionEn; } if (string.IsNullOrEmpty(model.Description) && !string.IsNullOrEmpty(model.DescriptionEn)) { model.Description = model.DescriptionEn; } var tempNewMarkerId = repo.AddMarker(model, openTimes, closeTimes, userGuid); return(true); }
public bool EditMarker(NewMarkerModel model, string openTimesString, string closeTimesString, HttpPostedFileBase markerPhoto, HttpPostedFileBase markerLogo, IEnumerable <HttpPostedFileBase> markerPhotos, string removePhotosString) { var repo = DependencyResolver.Current.GetService <IRepository>(); var auth = DependencyResolver.Current.GetService <IAuthProvider>(); if (repo.GetCities().All(tc => tc.Id != model.CityId)) { model.CityId = 0; } if (string.IsNullOrEmpty(model.Street) || model.Street == "Unnamed Road") { model.Street = "Улица не определена"; } if (string.IsNullOrEmpty(model.House)) { model.House = "Нет"; } var openTimes = JsonConvert.DeserializeObject <List <WorkTimeDay> >(openTimesString); var closeTimes = JsonConvert.DeserializeObject <List <WorkTimeDay> >(closeTimesString); var removePhotos = JsonConvert.DeserializeObject <List <RemovePhoto> >(removePhotosString); var removePhotoIds = removePhotos.Select(p => p.id); var userGuid = auth.UserGuid; if (markerPhoto != null) { FileProvider.DeleteFile(model.Photo); var filePath = FileProvider.SaveMarkerPhoto(markerPhoto); model.Photo = filePath; } if (markerLogo != null) { FileProvider.DeleteFile(model.Logo); var filePath = FileProvider.SaveMarkerLogo(markerLogo); model.Logo = filePath; } if (markerPhotos != null && markerPhotos.Any()) { model.marker_photos = new List <marker_photos>(); foreach (var photo in markerPhotos) { var filePath = FileProvider.SaveMarkerPhoto(photo); model.marker_photos.Add(new marker_photos { Photo = filePath }); } } if (removePhotos != null && removePhotos.Any()) { foreach (var photo in removePhotos) { FileProvider.DeleteFile(photo.photo); } } if (string.IsNullOrEmpty(model.Name) && !string.IsNullOrEmpty(model.NameEn)) { model.Name = model.NameEn; } if (string.IsNullOrEmpty(model.Introduction) && !string.IsNullOrEmpty(model.IntroductionEn)) { model.Introduction = model.IntroductionEn; } if (string.IsNullOrEmpty(model.Description) && !string.IsNullOrEmpty(model.DescriptionEn)) { model.Description = model.DescriptionEn; } repo.EditMarker(model, openTimes, closeTimes, removePhotoIds?.ToList(), userGuid); return(true); }
public void EditMarker(NewMarkerModel model, List <WorkTimeDay> openTimes, List <WorkTimeDay> closeTimes, List <int> removePhotoIds, string userGuid) { var trans = _db.Database.BeginTransaction(); try { GetUserByGuid(userGuid); var newMarker = _db.marker.First(m => m.Id == model.Id); model.CopyTo(newMarker); newMarker.BaseCategoryId = model.BaseCategoryId; newMarker.CityId = model.CityId; newMarker.DiscountId = model.DiscountId; newMarker.StatusId = model.StatusId; if (model.StatusId == GetStatusByTag(MarkerStatuses.Published).Id) { newMarker.PublishedDate = DateTime.Now; } else if (model.StatusId == GetStatusByTag(MarkerStatuses.Checking).Id) { newMarker.CheckDate = DateTime.Now; newMarker.PublishedDate = null; } else { newMarker.CheckDate = null; newMarker.PublishedDate = null; } if (model.marker_photos != null && model.marker_photos.Count > 0) { _db.marker_photos.AddRange(model.marker_photos.Select(p => new marker_photos { MarkerId = newMarker.Id, Photo = p.Photo })); } if (removePhotoIds != null && removePhotoIds.Count > 0) { _db.marker_photos.RemoveRange(_db.marker_photos.Where(p => removePhotoIds.Contains(p.Id))); } _db.subcategory.RemoveRange(_db.subcategory.Where(s => s.MarkerId == newMarker.Id)); _db.phone.RemoveRange(_db.phone.Where(s => s.MarkerId == newMarker.Id)); _db.worktime.RemoveRange(_db.worktime.Where(s => s.MarkerId == newMarker.Id)); var subCategories = model.SubCategories?.Select(sc => new subcategory { CategoryId = sc, MarkerId = newMarker.Id }) .ToList() ?? new List <subcategory>(); var phones = model.Phones.Where(p => p.Length != 0) .Select(p => new phone { Number = p, MarkerId = newMarker.Id, Primary = false }).ToList(); var firstOrDefault = phones.FirstOrDefault(); if (firstOrDefault != null) { firstOrDefault.Primary = true; } var workTimes = openTimes.Join(closeTimes.Select(ct => new { ct.WeekDayId, CloseTime = ct.Time }), ot => ot.WeekDayId, ct => ct.WeekDayId, (ot, ct) => new { ot.WeekDayId, OpenTime = ot.Time, ct.CloseTime }) .Where(t => t.CloseTime != null && t.OpenTime != null) .ToList(); //собираем из двух частей полное время работы _db.subcategory.AddRange(subCategories); _db.phone.AddRange(phones); _db.worktime.AddRange(workTimes.Select(t => new worktime { CloseTime = t.CloseTime.Value, OpenTime = t.OpenTime.Value, MarkerId = newMarker.Id, WeekDayId = t.WeekDayId })); _db.SaveChanges(); trans.Commit(); } catch (Exception) { trans.Rollback(); throw; } }