private async Task <List <WorkResponse> > GetWorksInternalAsync(int personId, int userId) { // get work types var worktypes = await _workTypesRepository.GetWorkTypesDictionaryAsync(); // get all work links and workIds var workLinks = await _worksRepository.BuildWorksTreeAsync(personId); var workIds = workLinks.Select(c => c.WorkId).Distinct().ToList(); // marks var marks = await _marksRepository.GetWorkMarkAsync(workIds.ToArray()); var workMarks = marks.ToDictionary(c => c.WorkId, c => c); if (marks.Count > 0 && userId > 0) { var userMarks = await _marksRepository.GetUserMarkAsync(userId, workIds.ToArray()); foreach (var userMark in userMarks) { MarkResponse workMarkTemp; workMarks.TryGetValue(userMark.WorkId, out workMarkTemp); if (workMarkTemp != null) { workMarks[userMark.WorkId].UserMark = userMark.UserMark; } } } // get all works var worksRaw = await _worksRepository.GetWorksByIdsAsync(workIds); // get all work's people var peopleDic = await _personsRepository.GetPersonsByWorksIdsAsync(workIds); List <WorkResponse> works = new List <WorkResponse>(); foreach (var work in worksRaw) { var workType = worktypes[work.WorkTypeId]; // manual restriction to show the work in biblio if (work.ShowInBiblio == 2) { continue; } // restrict to show magazines if (work.WorkTypeId == 26) { continue; } // restrict children works (non-active) var workLink = workLinks.SingleOrDefault(c => c.WorkId == work.Id && c.LinkType == 2); if (workLink?.ParentWorkId != null) { continue; } // multiauthors cycles var workPeople = peopleDic.GetValueOrDefault(work.Id); if (workType.IsNode && !workLinks.Any(c => c.WorkId == work.Id && c.LinkType == 2 && c.ParentWorkId != null)) { if (workPeople == null || workPeople.Count(c => c.PersonId != personId) == workPeople.Count) { workType = worktypes[50]; } } // remove all non-author's works if (workType.WorkTypeId != 50 && workPeople?.Count(c => c.PersonId != personId) == workPeople?.Count) { continue; } var workResponse = CreateWorkResponse(work, workType, peopleDic, workLink, personId, workMarks); if (work.ShowSubworksInBiblio == 1 || (work.ShowSubworksInBiblio != 2 && workType.IsNode) || work.WorkTypeId == 50) { GetSubworks(workLinks, work, workResponse, worksRaw, worktypes, peopleDic, personId, workMarks); } works.Add(workResponse); } return(works); }