using Controller.Repository; using Controller.ViewModels; using DataModels.Enums; using DataModels.Models; namespace Controller.BusinessLogic { public class StudentLogic { private readonly StudentRepository _studentRepository; private readonly GroupRepository _groupRepository; private readonly SpecializationRepository _specializationRepository; public StudentLogic(StudentRepository studentRepository, GroupRepository groupRepository, SpecializationRepository specializationRepository) { _studentRepository = studentRepository; _groupRepository = groupRepository; _specializationRepository = specializationRepository; } public async Task> GetViewModel() { //var students = await _studentRepository.GetAll(); var students = new List { new Student { Id = 1, Name = "Иванов", GroupId = 1, SpecializationId = null, Status = Status.Study }, new Student { Id = 2, Name = "Петров", GroupId = null, SpecializationId = null, Status = Status.Academ }, new Student { Id = 3, Name = "Сидорова", GroupId = 2, SpecializationId = null } }; var result = new List(); var groups = new List { new Group { Id = 1, Name = "Группа 101", Course = 1, SpecializationId = 1, MaxStudentCount = 25, Number = 101 }, new Group { Id = 2, Name = "Группа 202", Course = 2, SpecializationId = 2, MaxStudentCount = 30, Number = 202 }, new Group { Id = 3, Name = "Группа 303", Course = 3, SpecializationId = 3, MaxStudentCount = 20, Number = 303 } }; // Моковые данные специализаций var specializations = new List { new Specialization { Id = 1, Name = "Информатика", Code = "INF" }, new Specialization { Id = 2, Name = "Математика", Code = "MATH" }, new Specialization { Id = 3, Name = "Физика", Code = "PHYS" } }; foreach (var student in students) { //var group = student.GroupId.HasValue // ? await _groupRepository.Get(student.GroupId.Value) : null; //var specialization = student.SpecializationId.HasValue // ? await _specializationRepository.Get(student.SpecializationId.Value) : null; var group = student.GroupId.HasValue ? groups.FirstOrDefault(g => g.Id == student.GroupId.Value) : null; var specialization = student.SpecializationId.HasValue ? specializations.FirstOrDefault(s => s.Id == student.SpecializationId.Value) : group?.SpecializationId != null ? specializations.FirstOrDefault(s => s.Id == group.SpecializationId) : null; result.Add(new StudentViewModel(student, group, specialization)); } return result; } public Dictionary GetStatusNames() { return new Dictionary { { Status.Study, "Обучается" }, { Status.Expulsion, "Отчислен" }, { Status.Graduated, "Завершил обучение, выпущен" }, { Status.Academ, "Академический отпуск" } }; } } }