public async Task <IActionResult> Index(Order order) { //若未登入則跳轉登入頁 if (HttpContext.Session.GetInt32(cUtility.Current_User_Id) == null) { return(RedirectToAction("Login", "Member")); } //填入 order 裡未從前端取得的值 order.HostMemberId = HttpContext.Session.GetInt32(cUtility.Current_User_Id).Value; order.StartTime = DateTime.Now; DistrictRef dist = db.DistrictRefs.Where(d => d.DistrictId == order.DistrictId) .Include(d => d.City).First(); string address = dist.City.CityName + dist.DistrictName + order.Address; //todo read this //https://stackoverflow.com/questions/30419739/return-to-view-with-async-await var latlong = cUtility.addressToLatlong(address); order.Latitude = latlong[0]; order.Longitude = latlong[1]; order.AvailableCount = order.MaxCount; order.IsActive = true; if (order.OrderDescription == null) { order.OrderDescription = ""; } //將 order 寫入資料庫 db.Add(order); await db.SaveChangesAsync(); return(RedirectToAction("Index", "ChatMessageRecords", new { grouptype = 1, groupid = order.OrderId })); }
public string Search( int DistrictInput, string addressInput) { //設定最大搜尋範圍(寫死為3000) //var distantMax = db.RangeRefs.Last().RangeInMeters; int distanceMax = 3000; //從參數求出完整地址,並轉成經緯度 DistrictRef dist = db.DistrictRefs.Where(d => d.DistrictId == DistrictInput) .Include(d => d.City).First(); string address = dist.City.CityName + dist.DistrictName + addressInput; var latlong = cUtility.addressToLatlong(address); GeoCoordinate userLocation = new GeoCoordinate((double)latlong[0], (double)latlong[1]); //disable query and use the first user's location for testing //var tempUser = db.Members.First(); //GeoCoordinate userLocation = new GeoCoordinate((double)tempUser.Latitude, (double)tempUser.Longitude); //若使用者有登入,則先過濾掉使用者自己新增的服務 IQueryable <GarbageServiceOffer> firstPass = db.GarbageServiceOffers; if (HttpContext.Session.GetInt32(cUtility.Current_User_Id) != null) { firstPass = db.GarbageServiceOffers.Where(o => o.HostMemberId != HttpContext.Session.GetInt32(cUtility.Current_User_Id).Value); } //過濾到期、已額滿或在搜尋範圍外的服務,並轉成json var bagTypeSearch = from o in firstPass where o.IsActive == true && !(o.L3available == 0 && o.L5available == 0 && o.L14available == 0 && o.L25available == 0 && o.L33available == 0 && o.L75available == 0 && o.L120available == 0) select o; var newObject = from o in bagTypeSearch.Include(o => o.District).Include(o => o.District.City).Include(o => o.GoRange) select new { o.GarbageServiceId, o.DistrictId, o.District.DistrictName, o.District.City.CityName, o.Address, o.EndTime, o.CanGo, o.GoRange.RangeInMeters, o.L3available, o.L5available, o.L14available, o.L25available, o.L33available, o.L75available, o.L120available, o.Latitude, o.Longitude, //目前先將查詢位置的經緯度存入每筆資料中 //put user location in every data, not worth putting it elsewhere when the data count is small. userLat = latlong[0], userLong = latlong[1], Distance = userLocation.GetDistanceTo(new GeoCoordinate((double)o.Latitude, (double)o.Longitude)), }; var offerList = newObject.AsEnumerable().Where(o => o.Distance <= distanceMax).ToList(); return(JsonConvert.SerializeObject(offerList)); }
public string Search(int DistrictInput, string addressInput) { //todo search by endtime & price ? //設定搜尋範圍 //var distantMax = db.RangeRefs.Last().RangeInMeters; int distanceMax = 3000; //從參數求出完整地址,並轉成經緯度 DistrictRef dist = db.DistrictRefs.Where(d => d.DistrictId == DistrictInput) .Include(d => d.City).First(); string address = dist.City.CityName + dist.DistrictName + addressInput; var latlong = cUtility.addressToLatlong(address); GeoCoordinate userLocation = new GeoCoordinate((double)latlong[0], (double)latlong[1]); //disable query and use the first user's location for testing //var tempUser = db.Members.First(); //GeoCoordinate userLocation = new GeoCoordinate((double)tempUser.Latitude, (double)tempUser.Longitude); //若使用者有登入,則先過濾掉使用者自己新增的團購 IQueryable <Order> firstPass = db.Orders; if (HttpContext.Session.GetInt32(cUtility.Current_User_Id) != null) { firstPass = db.Orders.Where(o => o.HostMemberId != HttpContext.Session.GetInt32(cUtility.Current_User_Id).Value); } //過濾掉已結束和搜尋範圍外的結果並回傳json var newObject = from o in firstPass.Include(o => o.District).Include(o => o.District.City).Include(o => o.GoRange) where o.IsActive == true select new { o.OrderId, o.ProductId, o.DistrictId, o.District.DistrictName, o.District.City.CityName, o.Address, o.EndTime, o.CanGo, o.GoRange.RangeInMeters, o.AvailableCount, o.Latitude, o.Longitude, o.HostMemberId, //目前先將查詢位置的經緯度存入每筆資料中 //put user location in every data, not worth putting it elsewhere when the data count is small. userLat = latlong[0], userLong = latlong[1], Distance = userLocation.GetDistanceTo(new GeoCoordinate((double)o.Latitude, (double)o.Longitude)), }; var offerList = newObject.AsEnumerable().Where(o => o.Distance <= distanceMax).ToList(); return(JsonConvert.SerializeObject(offerList)); }
public async Task <IActionResult> Index(GarbageServiceOffersViewModel g) { if (HttpContext.Session.GetInt32(cUtility.Current_User_Id) == null) { return(RedirectToAction("Login", "Member")); } g.HostMemberId = HttpContext.Session.GetInt32(cUtility.Current_User_Id).Value; if (g.Address != null) { g.StartTime = DateTime.Now; g.ServiceTypeId = 1; //get latlong from address DistrictRef dist = _context.DistrictRefs.Where(d => d.DistrictId == g.DistrictId) .Include(d => d.City).First(); string address = dist.City.CityName + dist.DistrictName + g.Address; var latlong = cUtility.addressToLatlong(address); g.Latitude = latlong[0]; g.Longitude = latlong[1]; g.IsActive = true; g.gso.L3available = g.L3maxCount; g.gso.L5available = g.L5maxCount; g.gso.L14available = g.L14maxCount; g.gso.L25available = g.L25maxCount; g.gso.L33available = g.L33maxCount; g.gso.L75available = g.L75maxCount; g.gso.L120available = g.L120maxCount; _context.Add(g.gso); await _context.SaveChangesAsync(); return(RedirectToAction("Index", "ChatMessageRecords", new { grouptype = 2, groupid = g.gso.GarbageServiceId })); } ViewData["DistrictId"] = new SelectList(_context.DistrictRefs, "DistrictId", "DistrictName", g.DistrictId); ViewData["GoRangeId"] = new SelectList(_context.RangeRefs, "RangeId", "RangeInMeaters", g.GoRangeId); ViewData["CityId"] = new SelectList(_context.CityRefs, "CityId", "CityName", g.District); ViewData["ServiceTypeId"] = new SelectList(_context.ServiceTypeRefs, "ServiceTypeId", "ServiceName", g.ServiceTypeId); return(View(g)); }