public async Task <ProductListRes> GetProductDetailsBySearchCriteria(ProductListReq request) { FilterDefinition <mProducts> filter; filter = Builders <mProducts> .Filter.Empty; FilterDefinition <mProductHotelAdditionalInfo> filterHAI; if (!string.IsNullOrWhiteSpace(request.ProdType)) { //filter = filter & Builders<mProducts>.Filter.Where(f => request.ProdType == f.ProductType); filter = filter & Builders <mProducts> .Filter.Regex(x => x.ProductType, new BsonRegularExpression(new Regex(request.ProdType, RegexOptions.IgnoreCase))); } if (!string.IsNullOrWhiteSpace(request.ProdName) || request.ProdName == "###") { request.ProdName = request.ProdName.Replace("###", ""); filter = filter & Builders <mProducts> .Filter.Regex(x => x.ProdName, new BsonRegularExpression(new Regex(request.ProdName.Trim(), RegexOptions.IgnoreCase))); } if (!string.IsNullOrWhiteSpace(request.ProdCode)) { filter = filter & Builders <mProducts> .Filter.Regex(x => x.ProductCode, new BsonRegularExpression(new Regex(request.ProdCode.Trim(), RegexOptions.IgnoreCase))); } if (request.IsPlaceHolder != null) { filter = filter & Builders <mProducts> .Filter.Where(x => x.Placeholder == request.IsPlaceHolder); } if (!string.IsNullOrWhiteSpace(request.City_Id)) { string lCity_Id = request.City_Id.ToLower(); filter = filter & Builders <mProducts> .Filter.Regex(x => x.Resort_Id, new BsonRegularExpression(new Regex(lCity_Id.Trim(), RegexOptions.IgnoreCase))); } if (!string.IsNullOrWhiteSpace(request.Country_Id)) { string lCountry_Id = request.Country_Id.ToLower(); filter = filter & Builders <mProducts> .Filter.Regex(x => x.ParentResort_Id, new BsonRegularExpression(new Regex(lCountry_Id.Trim(), RegexOptions.IgnoreCase))); } filter = filter & Builders <mProducts> .Filter.Regex(x => x.Status, new BsonRegularExpression(new Regex("", RegexOptions.IgnorePatternWhitespace))); var result = await _MongoContext.mProducts.Find(filter).Project(p => new ProductList { LocationInfo = new ProductLocation { CountryName = p.CountryName, CountryCode = p.ParentResort_Id, CityName = p.CityName, CityCode = p.Resort_Id, Lat = p.Lat, Long = p.Long, Address = p.FullAddress, PostCode = p.PostCode }, Code = p.ProductCode, Name = p.ProdName, VoyagerProductId = p.VoyagerProduct_Id, Type = p.ProductType, }).ToListAsync(); result = result.OrderBy(p => p.Name).ToList(); var res = new ProductListRes(); if (result.Count() > 0) { foreach (ProductList p in result) { if (p.Type.Trim().ToLower() == "hotel") { filterHAI = Builders <mProductHotelAdditionalInfo> .Filter.Empty; var Product_Id = p.VoyagerProductId; filterHAI = filterHAI & Builders <mProductHotelAdditionalInfo> .Filter.Regex(x => x.ProductId, new BsonRegularExpression(new Regex(Product_Id, RegexOptions.IgnorePatternWhitespace))); var resultHAI = await _MongoContext.mProductHotelAdditionalInfo.Find(filterHAI).Project(x => new { BudgetCategory = x.BudgetCategory, StarRating = x.StarRating, Location = x.Location, HotelChain = x.HotelChain }).FirstOrDefaultAsync(); if (resultHAI != null) { p.Location = resultHAI.Location; p.StarRating = resultHAI.StarRating; p.Category = resultHAI.BudgetCategory; p.Chain = resultHAI.HotelChain; } } } res.Products = result; } return(res); }
public async Task <IActionResult> GetProductList([FromBody] ProductListReq request) { var response = new ProductListRes(); try { if (!ModelState.IsValid) { response.ResponseStatus.Status = "Failure"; response.ResponseStatus.ErrorMessage = "Request is not valid"; return(BadRequest(response.ResponseStatus)); } else { if (request != null) { if (request.Country_Id == null) { response.ResponseStatus.Status = "Failure"; response.ResponseStatus.ErrorMessage = "Country Id can not be blank."; return(BadRequest(response.ResponseStatus)); } else if (request.City_Id == null) { Guid Country_Id = Guid.Empty; if ((!Guid.TryParse(request.Country_Id, out Country_Id))) { response.ResponseStatus.Status = "Failure"; response.ResponseStatus.ErrorMessage = "Country Id is not valid."; return(BadRequest(response.ResponseStatus)); } response.ResponseStatus.Status = "Failure"; response.ResponseStatus.ErrorMessage = "City Id can not be blank."; return(BadRequest(response.ResponseStatus)); } else { Guid City_Id = Guid.Empty; if (!Guid.TryParse(request.City_Id, out City_Id)) { response.ResponseStatus.Status = "Failure"; response.ResponseStatus.ErrorMessage = "City Id is not valid."; return(BadRequest(response.ResponseStatus)); } else { var result = await _productRepository.GetProductDetailsBySearchCriteria(request); if (result != null && result.Products.Count > 0) { response.ResponseStatus.Status = "Success"; response.Products = result.Products; } else { response.ResponseStatus.Status = "Success"; response.ResponseStatus.ErrorMessage = "No Records Found."; return(NotFound(response.ResponseStatus)); } } } } else { response.ResponseStatus.Status = "Failure"; response.ResponseStatus.ErrorMessage = "Product details can not be blank."; return(BadRequest(response.ResponseStatus)); } } } catch (Exception ex) { response.ResponseStatus.Status = "Failure"; response.ResponseStatus.ErrorMessage = "An error occurs " + ex.Message.ToString(); return(BadRequest(response.ResponseStatus)); } return(Ok(response)); }