Example #1
0
 public async Task <IActionResult> GetIssuesForUser([FromBody] SearchIssueFormModel searchIssueForm)
 {
     return(Ok(GetRequestResult(await _issueService.GetIssuesForUserAsync(searchIssueForm))));
 }
        public async Task <SearchIssueViewModel> GetIssuesForUserAsync(SearchIssueFormModel searchIssueForm)
        {
            try
            {
                var queryable = _context.Issues.Include(c => c.EquipmentIssueSubscriptions).Include(c => c.User).AsQueryable();

                if (!string.IsNullOrEmpty(searchIssueForm.Title))
                {
                    queryable = queryable.Where(q => q.Title.Contains(searchIssueForm.Title));
                }

                if (searchIssueForm.IssueType.HasValue)
                {
                    queryable = queryable.Where(q => q.IssueType == searchIssueForm.IssueType);
                }

                if (searchIssueForm.IsActive.HasValue)
                {
                    queryable = queryable.Where(q => q.IsActive == searchIssueForm.IsActive);
                }
                if (searchIssueForm.UserId.HasValue)
                {
                    queryable = queryable.Where(q => q.UserId == searchIssueForm.UserId);
                }
                if (searchIssueForm.EquipmentIds.Count > 0)
                {
                    //var eqIssueSubs = _context.EquipmentIssueSubscriptions.Where(c => searchIssueForm.EquipmentIds.Contains(c.EquipmentId)).Select(c=>c.IssueId).ToList();
                    queryable = queryable.Where(c => c.EquipmentIssueSubscriptions.Any(x => searchIssueForm.EquipmentIds.Contains(x.EquipmentId)));
                }
                if (searchIssueForm.SortModel != null)
                {
                    switch (searchIssueForm.SortModel.Col + "|" + searchIssueForm.SortModel.Sort)
                    {
                    case "creationdate|asc":
                        queryable = queryable.OrderBy(x => x.CreationDate);
                        break;

                    case "creationdate|desc":
                        queryable = queryable.OrderByDescending(x => x.CreationDate);
                        break;

                    default:
                        queryable = queryable.OrderBy(x => x.CreationDate);
                        break;
                    }
                }
                else
                {
                    queryable = queryable.OrderBy(x => x.CreationDate);
                }
                var response = await queryable.Skip((searchIssueForm.PageIndex - 1) *searchIssueForm.PageSize).Take(searchIssueForm.PageSize).ToListAsync();

                var count = await queryable.CountAsync();

                var searchViewModel = new SearchIssueViewModel();

                searchViewModel.searchIssueViewModel = response.Select(resp => new IssueViewModel()
                {
                    Id           = resp.Id,
                    AnswerCount  = resp.AnswerCounts,
                    CreationDate = resp.CreationDate,
                    Description  = resp.Description,
                    Equipments   = resp.EquipmentIssueSubscriptions.Select(x => new EquipmentViewModel()
                    {
                        Description   = x.Equipment.Description,
                        EquipmentName = x.Equipment.EquipmentName,
                        Id            = x.EquipmentId
                    }).ToList(),
                    IsActive       = resp.IsActive,
                    SumLikes       = resp.IssueDetails.Sum(c => c.Likes),
                    IssueType      = resp.IssueType,
                    LastUpdateDate = resp.LastUpdateDate,
                    Title          = resp.Title,
                    UserId         = resp.UserId,
                    UserName       = resp.User.Username,
                    FileId         = resp.FileId
                }).ToList();
                searchViewModel.PageIndex  = searchIssueForm.PageIndex;
                searchViewModel.PageSize   = searchIssueForm.PageSize;
                searchViewModel.TotalCount = count;
                return(searchViewModel);
            }
            catch (Exception ex)
            {
                throw new ExperienceManagementGlobalException(IssueServiceErrors.GetIssuesError, ex);
            }
        }