public ActionResult AddPlace(PlaceAddForm model) { List<ShortPlace> places = new List<ShortPlace>(); using (var db = new RazomContext()) { PlaceSearchEngine.PlaceSearchEngine engine = new PlaceSearchEngine.PlaceSearchEngine(); List<int> placeIDs = engine.searchPlaces(model.Token); List<int> placeInThisTrip = (from pl in db.Places join tp in db.TravelPlaces on pl.PlaceID equals tp.PlaceID join tr in db.Travels on tp.TravelID equals tr.TravelID where tr.TravelID == model.TripID select pl.PlaceID).ToList(); foreach (var item in placeIDs) { if (!placeInThisTrip.Contains(item)) { places.Add(db.Places.Where(p => p.PlaceID == item).Select(p => new ShortPlace { ID = p.PlaceID, Name = p.Name }).FirstOrDefault()); } } } int pageSize = 6; int pCount = 0; if (places.Count % pageSize == 0) { pCount = places.Count / pageSize; } else pCount = places.Count / pageSize + 1; places = places.Take(pageSize).ToList(); PlaceAddForm paf = new PlaceAddForm() { Token = model.Token, TripID = model.TripID, PlacesInfo = new PlaceCollection() { Places = places, CurrentPage = 1, PagesCount = pCount } }; return View(paf); }
private List<ShortPlace> getSearchList(string token) { PlaceSearchEngine.PlaceSearchEngine engine = new PlaceSearchEngine.PlaceSearchEngine(); List<int> ids = engine.searchPlaces(token); List<ShortPlace> result = new List<ShortPlace>(); using (var db = new RazomContext()) { ShortPlace sp; foreach (int item in ids) { Places p = db.Places.Find(item); if (p == null) { continue; } List<string> Tags = (from t in db.Tag join ttp in db.TagToPlace on t.TagID equals ttp.TagID where ttp.PlaceID == item select t.NameTag).ToList(); var photo = db.PhotosPlace.Where(ph => ph.FileFoto != null && ph.PlaceID == p.PlaceID).ToList(); var photoUrl = db.PhotosPlace.Where(ph => ph.href != null && ph.PlaceID == p.PlaceID).ToList(); sp = new ShortPlace { Name = p.Name, City = db.Region.Find(p.CityID).Name, PlaceID = p.PlaceID, PhotoByte = photo.Count != 0 ? photo.First().FotoID : 0, PhotoUrl = photoUrl.Count != 0 ? photoUrl.First().href : "", Rating = p.Rating ?? 0, PlaceType = p.PlaceType != null ? db.PlaceType.Find(p.PlaceTypeID).Type : string.Empty, tags = Tags }; result.Add(sp); } } return result; }
public ActionResult PagerAddPlace(FormCollection form) { List<ShortPlace> places = new List<ShortPlace>(); int page = int.Parse(form["page"].ToString()); int travel_id = int.Parse(form["t_id"].ToString()); string token = form["id"].ToString(); using (var db = new RazomContext()) { PlaceSearchEngine.PlaceSearchEngine engine = new PlaceSearchEngine.PlaceSearchEngine(); List<int> placeIDs = engine.searchPlaces(token); List<int> placeInThisTrip = (from pl in db.Places join tp in db.TravelPlaces on pl.PlaceID equals tp.PlaceID join tr in db.Travels on tp.TravelID equals tr.TravelID where tr.TravelID == travel_id select pl.PlaceID).ToList(); foreach (var item in placeIDs) { if (!placeInThisTrip.Contains(item)) { places.Add(db.Places.Where(p => p.PlaceID == item).Select(p => new ShortPlace { ID = p.PlaceID, Name = p.Name }).FirstOrDefault()); } } } int pageSize = 6; int pCount = 0; if (places.Count % pageSize == 0) { pCount = places.Count / pageSize; } else pCount = places.Count / pageSize + 1; places = places.Skip((page - 1) * pageSize).Take(pageSize).ToList(); PlaceAddForm paf = new PlaceAddForm() { Token = token, TripID = travel_id, PlacesInfo = new PlaceCollection() { Places = places, CurrentPage = page, PagesCount = pCount } }; return View("AddPlace",paf); }