public PlantResult GetPlantInfo(string imageData, string userToken) { var keywords = _visionService.GetQueryKeywords(imageData); using (var context = new AppDbContext()) { var searchParam = new SqlParameter("@SearchTerm", keywords); var plantId = context.Database.SqlQuery <int>("FindPlant @SearchTerm", searchParam) .ToList().FirstOrDefault(); var plant = (from x in context.Plants where x.PlantId == plantId select x).FirstOrDefault(); if (plant == null) { return(null); } var plantHistory = (from x in context.UsersPlantsInfos .Include(e => e.User) .Include(e => e.Plant) where x.User.LoginToken == userToken && x.Plant.PlantId == plant.PlantId select x).FirstOrDefault(); var isFlowerFavorite = false; if (plantHistory == null) { var user = (from x in context.Users where x.LoginToken == userToken select x).FirstOrDefault(); context.UsersPlantsInfos.Add(new UsersPlantsInfo() { User = user, Plant = plant, IsFavourite = false }); context.SaveChanges(); } else { isFlowerFavorite = plantHistory.IsFavourite; } return(new PlantResult() { PlantId = plant.PlantId, Name = plant.Name, ImageUrl = Appconstants.DomainUrl + plant.Imageurl, Info = plant.Description, IsFavorite = isFlowerFavorite }); } }
public Plant GetPlantInfo(string imageData) { var keywords = _visionService.GetQueryKeywords(imageData); var filter = Builders <Plant> .Filter.Text(keywords); var projection = Builders <Plant> .Projection.MetaTextScore("score"); var sort = Builders <Plant> .Sort.MetaTextScore("score"); var plants = Db.GetCollection <Plant>("plants") .Find(filter) .Project <Plant>(projection) .Sort(sort) .ToList(); return(plants.FirstOrDefault(p => p.RelevanceScore >= 4.0f)); }