Exemplo n.º 1
0
        public IHttpActionResult Algorithm([FromUri] int[] list, int?areaId)
        {
            if (list == null)
            {
                return(BadRequest());
            }

            List <Tag> tags = _tagService.GetAll()
                              .Where(tag => tag.Answer
                                     .Any(answer => list.Contains(answer.Id))
                                     )
                              .ToList();

            Tree tree = _searchTreeService.BuildTree(_locationService.GetAll().Include(location => location.Tags)
                                                     .ToList());

            var resultIds       = _searchTreeService.SearchTree(tags, tree);
            var locationsResult = _locationService.GetAll()
                                  .Include(location => location.Reviews)
                                  .Include(location => location.Tags)
                                  .Include(location => location.Photos.Select(__ => __.Photo))
                                  .Where(location => !areaId.HasValue || location.AreaId == areaId)
                                  .Where(location => resultIds.Contains(location.Id));

            List <TreeViewModels> resultList = new List <TreeViewModels>();

            foreach (Location location in locationsResult)
            {
                var locationTags = location.Tags;
                var commonTags   = locationTags.Intersect(tags);
                int ratingCount  = location.Reviews.Count;
                var rating       = location.Reviews.Sum(_ => _.Rating) / ratingCount;
                rating = float.IsNaN(rating) ? 0 : rating;

                TreeViewModels result = new TreeViewModels
                {
                    Id            = location.Id,
                    Address       = location.Address,
                    Location      = location.Name,
                    Percent       = (tags.Count / commonTags.Count()).ToString(),
                    PrimaryPhoto  = location.Photos.FirstOrDefault(_ => _.IsPrimary)?.Photo.Path,
                    Rating        = rating,
                    Reasons       = commonTags.Select(tag => tag.Name).ToList(),
                    ReviewCount   = location.Reviews.Count,
                    Categories    = location.Category,
                    TotalTimeStay = location.TotalTimeStay
                };

                resultList.Add(result);
            }


            return(Ok(resultList.OrderByDescending(_ => _.Reasons.Count)));
        }
Exemplo n.º 2
0
        public async Task <IHttpActionResult> Algorithm(CreateSuggestedPlanViewModels viewModel)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            List <Tag> tags = _tagService.GetAll()
                              .Where(tag => tag.Answer
                                     .Any(answer => viewModel.Answers.Contains(answer.Id))
                                     )
                              .ToList();

            var  path = CurrentContext.Server.MapPath("/Tree/tree.json");
            Tree tree = _searchTreeService.ReadTree(path);

            if (tree == null)
            {
                tree = _searchTreeService.BuildTree(_locationService.GetAll().Include(location => location.Tags)
                                                    .ToList());
                _searchTreeService.WriteTree(path, tree);
            }

            var resultIds       = _searchTreeService.SearchTree(tags, tree);
            var locationsResult = _locationService.GetAll()
                                  .Include(location => location.Reviews)
                                  .Include(location => location.Tags)
                                  .Include(location => location.Photos.Select(__ => __.Photo))
                                  .Where(location => location.AreaId == viewModel.AreaId)
                                  .Where(location => resultIds.Contains(location.Id));

            List <TreeViewModels> resultList = new List <TreeViewModels>();

            foreach (Location location in locationsResult)
            {
                var locationTags = location.Tags;
                var commonTags   = locationTags.Intersect(tags);
                int ratingCount  = location.Reviews.Count;
                var rating       = location.Reviews.Sum(_ => _.Rating) / ratingCount;
                rating = float.IsNaN(rating) ? 0 : rating;

                foreach (Review review in location.Reviews)
                {
                    rating += review.Rating;
                }



                TreeViewModels result = new TreeViewModels
                {
                    Id            = location.Id,
                    Address       = location.Address,
                    Location      = location.Name,
                    Percent       = (tags.Count / commonTags.Count()).ToString(),
                    PrimaryPhoto  = location.Photos.FirstOrDefault(_ => _.IsPrimary)?.Photo.Path.ToString(),
                    Rating        = rating,
                    Reasons       = commonTags.Select(tag => tag.Name).ToList(),
                    ReviewCount   = location.Reviews.Count,
                    Categories    = location.Category,
                    TotalTimeStay = location.TotalTimeStay
                };


                if (location.Tags.All(tag => tag.Name != "cafe"))
                {
                    resultList.Add(result);
                }
            }

            var locationListResult = resultList.OrderByDescending(_ => _.Reasons.Count)
                                     .Select(model => new Core.ObjectModels.Entities.Helper.TreeViewModels
            {
                Address      = model.Address,
                Categories   = model.Categories,
                Id           = model.Id,
                Location     = model.Location,
                Percent      = model.Percent,
                PrimaryPhoto = model.PrimaryPhoto,
                Rating       = model.Rating,
                Reasons      = model.Reasons
            })
                                     .ToList();


            int userId = (await CurrentUser()).Id;

            _loggingService.AddSentryBreadCrum("CreateSuggestedPlan", data: new Dictionary <string, object>
            {
                ["input model"] = viewModel
            });

            Plan resultPlan = await _planService.CreateSuggestedPlan(
                new Plan
            {
                Name      = viewModel.Name,
                AreaId    = viewModel.AreaId,
                StartDate = viewModel.StartDate,
                EndDate   = viewModel.EndDate,
                CreatorId = userId,
                MemberId  = userId
            },
                locationListResult);

            return(Ok(resultPlan.Id));
        }