public static StudentModel ToStudentModel(Student studentEntity)
        {
            StudentModel studentModel = new StudentModel();
            studentModel.ID = studentEntity.ID;
            studentModel.FirstName = studentEntity.FirstName;
            studentModel.LastName = studentEntity.LastName;
            studentModel.Age = studentEntity.Age;
            studentModel.Grade = studentEntity.Grade;
            
            studentModel.School = new SchoolDetails()
            {
                ID = studentEntity.School.ID,
                Name = studentEntity.School.Name,
                Location = studentEntity.School.Location
            };

            foreach (Mark mark in studentEntity.Marks)
            {
                studentModel.Marks.Add(new MarkModel()
                    {
                        ID = mark.ID,
                        Subject = mark.Subject,
                        Value = mark.Value
                    });
            }

            return studentModel;
        }
        public static Student CreateOrLoadStudent(StudentModel studentModel, IUnitOfWork unitOfWork)
        {
            Student student = unitOfWork.StudentsRepository.Get(studentModel.ID);
            if (student != null)
            {
                return student;
            }

            Student newStudent = new Student()
                {
                    FirstName = studentModel.FirstName,
                    LastName = studentModel.LastName,
                    Age = studentModel.Age,
                    Grade = studentModel.Grade,
                    School = CreateOrLoadSchool(studentModel.School, unitOfWork)
                };

            foreach (MarkModel mark in studentModel.Marks)
            {
                newStudent.Marks.Add(MarksMapper.ToMarkEntity(mark));
            }

            unitOfWork.StudentsRepository.Add(newStudent);
            unitOfWork.Save();

            return newStudent;
        }
        public HttpResponseMessage Add(StudentModel studentModel)
        {
            var httpResponse = this.PerformOperation(() =>
            {
                var studentEntity = StudentsMapper.ToStudentEntity(studentModel, unitOfWork);

                this.unitOfWork.StudentsRepository.Add(studentEntity);
                this.unitOfWork.Save();

                studentModel.ID = studentEntity.ID;
                return studentModel;
            });

            return httpResponse;
        }
        public static Student ToStudentEntity(StudentModel studentModel, IUnitOfWork unitOfWork)
        {
            Student student = new Student();
            student.Age = studentModel.Age;
            student.FirstName = studentModel.FirstName;
            student.LastName = studentModel.LastName;
            student.Grade = studentModel.Grade;
            
            student.School = Extensions.CreateOrLoadSchool(studentModel.School, unitOfWork);
            
            foreach (MarkModel mark in studentModel.Marks)
            {
                student.Marks.Add(new Mark() { Subject = mark.Subject, Value = mark.Value });
            }

            return student;
        }
        public void AddValidStudent_ShouldBeAddedCorrectly()
        {
            var unitOfWork = Mock.Create<IUnitOfWork>();

            School school = new School() { Location = "Location", Name = "Name" };

            Student studentEntity = new Student()
            {
                FirstName = "Pesho",
                LastName = "Peshov",
                Age = 16,
                Grade = 10,
                School = school,
                Marks = new List<Mark>() { new Mark() { Subject = "math", Value = 6 } }
            };

            bool isItemAdded = false;
            Mock.Arrange(() => unitOfWork.SchoolsRepository.Get(Arg.IsAny<int>()))
                .Returns(school);

            Mock.Arrange(() => unitOfWork.StudentsRepository.Add(Arg.IsAny<Student>()))
                .DoInstead(() => isItemAdded = true)
                .Returns(studentEntity);

            var studentsController = new StudentsController(unitOfWork);
            SetupController(studentsController);

            StudentModel studentModel = new StudentModel()
            {
                FirstName = "Pesho",
                LastName = "Peshov",
                Age = 16,
                Grade = 10,
                School = new SchoolDetails() { Location = school.Location, Name = school.Name },
                Marks = new List<MarkModel>() { new MarkModel() { Subject = "math", Value = 6 } }
            };
            studentsController.Add(studentModel);

            Assert.IsTrue(isItemAdded);
        }
        public void AddStudentWithoutSchool_ShouldNotBeAdded()
        {
            var unitOfWork = Mock.Create<IUnitOfWork>();

            Student studentEntity = new Student()
            {
                FirstName = "Pesho",
                LastName = "Peshov",
                Age = 16,
                Grade = 10,
                Marks = new List<Mark>() { new Mark() { Subject = "math", Value = 6 } }
            };

            bool isItemAdded = false;
            Mock.Arrange(() => unitOfWork.StudentsRepository.Add(Arg.IsAny<Student>()))
                .DoInstead(() => isItemAdded = true)
                .Returns(studentEntity);

            var studentsController = new StudentsController(unitOfWork);
            SetupController(studentsController);

            StudentModel studentModel = new StudentModel()
            {
                FirstName = "Pesho",
                LastName = "Peshov",
                Age = 16,
                Grade = 10,
                Marks = new List<MarkModel>() { new MarkModel() { Subject = "math", Value = 6 } }
            };
            studentsController.Add(studentModel);

            Assert.IsFalse(isItemAdded);
        }