Exemplo n.º 1
0
        /// <summary>
        /// Searches Projects
        /// </summary>
        /// <remarks>Used for the project search page.</remarks>
        /// <param name="districts">Districts (comma seperated list of id numbers)</param>
        /// <param name="project">name or partial name for a Project</param>
        /// <param name="hasRequests">if true then only include Projects with active Requests</param>
        /// <param name="hasHires">if true then only include Projects with active Rental Agreements</param>
        /// <param name="status">if included, filter the results to those with a status matching this string</param>
        /// <response code="200">OK</response>
        public virtual IActionResult ProjectsSearchGetAsync(string districts, string project, bool?hasRequests, bool?hasHires, string status)
        {
            int?[] districtTokens = ParseIntArray(districts);

            var data = _context.Projects
                       .Include(x => x.District.Region)
                       .Include(x => x.PrimaryContact)
                       .Select(x => x);

            if (districtTokens != null && districts.Length > 0)
            {
                data = data.Where(x => districtTokens.Contains(x.District.Id));
            }

            if (hasRequests != null)
            {
                // throw new NotImplementedException();
            }

            if (hasHires != null)
            {
                // throw new NotImplementedException();
            }

            if (project != null)
            {
                // Allow for case insensitive search of project name
                data = data.Where(x => x.Name.ToLowerInvariant().Contains(project.ToLowerInvariant()));
            }

            var result = new List <ProjectSearchResultViewModel>();

            foreach (var item in data)
            {
                ProjectSearchResultViewModel newItem = item.ToViewModel();
                result.Add(item.ToViewModel());
            }

            // second pass to do calculated fields.
            foreach (ProjectSearchResultViewModel projectSearchResultViewModel in result)
            {
                // calculated fields.
                projectSearchResultViewModel.Requests = _context.RentalRequests
                                                        .Include(x => x.Project)
                                                        .Where(x => x.Project.Id == projectSearchResultViewModel.Id)
                                                        .Count();

                // TODO filter on status once RentalAgreements has a status field.
                projectSearchResultViewModel.Hires = _context.RentalAgreements
                                                     .Include(x => x.Project)
                                                     .Where(x => x.Project.Id == projectSearchResultViewModel.Id)
                                                     .Count();
            }

            return(new ObjectResult(result));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Project search result view model
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static ProjectSearchResultViewModel ToViewModel(this Project model)
        {
            var dto = new ProjectSearchResultViewModel();

            if (model != null)
            {
                dto.Id             = model.Id;
                dto.Name           = model.Name;
                dto.PrimaryContact = model.PrimaryContact;
                dto.District       = model.District;
            }
            return(dto);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Project search result view model
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static ProjectSearchResultViewModel ToViewModel(this Project model)
        {
            var dto = new ProjectSearchResultViewModel();

            if (model != null)
            {
                dto.Id               = model.Id;
                dto.Status           = model.Status;
                dto.Name             = model.Name;
                dto.PrimaryContact   = model.PrimaryContact;
                dto.District         = model.District;
                dto.RentalRequests   = model.RentalRequests;
                dto.RentalAgreements = model.RentalAgreements;

                // calculate request and hire count
                dto.CountRequests();
                dto.CountHires();

                dto.RentalRequests   = null;
                dto.RentalAgreements = null;
            }
            return(dto);
        }
 /// <summary>
 /// Setup the test.
 /// </summary>
 public ProjectSearchResultViewModelModelTests()
 {
     instance = new ProjectSearchResultViewModel();
 }