private Period GetPeriodForViewModel(ProjectViewModel.PeriodViewModel periodViewModel) { var period = _dbContext.Set <Period>() .FirstOrDefault(p => p.Start.Year == periodViewModel.Date.Year && p.Start.Month == periodViewModel.Date.Month); if (period == default) { var periodEntity = _dbContext.Set <Period>() .Add(new Period( new DateTime(periodViewModel.Date.Year, periodViewModel.Date.Month, 1), new DateTime(periodViewModel.Date.Year, periodViewModel.Date.Month, 28) )); _dbContext.SaveChanges(); period = periodEntity.Entity; } return(period); }
public ProjectViewModel GetProjectViewModelById(int id) { var a = _dbContext.Set <FunctioningCapacityResource>() .Where(resource => resource.ProjectId == id) .Include(resource => resource.Project) .ThenInclude(project => project.ProjectStatus) .Include(resource => resource.Resource) .ThenInclude(resource => resource.Employee) .Include(resource => resource.Period) .OrderBy(resource => resource.Period.Start) .ToList() .GroupBy(resource => resource.Period) .ToDictionary(grouping => grouping.Key, grouping => grouping.ToList()); var project = _dbContext.Set <Project>().Find(id); var periodsCapacity = _dbContext.Set <FunctioningCapacityProject>() .Where(capacityProject => capacityProject.ProjectId == id) .Include(capacityProject => capacityProject.Period) .ToList(); var periods2 = periodsCapacity.Select(periodCap => { var fresArr = new ProjectViewModel.ResourceCapacityViewModel[0]; if (a.ContainsKey(periodCap.Period)) { fresArr = a[periodCap.Period] .Select(functioningCapacityResource => new ProjectViewModel.ResourceCapacityViewModel(functioningCapacityResource.Resource.Id, $"{functioningCapacityResource.Resource.Employee.FirstName} " + $"{functioningCapacityResource.Resource.Employee.LastName}", functioningCapacityResource.FunctionCapacity)) .ToArray(); } var capacity = periodCap.FunctioningCapacity; var period = new ProjectViewModel.PeriodViewModel(capacity, periodCap.Period.Start, fresArr); return(period); }).ToArray(); var projectViewModel = new ProjectViewModel(id, project.Name, project.ProjectStatusId, periods2); return(projectViewModel); }