public ActionResult ImportTransportTypes() { String[] transportTypes = new[] { "Автобусы", "Троллейбусы", "Маршрутные такси" }; using (DataContext dataContext = new DataContext()) { foreach (String typeName in transportTypes) { TransportType type = new TransportType() { Name = typeName }; dataContext.TransportTypes.Add(type); dataContext.SaveChanges(); } } return this.Content("Transport types imported"); }
public ActionResult Set(Guid id, Double lat, Double lng) { using (DataContext dataContext = new DataContext()) { Station station = dataContext.Stations.Find(id); if (station == null) { return this.Json(new { success = false, message = "Station not found`" }); } HandledStation handledStation = dataContext.HandledStations.FirstOrDefault(q => q.StationID == id); if (handledStation == null) { handledStation = new HandledStation() { StationID = id, UpdateTime = DateTime.Now }; dataContext.HandledStations.Add(handledStation); } station.Lat = lat; station.Lng = lng; dataContext.SaveChanges(); return this.Json(new { success = true }); } }
public static long CreateCompany(Company c) { using(var db = new DataContext()){ db.Companies.Add(c); db.SaveChanges(); } return c.Id; }
public ActionResult GetReplacements() { using (DataContext dataContext = new DataContext()) { IEnumerable<Replacement> replacements = dataContext.Replacements.ToList(); return this.Json(replacements, JsonRequestBehavior.AllowGet); } }
public static void CreateDish(Dish dish) { using(var db = new DataContext()) { var rest = db.Restaurants.Find(dish.Restaurant.Id); dish.Restaurant = rest; rest.Dishes.Add(dish); db.SaveChanges(); } }
public static Image CreateImage(string imgUrl) { var image = new Image { ImageUrl = imgUrl }; using (var db = new DataContext()) { db.Images.Add(image); db.SaveChanges(); } return image; }
public static Menu CreateMenu(Restaurant r, int week, int year, string info) { var m = new Menu() {Week = week, Year = year, Info = info}; using(var db = new DataContext()) { m.Restaurant = db.Restaurants.Find(r.Id); db.Menus.Add(m); db.SaveChanges(); } return m; }
public static long CreateLunchArea(LunchArea l) { using (var db = new DataContext()) { if(!db.LunchAreas.Any(la => la.Name.ToLower().Equals(l.Name.ToLower()))) { db.LunchAreas.Add(l); db.SaveChanges(); } } return l.Id; }
public static long CreateRestaurant(Restaurant r, List<long> areaIds) { using (var db = new DataContext()) { r.Company = db.Companies.Find(r.Company.Id); db.Restaurants.Add(r); foreach (var id in areaIds) { r.Areas.Add(db.LunchAreas.Find(id)); } db.SaveChanges(); } return r.Id; }
public static void CreateMenuDay(MenuDay md) { using(var db = new DataContext()) { foreach (var menuDish in md.Dishes) { menuDish.Dish.Restaurant = db.Restaurants.Find(menuDish.Dish.Restaurant.Id); } md.DayOfWeekId = (int) md.DayOfWeek; md.Menu = db.Menus.Find(md.Menu.Id); db.MenuDays.Add(md); db.SaveChanges(); } }
/// <summary> /// Gets this instance. /// </summary> /// <returns></returns> public ActionResult Get() { using (DataContext dataContext = new DataContext()) { IEnumerable<Object> stations = dataContext.Stations.OrderBy(q => q.Name) .Select(q => new { Station = q, Handled = dataContext.HandledStations.FirstOrDefault(h => h.StationID == q.ID) }) .ToList(); return this.Json(stations, JsonRequestBehavior.AllowGet); } }
public static long CreateCompany(CompanyCreateModel createModel) { var db = new DataContext(); var c = new Company(createModel.IsRestaurant) { Name = createModel.CompanyName, Organisationnr = createModel.Organisationnr, Information = createModel.Information, Notes = createModel.Notes, Latitude = createModel.Latitude, Longitude = createModel.Longitude, EniroId = createModel.EniroId, Email = createModel.Email, Url = createModel.Url, //PhoneNumbers = model.PhoneNumbers.Select(p => new PhoneNumber { // Number = p.Number, // Type = (PhoneNumberType) p.Type // }).ToList(), Adress = new Adress { PostCode = createModel.PostCode, Street = createModel.Street, PostArea = createModel.PostArea } }; try { db.Companies.Add(c); //c.Adress.City = db.Cities.Find(c.Adress.City.Id); if (createModel.IsRestaurant) { var r = new Restaurant(); var area = db.LunchAreas.Find(createModel.LunchAreaId); r.SetDataFromCompany(c, area); r.Areas.Add(area); db.Restaurants.Add(r); } db.SaveChanges(); return c.Id; } catch (Exception ex) { throw new Exception(); } }
// GET: api/Search public IEnumerable <Tag> Find(string term) { List <Tag> results = new List <Models.Tag>(); using (DataContext db = new Models.DataContext()) { var terms = term.Split(' '); results = db.Tags.Include("Organisation") .Include("Product") .Include("Article") .Include("Contact") .Include("Event") .Where(t => terms.Any(tag => t.TagValue.Contains(tag))).ToList(); } return(results); }
public static long CreateAdvertise(Company c, List<LunchArea> areas, Image img) { var ad = new Advertise { Company = c, Image = img }; using (var db = new DataContext()) { c.Advertises.Add(ad); var dbAreas = areas.Select(a => db.LunchAreas.Find(a.Id)).ToList(); var advertiseAreas = dbAreas.Select(a => new AdvertiseArea { LunchArea = a, Advertise = ad }); //var advertiseAreas = areas.Select(a => new AdvertiseArea { LunchArea = a, Advertise = ad }); foreach (var a in advertiseAreas) { ad.Areas.Add(a); } db.SaveChanges(); } return ad.Id; }
public static long CreateLunchArea(LunchAreaCreateModel model) { var l = new LunchArea() { Description = model.Description, Name = model.Name, Url = model.Url, LunchAreaStatus = LunchAreaStatus.New }; using (var db = new DataContext()) { db.LunchAreas.Add(l); db.SaveChanges(); } return l.Id; }
public ActionResult SetReplacement(String value, String replace) { using (DataContext dataContext = new DataContext()) { Replacement replacement = dataContext.Replacements.FirstOrDefault(q => q.Value == value); if (replacement == null) { replacement = new Replacement(); dataContext.Replacements.Add(replacement); } replacement.Value = value; replacement.ReplaceValue = replace; dataContext.SaveChanges(); return this.Json(new { success = true }); } }
public static IEnumerable<LunchArea> GetLunchAreas() { var db = new DataContext(); return db.LunchAreas.ToList().OrderBy(o => o.Name); }
public static LunchArea GetLunchAreaById(long id) { var db = new DataContext(); return db.LunchAreas.Find(id); }
//public static IEnumerable<Advertise> GetAdvertisesByLunchAreaId(long id) //{ // var db = new DataContext(); // return db.Advertises.Where(a => a.Company.City.Id == id); //} //public static IEnumerable<Restaurant> GetRestaurantsByCityId(long id) //{ // var db = new DataContext(); // return db.Restaurants.Where(r => r.City.Id == id); //} //public static IEnumerable<Dish> GetDishesByCityId(long id) //{ // var db = new DataContext(); // return db.Dishes.Where(d => d.Restaurant.City.Id == id); //} public static IEnumerable<Dish> GetDishesByLunchAreaId(long id) { var db = new DataContext(); return (IEnumerable<Dish>) db.Restaurants.Where(l => l.Areas.Any(a => a.Id == id)).Select(d => d.Dishes); //return db.Dishes.Where(d => d.Restaurant.Areas.Any(l => l.Id.Equals(id))); }
public static IEnumerable<Advertise> GetAddsByLunchAreaId(long areaId) { var db = new DataContext(); var areas = db.AdvertiseAreas.Where(x => x.LunchArea.Id == areaId).ToList(); return areas.Where(a => a.LunchArea.Id == areaId).Select(x => x.Advertise); }
private async Task DoParseStations() { HttpClient client = new HttpClient(); HttpResponseMessage response = await client.GetAsync("http://proezd.by/js/localdata.js"); String data = await response.Content.ReadAsStringAsync(); Int32 start = data.IndexOf('['); Int32 length = data.LastIndexOf(']') - start + 1; data = data.Substring(start, length); Int32 handledStationCount = 0; List<String> stations = JsonConvert.DeserializeObject<List<String>>(data); IHubContext progressHubContext = GlobalHost.ConnectionManager.GetHubContext<ProgressHub>(); using (DataContext dataContext = new DataContext()) { foreach (String stationName in stations) { String url = String.Format("http://proezd.by/stop?poisk_up={0}", stationName); String stationHtml = await (await client.GetAsync(url)).Content.ReadAsStringAsync(); HtmlDocument html = new HtmlDocument(); html.LoadHtml(stationHtml); HtmlNode document = html.DocumentNode; HtmlNode listNode = document.QuerySelector(".spisok_na_vivod_33"); dataContext.SaveChanges(); foreach (HtmlNode infoNode in listNode.QuerySelectorAll(".vivod_33 a")) { String hrefAttr = infoNode.GetAttributeValue("href", String.Empty); String name = infoNode.InnerText; String description = infoNode .QuerySelector(".opisianie_oostanovok").InnerText .Replace("(", String.Empty) .Replace(")", String.Empty); String stationCodeStr = hrefAttr.Replace("?id=", String.Empty); Int32 code = 0; if (!Int32.TryParse(stationCodeStr, out code)) { continue; } Station station = new Station() { Code = code, Name = name, }; dataContext.Stations.Add(station); dataContext.SaveChanges(); } handledStationCount++; Info info = new Info() { Progress = ((handledStationCount == stations.Count) ? 1 : ((Single)handledStationCount / stations.Count)) * 100, Operation = stationName }; progressHubContext.Clients.All.onProgress(info); } } }
public static IEnumerable<Company> GetCompaniesAll() { var db = new DataContext(); return db.Companies.OrderBy(o => o.Name); }
public static bool EniroIdExists(int eniroId) { var db = new DataContext(); return db.Companies.Any(c => c.EniroId.Equals(eniroId)); }
public async Task ParseSchedule() { using (HttpClient client = new HttpClient()) using (DataContext dataContext = new DataContext()) { IEnumerable<Station> stations = dataContext.Stations.ToList(); foreach (Station station in stations) { String data = await client.GetStringAsync(String.Format("http://proezd.by/stop?id={0}", station.Code)); HtmlDocument htmlDocument = new HtmlDocument(); htmlDocument.LoadHtml(data); HtmlNode document = htmlDocument.DocumentNode; TransportType busType = dataContext.TransportTypes.FirstOrDefault( q => q.Name.StartsWith("Автобус", StringComparison.InvariantCultureIgnoreCase)); if (busType != null) { HtmlNode busesNode = document.QuerySelector(".vivod_avtobusov_po_namber_ouan"); IEnumerable<HtmlNode> schRows = busesNode.QuerySelectorAll(".st_hr"); foreach (HtmlNode schRow in schRows) { HtmlNode transportInfoNode = schRow.QuerySelector(".naprovleniu_v_avtobusov_v_spiske_marshrut a"); String numbStr = transportInfoNode.GetAttributeValue("href", String.Empty); String number = numbStr.Substring(numbStr.IndexOf('_') + 1); String name = transportInfoNode.InnerText; Transport transport = dataContext.Transports.FirstOrDefault(q => q.Name == number); if (transport == null) { transport = new Transport() { Name = number, TypeID = busType.ID }; dataContext.SaveChanges(); } } } } } }
private async Task DoParseRoutes(String routesListUrl, String transportTypeName, String hrefBegin) { Int32 handledRouteCount = 0; IHubContext progressHubContext = GlobalHost.ConnectionManager.GetHubContext<ProgressHub>(); using (HttpClient client = new HttpClient()) using (DataContext dataContext = new DataContext()) { Guid busTypeId = dataContext.TransportTypes.First(x => x.Name.Equals(transportTypeName)).ID; String pageContent = await client.GetStringAsync(routesListUrl); HtmlDocument htmlDocument = new HtmlDocument(); htmlDocument.LoadHtml(pageContent); HtmlNode document = htmlDocument.DocumentNode; List<HtmlNode> busRouteLinks = document.QuerySelectorAll(".naprovlenie_odin_variant a").ToList(); List<String> busRouteLinksHref = busRouteLinks.Select(busRoute => busRoute.GetAttributeValue("href", String.Empty)).ToList(); // Extract route number List<String> routes = busRouteLinksHref.Select(x => { String temString = x.Remove(0, hrefBegin.Length); temString = temString.Split('&')[0]; return temString; }).Distinct().ToList(); foreach (String route in routes) { Transport transport = new Transport { Name = route, TypeID = busTypeId }; dataContext.Transports.Add(transport); dataContext.SaveChanges(); handledRouteCount++; Info info = new Info() { Progress = ((handledRouteCount == routes.Count) ? 1 : ((Single)handledRouteCount / routes.Count)) * 100, Operation = route }; progressHubContext.Clients.All.onProgress(info); } } }
private async Task DoParseDirections() { Int32 handledRouteCount = 0; IHubContext progressHubContext = GlobalHost.ConnectionManager.GetHubContext<ProgressHub>(); using (HttpClient client = new HttpClient()) using (DataContext dataContext = new DataContext()) { // Get all transports List<Transport> transports = dataContext.Transports.ToList(); List<Station> stations = dataContext.Stations.ToList<Station>(); // Get href attributes for bus List<String> hrefs = new List<String>(); String pageContent = await client.GetStringAsync(ProezdByUrlDictionary.BusRoutesList); hrefs.AddRange(ExtractDirectionStationsPageUrl(pageContent)); pageContent = await client.GetStringAsync(ProezdByUrlDictionary.TrolleybusRoutesList); hrefs.AddRange(ExtractDirectionStationsPageUrl(pageContent)); foreach (Transport transport in transports) { // Get transport type TransportType transportType = dataContext.TransportTypes.First(x => x.ID.Equals(transport.TypeID)); switch (transportType.Name) { case ("Автобусы"): { #region Up direction String upDirectionUrl = String.Format(ProezdByUrlDictionary.BusUpDirectionUrl, transport.Name); if (hrefs.Contains(upDirectionUrl)) { pageContent = await client.GetStringAsync(upDirectionUrl); List<Int32> directionStationsCodes = ExtractDirectionStationsCodes(pageContent); // Create direction Direction direction = new Direction { Name = transport.Name, TransportID = transport.ID, StartID = stations.AsParallel().First(x => x.Code.Equals(directionStationsCodes.First())).ID, EndID = stations.AsParallel().First(x => x.Code.Equals(directionStationsCodes.Last())).ID }; dataContext.Directions.Add(direction); dataContext.SaveChanges(); // Create direction stations for (Int32 i = 0; i < directionStationsCodes.Count; i++) { dataContext.DirectionStations.Add(new DirectionStation { DirectionID = direction.ID, StationID = stations.AsParallel().First(x => x.Code.Equals(directionStationsCodes[i])).ID, Order = i }); } dataContext.SaveChanges(); } #endregion #region Down direction String downDirectionUrl = String.Format(ProezdByUrlDictionary.BusDownDirectionUrl, transport.Name); if (hrefs.Contains(downDirectionUrl)) { pageContent = await client.GetStringAsync(downDirectionUrl); List<Int32> directionStationsCodes = ExtractDirectionStationsCodes(pageContent); // Create direction Direction direction = new Direction { Name = transport.Name, TransportID = transport.ID, StartID = stations.AsParallel().First(x => x.Code.Equals(directionStationsCodes.First())).ID, EndID = stations.AsParallel().First(x => x.Code.Equals(directionStationsCodes.Last())).ID }; dataContext.Directions.Add(direction); dataContext.SaveChanges(); // Create direction stations for (Int32 i = 0; i < directionStationsCodes.Count; i++) { dataContext.DirectionStations.Add(new DirectionStation { DirectionID = direction.ID, StationID = stations.AsParallel().First(x => x.Code.Equals(directionStationsCodes[i])).ID, Order = i }); } dataContext.SaveChanges(); } #endregion } break; case ("Троллейбусы"): { #region Up direction String upDirectionUrl = String.Format(ProezdByUrlDictionary.TrolleybusUpDirectionUrl, transport.Name); if (hrefs.Contains(upDirectionUrl)) { pageContent = await client.GetStringAsync(upDirectionUrl); List<Int32> directionStationsCodes = ExtractDirectionStationsCodes(pageContent); Direction direction = new Direction { Name = transport.Name, TransportID = transport.ID, StartID = stations.AsParallel().First(x => x.Code.Equals(directionStationsCodes.First())).ID, EndID = stations.AsParallel().First(x => x.Code.Equals(directionStationsCodes.Last())).ID }; dataContext.Directions.Add(direction); dataContext.SaveChanges(); // Create direction stations for (Int32 i = 0; i < directionStationsCodes.Count; i++) { dataContext.DirectionStations.Add(new DirectionStation { DirectionID = direction.ID, StationID = stations.AsParallel().First(x => x.Code.Equals(directionStationsCodes[i])).ID, Order = i }); } dataContext.SaveChanges(); } #endregion #region Down direction String downDirectionUrl = String.Format(ProezdByUrlDictionary.TrolleybusDownDirectionUrl, transport.Name); if (hrefs.Contains(downDirectionUrl)) { pageContent = await client.GetStringAsync(downDirectionUrl); List<Int32> directionStationsCodes = ExtractDirectionStationsCodes(pageContent); Direction direction = new Direction { Name = transport.Name, TransportID = transport.ID, StartID = stations.AsParallel().First(x => x.Code.Equals(directionStationsCodes.First())).ID, EndID = stations.AsParallel().First(x => x.Code.Equals(directionStationsCodes.Last())).ID }; dataContext.Directions.Add(direction); dataContext.SaveChanges(); // Create direction stations for (Int32 i = 0; i < directionStationsCodes.Count; i++) { dataContext.DirectionStations.Add(new DirectionStation { DirectionID = direction.ID, StationID = stations.AsParallel().First(x => x.Code.Equals(directionStationsCodes[i])).ID, Order = i }); } dataContext.SaveChanges(); } #endregion } break; } #region Handle progress handledRouteCount++; Info info = new Info() { Progress = ((handledRouteCount == transports.Count) ? 1 : ((Single)handledRouteCount / transports.Count)) * 100, Operation = transport.Name }; progressHubContext.Clients.All.onProgress(info); #endregion } } }
public static string GetTodaysNamesDay(string key) { var db = new DataContext(); return db.NameDays.FirstOrDefault(n => n.Key == key).Names; }
public ClassTemplateCRUDViewModel(DataContext context) { SelectedTemplate = new ClassTemplate(); this._context = context; }
public CourseViewModel(DataContext context) { SelectedTeacher = new Teacher(); this._context = context; }
public static IEnumerable<Company> CompaniesByLunchArea(long id) { var db = new DataContext(); return db.Companies.Where(c => c.Restaurants.Any(r => r.Areas.Any(a => a.Id == id))); }
private async Task DoParseTime() { Int32 handledRouteCount = 0; IHubContext progressHubContext = GlobalHost.ConnectionManager.GetHubContext<ProgressHub>(); using (HttpClient client = new HttpClient()) using (DataContext dataContext = new DataContext()) { // Get all transports List<Transport> transports = dataContext.Transports.ToList(); List<Station> stations = dataContext.Stations.ToList<Station>(); // Get href attributes for bus List<String> hrefs = new List<String>(); foreach (Transport transport in transports) { foreach (Station station in stations) { String timeUrl = String.Format(ProezdByUrlDictionary.BusUpSheduleUrl, station.Code, transport.Name); String pageContent = await client.GetStringAsync(timeUrl); List<TimeStopTransport> dateTimes = GetTime(pageContent); if (dateTimes.Count > 0) { Schedule scheduleWorkDays = new Schedule { TransportID = transport.ID, StationID = station.ID, IsWeekend = false }; Schedule scheduleWeekend = new Schedule { TransportID = transport.ID, StationID = station.ID, IsWeekend = true }; dataContext.Schedules.Add(scheduleWorkDays); dataContext.Schedules.Add(scheduleWeekend); dataContext.SaveChanges(); foreach (TimeStopTransport item in dateTimes) { ScheduleItem scheduleItem = new ScheduleItem { ScheduleID = item.IsWeekend ? scheduleWeekend.ID : scheduleWorkDays.ID, Time = new DateTime(2013, 5, 5, item.Hour, item.Minute, 0) }; dataContext.ScheduleItems.Add(scheduleItem); dataContext.SaveChanges(); } } } #region Handle progress handledRouteCount++; Info info = new Info() { Progress = ((handledRouteCount == transports.Count) ? 1 : ((Single)handledRouteCount / transports.Count)) * 100, Operation = transport.Name }; progressHubContext.Clients.All.onProgress(info); #endregion } } }