public async Task <HttpResult> AddUpdateGymFinderGym([FromBody] GymFinderGym gym) { try { //User editor = db.User.Find(gym.OwnerId); bool userLoggedIn = Functions.UserLoggedIn(Request, out User editor); bool adminLoggedIn = Functions.AdminLoggedIn(Request, out _); //if ((gym.Id > 0 && !adminLoggedIn ) //gym belongs to someone // && ( // (!adminLoggedIn && !userLoggedIn) //no user logged in // || (userLoggedIn && editor.Id != gym.OwnerId) ) //or user logged in but does not own the gym // ) //{ // throw new Exception("Not logged in!"); //} if (gym.Id > 0 && !userLoggedIn && !adminLoggedIn) { //gym belongs to someone but no user logged in throw new Exception("Not logged in!"); } else if (gym.Id > 0 && !adminLoggedIn && userLoggedIn && editor.Id != gym.OwnerId) { //user is not admin, and gym does not belong to this user throw new Exception("Not logged in!"); } //all okay, gym belongs to user, or user is admin. DateTime now = DateTime.Now; CityGeo city = db.CityGeo.Find(gym.LocationCityId); gym.LocationCountryId = db.CountryGeo.FirstOrDefault(x => x.CountryName == city.CountryName).Id; if (gym.Id > 1) { GymFinderGym updating = db.GymFinderGym.Find(gym.Id); Functions.CheckNull(updating); if (!string.IsNullOrEmpty(gym.Website) && gym.Website.StartsWith("www")) { gym.Website = "https://" + gym.Website; } if (!string.IsNullOrEmpty(gym.Youtube) && gym.Youtube.StartsWith("www")) { gym.Youtube = "https://" + gym.Youtube; } if (!string.IsNullOrEmpty(gym.Facebook) && gym.Facebook.StartsWith("www")) { gym.Facebook = "https://" + gym.Facebook; } if (!string.IsNullOrEmpty(gym.Twitter) && gym.Twitter.StartsWith("www")) { gym.Twitter = "https://" + gym.Twitter; } if (!string.IsNullOrEmpty(gym.Instagram) && gym.Instagram.StartsWith("www")) { gym.Instagram = "https://" + gym.Instagram; } if (!string.IsNullOrEmpty(gym.Linkedin) && gym.Linkedin.StartsWith("www")) { gym.Linkedin = "https://" + gym.Instagram; } updating.Phone = gym.Phone; updating.Website = gym.Website; updating.Facebook = gym.Facebook; updating.Twitter = gym.Twitter; updating.Instagram = gym.Instagram; updating.AverageRating = gym.AverageRating; updating.Cafe = gym.Cafe; updating.CardioMachines = gym.CardioMachines; updating.ChangingRooms = gym.ChangingRooms; updating.ClassesAvailable = gym.ClassesAvailable; updating.Crossfit = gym.Crossfit; updating.Description = gym.Description; updating.FreeWeightsBarsPlates = gym.FreeWeightsBarsPlates; updating.FreeWeightsDumbbells = gym.FreeWeightsDumbbells; updating.LocationCityId = gym.LocationCityId; updating.LocationCountryId = gym.LocationCountryId; updating.LocationLat = (double)city.Latitude; updating.LocationLong = (double)city.Longitude; updating.MembersOnly = gym.MembersOnly; updating.Name = gym.Name; updating.NoMembershipRequired = gym.NoMembershipRequired; updating.OlympicLifting = gym.OlympicLifting; updating.Other = gym.Other; updating.Physio = gym.Physio; updating.Powerlifting = gym.Powerlifting; updating.ResistanceMachines = gym.ResistanceMachines; updating.Sauna = gym.Sauna; updating.OwnerId = gym.OwnerId; updating.StreetAddress = gym.StreetAddress; updating.SwimmingPool = gym.SwimmingPool; updating.Toilets = gym.Toilets; updating.Lockers = gym.Lockers; updating.Strongman = gym.Strongman; updating.TwentyFourHour = gym.TwentyFourHour; updating.VendingMachine = gym.VendingMachine; updating.LocationCityName = city.CityName; updating.LocationCountryName = city.CountryName; updating.Status = gym.Status; updating.ModifiedDate = now; updating.Whatsapp = gym.Whatsapp; updating.Linkedin = gym.Linkedin; updating.GooglePlus = gym.GooglePlus; updating.Snapchat = gym.Snapchat; updating.Skype = gym.Skype; updating.Youtube = gym.Youtube; db.Entry(updating).State = Microsoft.EntityFrameworkCore.EntityState.Modified; await db.SaveChangesAsync(); } else { gym.CreationDate = now; gym.ModifiedDate = now; gym.LocationLat = (double)city.Latitude; gym.LocationLong = (double)city.Longitude; gym.LocationCityName = city.CityName; gym.LocationCountryName = city.CountryName; gym.Status = (int)Enums.GymStatus.Pending; gym.AverageRating = 5d; db.GymFinderGym.Add(gym); await db.SaveChangesAsync(); } return(new HttpResult(true, gym, "")); } catch (Exception e) { return(new HttpResult(false, null, Functions.ErrorMessage(e))); } }
public async Task <HttpResult> Search([FromBody] GymSearch q) { try { int takeAmount = 10; int pageNum = q.Page; string k = q.Keywords.ToLower().Trim(); //if (string.IsNullOrEmpty(k)) // k = " "; ////for some reason search takes less time if given keyword is not empty... List <string> queryWords = k.Replace(',', ' ').Split(' ').Where(x => x.Length > 0).ToList(); IQueryable <GymFinderGym> gyms = (from g in db.GymFinderGym //let citySearch = city.Id > 0 where ( (g.Status == q.Status || q.Status == (int)Enums.GymStatus.Any) && ( string.IsNullOrEmpty(k) || queryWords.Any(w => g.Name.ToLower().Contains(w)) || queryWords.Any(w => g.StreetAddress.ToLower().Contains(w)) || queryWords.Any(w => g.LocationCityName.ToLower().Contains(w)) || queryWords.Any(w => g.LocationCountryName.ToLower().Contains(w)) || queryWords.All(w => g.Description.ToLower().Contains(w)) ) //&& //( // q.CityID == city.Id || !citySearch //) && (q.Cafe == g.Cafe || q.Cafe != 1) && (q.CardioMachines == g.CardioMachines || q.CardioMachines != 1) && (q.ChangingRooms == g.ChangingRooms || q.ChangingRooms != 1) && (q.ClassesAvailable == g.ClassesAvailable || q.ClassesAvailable != 1) && (q.Crossfit == g.Crossfit || q.Crossfit != 1) && (q.FreeWeightsBarsPlates == g.FreeWeightsBarsPlates || q.FreeWeightsBarsPlates != 1) && (q.FreeWeightsDumbbells == g.FreeWeightsDumbbells || q.FreeWeightsDumbbells != 1) && (q.MembersOnly == g.MembersOnly || q.MembersOnly != 1) && (q.NoMembershipRequired == g.NoMembershipRequired || q.NoMembershipRequired != 1) && (q.OlympicLifting == g.OlympicLifting || q.OlympicLifting != 1) && (q.Physio == g.Physio || q.Physio != 1) && (q.Powerlifting == g.Powerlifting || q.Powerlifting != 1) && (q.ResistanceMachines == g.ResistanceMachines || q.ResistanceMachines != 1) && (q.Sauna == g.Sauna || q.Sauna != 1) && (q.SwimmingPool == g.SwimmingPool || q.SwimmingPool != 1) && (q.Toilets == g.Toilets || q.Toilets != 1) && (q.TwentyFourHour == g.TwentyFourHour || q.TwentyFourHour != 1) && (q.VendingMachine == g.VendingMachine || q.VendingMachine != 1) && (q.Strongman == g.Strongman || q.Strongman != 1) && (q.Lockers == g.Lockers || q.Lockers != 1) ) select g); int total = gyms.Count(); CityGeo city = await db.CityGeo.FindAsync(q.CityID); //if (city == null) // city = new CityGeo { Id = 0 }; if (city != null) {//sort by distance first if city selected GeoAPI.Geometries.IGeometryFactory geometryFactory = NtsGeometryServices.Instance.CreateGeometryFactory(srid: 4326); GeoAPI.Geometries.IPoint cityLocation = geometryFactory.CreatePoint(new GeoAPI.Geometries.Coordinate((double)city.Latitude, (double)city.Longitude)); gyms = gyms .OrderBy(x => new GeoAPI.Geometries.Coordinate(x.LocationLat, x.LocationLong).Distance(cityLocation.Coordinate)) .ThenByDescending(x => x.CreationDate); } else if (!string.IsNullOrEmpty(k)) {//else sort by keyword relevance only gyms = gyms .OrderByDescending(x => x.Name.ToLower().StartsWith(k)) .ThenByDescending(x => x.CreationDate); } else {//else sort by rating gyms = gyms.OrderByDescending(x => x.AverageRating); } gyms = gyms.Skip(pageNum * takeAmount).Take(takeAmount); return(new HttpResult(true, new { gyms, total }, "")); } catch (Exception e) { return(new HttpResult(false, null, Functions.ErrorMessage(e))); } }
public async Task <HttpResult> Search([FromBody] CoachSearch q) { try { int takeAmount = 15; int pageNum = q.Page; string k = q.Keywords.ToLower().Trim(); List <string> queryWords = k.Replace(',', ' ').Split(' ').Where(x => x.Length > 0).ToList(); var coaches = (from u in db.User //let citySearch = city.Id > 0 where ( u.IsCoach == 1 && (u.Status == q.Status || q.Status == (int)Enums.UserStatus.Active) && ( string.IsNullOrEmpty(k) || queryWords.Any(w => u.FirstName.ToLower().Contains(w)) || queryWords.Any(w => u.LastName.ToLower().Contains(w)) || queryWords.Any(w => u.CityName.ToLower().Contains(w)) || queryWords.Any(w => u.CountryName.ToLower().Contains(w)) || queryWords.All(w => u.CoachBio.ToLower().Contains(w)) ) && (u.IsVerified == q.IsVerfied || q.IsVerfied != 1) && (u.CoachBodybuilding == q.CoachBodybuilding || q.CoachBodybuilding != 1) && (u.CoachClasses == q.CoachClasses || q.CoachClasses != 1) && (u.CoachCrossfit == q.CoachCrossfit || q.CoachCrossfit != 1) && (u.CoachDance == q.CoachDance || q.CoachDance != 1) && (u.CoachMasseuse == q.CoachMasseuse || q.CoachMasseuse != 1) && (u.CoachNutrition == q.CoachNutrition || q.CoachNutrition != 1) && (u.CoachOlympicLifting == q.CoachOlympicLifting || q.CoachOlympicLifting != 1) && (u.CoachOneOnOne == q.CoachOneOnOne || q.CoachOneOnOne != 1) && (u.CoachOnlineAvailable == q.CoachOnlineAvailable || q.CoachOnlineAvailable != 1) && (u.CoachOnlineOnly == q.CoachOnlineOnly || q.CoachOnlineOnly != 1) && (u.CoachOther == q.CoachOther || q.CoachOther != 1) && (u.CoachPhysio == q.CoachPhysio || q.CoachPhysio != 1) && (u.CoachPowerlifting == q.CoachPowerlifting || q.CoachPowerlifting != 1) && (u.CoachProgramOnly == q.CoachProgramOnly || q.CoachProgramOnly != 1) && (u.CoachStrongman == q.CoachStrongman || q.CoachStrongman != 1) && (u.CoachWeightLoss == q.CoachWeightLoss || q.CoachWeightLoss != 1) ) select u); int total = coaches.Count(); CityGeo city = await db.CityGeo.FindAsync(q.CityID); if (city != null) {//sort by distance first if city selected GeoAPI.Geometries.IGeometryFactory geometryFactory = NtsGeometryServices.Instance.CreateGeometryFactory(srid: 4326); GeoAPI.Geometries.IPoint cityLocation = geometryFactory.CreatePoint(new GeoAPI.Geometries.Coordinate((double)city.Latitude, (double)city.Longitude)); coaches = coaches.Where(x => x.Latitude.HasValue) .OrderBy(x => new GeoAPI.Geometries.Coordinate(x.Latitude.Value, x.Longitutde.Value).Distance(cityLocation.Coordinate)) .ThenByDescending(x => x.CreationDate); } else if (!string.IsNullOrEmpty(k)) {//else sort by keyword relevance only coaches = coaches .OrderByDescending(x => !string.IsNullOrEmpty(x.FirstName) && x.FirstName.ToLower().StartsWith(k)) .ThenByDescending(x => x.CreationDate); } else {//else sort by rating coaches = coaches.OrderByDescending(x => x.AverageRating); } coaches = coaches.Skip(pageNum * takeAmount).Take(takeAmount); if (coaches.Count() == 0) { return(new HttpResult(true, new { coaches = new List <UserPublic>(), total }, "")); } else { return(new HttpResult(true, new { coaches = coaches.Where(x => x != null).AsEnumerable().Select(s => new UserPublic(s, false)).ToList(), total }, "")); } } catch (Exception e) { var rawData = db.User.ToList(); return(new HttpResult(false, null, Functions.ErrorMessage(e))); } }