// Get all available sites for the current time and those who were not selected yet for the new route. private List <sites> getAvailableSites(smart_trip_dbEntities se, List <sites> sitesDbInfo, List <sites> newRoute, string currentTime) { // Remove all sites that were already assigned to previous hours in the new route. foreach (sites siteInNewRoute in newRoute) { foreach (sites siteInDb in sitesDbInfo.ToList()) { if (siteInDb.id == siteInNewRoute.id) { sitesDbInfo.Remove(siteInDb); } } } // Remove all closed sites for the current time. foreach (sites siteInDb in sitesDbInfo.ToList()) { bool isOpen = checkIfSiteIsOpen(se, siteInDb, currentTime); if (!isOpen) { sitesDbInfo.Remove(siteInDb); } } return(sitesDbInfo); }
public HttpResponseMessage getRoute(int routeId) { using (smart_trip_dbEntities se = new smart_trip_dbEntities()) { var route = se.routes.Where(s => s.id == routeId).ToList(); return(rh.HandleResponse(new { route = route })); } }
public HttpResponseMessage getAllSeasons() { using (smart_trip_dbEntities se = new smart_trip_dbEntities()) { var seasons = se.seasons.ToList(); return(rh.HandleResponse(new { seasons = seasons })); } }
public HttpResponseMessage getAllCompositions() { using (smart_trip_dbEntities se = new smart_trip_dbEntities()) { var compositions = se.composition.ToList(); return(rh.HandleResponse(new { compositions = compositions })); } }
public HttpResponseMessage getSiteOpeningHours(int siteId) { using (smart_trip_dbEntities se = new smart_trip_dbEntities()) { var openingHours = se.sites_opening_hours.Where(s => s.id == siteId).ToList(); return(rh.HandleResponse(new { site_opening_hours = openingHours })); } }
public HttpResponseMessage getAllCitySitesTypes(CitySiteType siteType) { using (smart_trip_dbEntities se = new smart_trip_dbEntities()) { var sites = se.sites.Where(s => s.city_id != null && s.city_id == siteType.cityId && s.types != null && s.types.Contains(siteType.typeId.ToString())).ToList(); return(rh.HandleResponse(new { city_sites = sites })); } }
public HttpResponseMessage getAllSites(int city_id) { using (smart_trip_dbEntities se = new smart_trip_dbEntities()) { var sites = se.sites.Where(s => s.city_id != null && s.city_id == city_id).ToList(); return(rh.HandleResponse(new { city_sites = sites })); } }
public HttpResponseMessage getAll() { using (smart_trip_dbEntities se = new smart_trip_dbEntities()) { var cities = se.cities.ToList(); return(rh.HandleResponse(new { cities = cities })); } }
private users getUserIfExists(smart_trip_dbEntities se, String userName, String password, String deviceToken) { try { return(se.users.FirstOrDefault(o => (o.uname.Equals(userName) && o.password.Equals(password)) || (!string.IsNullOrEmpty(o.device_token) && o.device_token.Equals(deviceToken)))); } catch (Exception ex) { return(null); } }
// Add new route to the saved route in the DB. private void addToSavedRoute(smart_trip_dbEntities se, List <sites> newRoute, int cityId) { var route = new routes(); route.name = "Route number " + (se.routes.Count() + 1).ToString(); route.city_id = cityId; route.user_id = ADMIN_ID; route.sites = getSitesString(newRoute); route.image_url = NEW_YORK_IMAGE_URL; route.rate = DEFAULT_RATE; route.description = DEFAULT_DESCRIPTION; se.routes.Add(route); se.SaveChanges(); }
// Check if site is open for a visit in the current time. private bool checkIfSiteIsOpen(smart_trip_dbEntities se, sites siteInDb, String currentTime) { // Get current day number. String openingHours = getSiteHours(se, (int)siteInDb.id); if (openingHours.Equals(OPEN_24_HOURS)) { return(true); } else if (openingHours.Equals(CLOSED)) { return(false); } else { return(currentTimeInRange(currentTime, openingHours)); } }
public HttpResponseMessage calcualte(RouteRequest userRequest) { using (smart_trip_dbEntities se = new smart_trip_dbEntities()) { String currentTime = userRequest.startTime; int numberOfSitesInRoute = getNumberOfSites(currentTime, userRequest.endTime); List <sites> sitesByUserPerferances = se.sites.Where(s => (s.compositions != null && s.compositions.Contains(userRequest.compositionId.ToString())) && (s.seasons != null && s.seasons.Contains(userRequest.seasonId.ToString())) && (s.city_id != null && s.city_id == userRequest.cityId)).ToList(); List <String> hoursPerSite = new List <String>(); if (sitesByUserPerferances.Count == 0) { return(rh.HandleResponse(new { HttpStatusCode.BadRequest, failed_calculation = ROUTE_FINDER_ERROR_MEASSAGE })); } else { List <sites> newRoute = new List <sites>(); Random rnd = new Random(); bool endOfCalculation = false; int i; for (i = 0; i < numberOfSitesInRoute && !endOfCalculation; i++) { // Get all available sites full info. List <sites> availableSites = new List <sites>(sitesByUserPerferances); availableSites = getAvailableSites(se, availableSites, newRoute, currentTime); if (availableSites.Count == 0) { endOfCalculation = true; } else { // Rand a site from avaialable sites for a visit and add for the new route. int siteIndex = rnd.Next(0, availableSites.Count); hoursPerSite.Add(currentTime); newRoute.Add(availableSites.ElementAt(siteIndex)); currentTime = getTimeForNextSite(currentTime); } } addToSavedRoute(se, newRoute, userRequest.cityId); return(rh.HandleResponse(new { route = newRoute, hours = hoursPerSite })); } } }
// --------------------------------- Server internal methods --------------------------------- // // Check if user exits in the DB. private bool isUserExists(smart_trip_dbEntities se, String userName, String password, String deviceToken) { try { bool userExists = se.users.Any(o => (o.uname.Equals(userName) && o.password.Equals(password)) || (o.device_token.Equals(deviceToken))); if (userExists) { return(true); } else { return(false); } } catch (Exception) { return(false); } }
// Get site opening hours from today. private string getSiteHours(smart_trip_dbEntities se, int id) { DateTime ClockInfoFromSystem = DateTime.Now; int day = (int)ClockInfoFromSystem.DayOfWeek; string openingHours = null; switch (day) { case 0: // Sunday. openingHours = (from h in se.sites_opening_hours where h.id == id select h.sunday).FirstOrDefault(); break; case 1: // Monday. openingHours = (from h in se.sites_opening_hours where h.id == id select h.monday).FirstOrDefault(); break; case 2: // Tueday. openingHours = (from h in se.sites_opening_hours where h.id == id select h.tuesday).FirstOrDefault(); break; case 3: // Wednesday. openingHours = (from h in se.sites_opening_hours where h.id == id select h.wednesday).FirstOrDefault(); break; case 4: // Thursday. openingHours = (from h in se.sites_opening_hours where h.id == id select h.thursday).FirstOrDefault(); break; case 5: // Friday. openingHours = (from h in se.sites_opening_hours where h.id == id select h.friday).FirstOrDefault(); break; case 6: // Saturday. openingHours = (from h in se.sites_opening_hours where h.id == id select h.saturday).FirstOrDefault(); break; } return(openingHours); }
public HttpResponseMessage login(UserConnection user) { using (smart_trip_dbEntities se = new smart_trip_dbEntities()) { var user_ = getUserIfExists(se, user.userName, user.password, user.deviceToken); if (user_ == null) { user_ = new users(); user_.uname = user.userName; user_.password = user.password; user_.device_token = user.deviceToken; se.users.Add(user_); se.SaveChanges(); } var cities = se.cities.ToList(); var sites = se.sites.Where(s => s.city_id == 1).ToList(); var categories = se.sites_types.ToList(); var recommandedRoutes = se.routes.Where(s => s.rate != null && s.rate >= 4).ToList(); return(rh.HandleResponse(new { user = user_, sites = sites, cities = cities, categories = categories, recommanded_routes = recommandedRoutes })); } }