//Add businesses //Input: NewBusinessDTO, NewPlaceDTO, CategoriesIdsDTO //Output: boolean if succeed or not public static bool AddBusiness(NewBusinessDTO business, NewPlaceDTO place, CategoriesIdsDTO categories) { SwapDbConnection db = new SwapDbConnection(); r_place_sub_and_main_category categoriedPlace; place placeInDb = db.places.FirstOrDefault(p => p.place_id == place.place_id); business newBusiness; List <r_place_sub_and_main_category> listOfPlacesCategories = new List <r_place_sub_and_main_category>(); newBusiness = new business { business_owner_id = business.business_owner_id, approve_by_admin = false, closing_hours = business.closing_hours, place_id = place.place_id, is_active = business.is_active, rating = 0, opening_hours = business.opening_hours }; if (placeInDb == null) { foreach (string subId in categories.subIds) { listOfPlacesCategories.Add(new r_place_sub_and_main_category { creation_date = DateTime.Now, place_id = place.place_id, main_id = categories.mainId, sub_id = subId }); } db.places.Add(new place { latitude = place.latitude, longitude = place.longitude, description = place.description, name = place.name, country = place.country, creation_date = DateTime.Now, place_id = place.place_id, post_code = place.post_code ?? "", settlement = place.settlement, state = place.state ?? "", street = place.street, street_number = place.street_number ?? "", business = newBusiness, r_place_sub_and_main_category = listOfPlacesCategories, }); } else { if (placeInDb.business != null || placeInDb.Event != null) { return(false); } foreach (string subId in categories.subIds) { categoriedPlace = placeInDb.r_place_sub_and_main_category.FirstOrDefault(r => r.main_id == categories.mainId && r.sub_id == subId); if (categoriedPlace == null) { placeInDb.r_place_sub_and_main_category.Add(new r_place_sub_and_main_category { creation_date = DateTime.Now, place_id = place.place_id, main_id = categories.mainId, sub_id = subId }); } } placeInDb.description = place.description; placeInDb.name = place.name; placeInDb.business = newBusiness; } db.SaveChanges(); return(true); }
//Get businesses nearby and filtered by categories //Input: PointDTO, CategoriesIdsDTO, radius //Output: List of MapBusinessDTO public static List <MapBusinessDTO> GetFilteredBusinessesAround(PointDTO position, CategoriesIdsDTO ids, double radius) { SwapDbConnection db = new SwapDbConnection(); PointDTO point; List <business> businesses; List <MapBusinessDTO> filteredBusinesses = new List <MapBusinessDTO>(); main_category mainCategory = db.main_category.FirstOrDefault(category => category.main_id == ids.mainId); string iconCategory; if (mainCategory == null) { return(filteredBusinesses); } point = new PointDTO(); businesses = db.businesses.Include(b => b.products).Where(b => b.is_active && b.approve_by_admin && b.place.r_place_sub_and_main_category.Any(r => r.main_id == ids.mainId && ids.subIds.Any(id => r.sub_id == id))).ToList(); iconCategory = mainCategory.google_value; foreach (business b in businesses) { point.lat = (double)b.place.latitude; point.lng = (double)b.place.longitude; if (PlaceService.GetDistance(point, position) <= radius) { filteredBusinesses.Add(new MapBusinessDTO { closing_hours = b.closing_hours, description = b.place.description, lat = b.place.latitude, lng = b.place.longitude, name = b.place.name ?? "", opening_hours = b.opening_hours, place_id = b.place_id, rating = b.rating, settlement = b.place.settlement ?? "", street = b.place.street ?? "", street_number = b.place.street_number ?? "", icon = iconCategory ?? "", products = b.products.Where(p => p.is_active && p.discount_end_date >= DateTime.Now).Select(product => new productDTO { business_id = product.business_id, creation_date = product.creation_date, description = product.description, discount = product.discount, discount_end_date = product.discount_end_date, discount_start_date = product.discount_start_date, is_active = product.is_active, name = product.name, price = product.price, product_id = product.product_id }).ToList() }); } } return(filteredBusinesses); }
public HttpResponseMessage GetFilteredBusinesses([FromUri] PointDTO position, [FromUri] CategoriesIdsDTO ids, double radius) { try { if (string.IsNullOrEmpty(ids.mainId) || ids.subIds == null) { return(Request.CreateResponse(HttpStatusCode.BadRequest, "Illegal parameters")); } List <MapBusinessDTO> list = BusinessService.GetFilteredBusinessesAround(position, ids, radius); return(Request.CreateResponse(HttpStatusCode.OK, list)); } catch (Exception e) { return(Request.CreateResponse(HttpStatusCode.InternalServerError, "There was an InternalServerError: " + e)); } }