public void All_WhenStudentDataIsValid()
        {
            var student1 = new Student()
            {
                FirstName = "Andrew",
                LastName = "Fuller",
                Age = 29,
                Grade = 4,
                SchoolId = schoolId
            };

            var student2 = new Student()
            {
                FirstName = "Robert",
                LastName = "King",
                Age = 20,
                Grade = 1,
                SchoolId = schoolId
            };

            int oldStudentsCount = this.dbContext.Set<Student>().Count();

            this.repository.Create(student1);
            this.repository.Create(student2);

            var studentsFound = this.repository.All<Student>(new[] { "School", "Marks" });

            Assert.AreEqual(oldStudentsCount + 2, studentsFound.Count());
            Assert.IsTrue(studentsFound.Any(s => s.Id == student1.Id));
            Assert.IsTrue(studentsFound.Any(s => s.Id == student2.Id));
        }
        public void Add_WhenStudentIsValid_ShouldAddTheStudent()
        {
            var studentDto = new StudentDto()
            {
                FirstName = "Andrew",
                LastName = "Fuller",
                Age = 29,
                Grade = 4,
                SchoolId = 1
            };

            var student = new Student()
            {
                Id = 1,
                FirstName = studentDto.FirstName,
                LastName = studentDto.FirstName,
                Age = studentDto.Age,
                Grade = studentDto.Grade,
            };

            bool isStudentAdded = false;
            var repository = Mock.Create<IRepository>();

            Mock.Arrange(() => repository.Create(Arg.IsAny<Student>()))
                .DoInstead(() => isStudentAdded = true)
                .Returns(student);

            var controller = new StudentsController(repository);
            SetupController(controller);

            HttpResponseMessage response = controller.Post(studentDto);

            Assert.IsTrue(isStudentAdded);
            Assert.AreEqual(HttpStatusCode.Created, response.StatusCode);
        }
        public void Add_WhenStudentDataIsValid_ShouldAddStudentToDatabase()
        {
            var newStudent = new Student()
            {
                FirstName = "Andrew",
                LastName = "Fuller",
                Age = 29,
                Grade = 4,
                SchoolId = schoolId
            };

            this.repository.Create(newStudent);
            var studentFound = this.dbContext.Set<Student>().Find(newStudent.Id);

            Assert.IsNotNull(studentFound);
            Assert.AreEqual(newStudent.FirstName, studentFound.FirstName);
            Assert.AreEqual(newStudent.LastName, studentFound.LastName);
            Assert.AreEqual(newStudent.Age, studentFound.Age);
            Assert.AreEqual(newStudent.Grade, studentFound.Grade);
            Assert.AreEqual(newStudent.SchoolId, studentFound.SchoolId);
        }
        public void GetAll_ShouldReturnAllStudents()
        {
            var student = new Student()
            {
                Id = 1,
                FirstName = "Andrew",
                LastName = "Fuller",
                Age = 29,
                Grade = 4,
                SchoolId = 1
            };

            IList<Student> students = new List<Student>();
            students.Add(student);

            var repository = Mock.Create<IRepository>();

            Mock.Arrange(() => repository.All<Student>(new[] { "School", "Marks" })).Returns(() => students.AsQueryable());

            var controller = new StudentsController(repository);

            var studentsFound = controller.Get();
            Assert.IsTrue(studentsFound.Count() == 1);
            Assert.AreEqual(student.FirstName, studentsFound.First().FirstName);
        }
        public void Filter_ShouldReturnAllStudentsMatchingThePredicate()
        {
            var student = new Student()
            {
                Id = 1,
                FirstName = "Andrew",
                LastName = "Fuller",
                Age = 29,
                Grade = 4,
                SchoolId = 1
            };

            IList<Student> students = new List<Student>();
            students.Add(student);

            var repository = Mock.Create<IRepository>();

            Mock.Arrange(() => repository.Filter<Student>(
                Arg.IsAny<Expression<Func<Student, bool>>>(),
                new[] { "School", "Marks" }))
                .Returns(() => students.AsQueryable());

            var controller = new StudentsController(repository);

            var studentsFound = controller.Get("Maths", 6.0f);
            Assert.IsTrue(studentsFound.Count() == 1);
            Assert.AreEqual(student.FirstName, studentsFound.First().FirstName);
        }
        public void GetById_ShouldReturnSingleStudent()
        {
            var student = new Student()
            {
                Id = 1,
                FirstName = "Andrew",
                LastName = "Fuller",
                Age = 29,
                Grade = 4,
                SchoolId = 1
            };

            var repository = Mock.Create<IRepository>();

            Mock.Arrange(() => repository.Find<Student>(
                Arg.IsAny<Expression<Func<Student, bool>>>(),
                new[] { "School", "Marks" }))
                .Returns(student);

            var controller = new StudentsController(repository);

            var studentFound = controller.Get(student.Id);
            Assert.AreEqual(student.Id, studentFound.Id);
            Assert.AreEqual(student.FirstName, studentFound.FirstName);
            Assert.AreEqual(student.LastName, studentFound.LastName);
            Assert.AreEqual(student.Age, studentFound.Age);
            Assert.AreEqual(student.Grade, studentFound.Grade);
            Assert.AreEqual(student.SchoolId, studentFound.SchoolId);
        }
        public void Filter_WhenStudentDataIsValid()
        {
            var newStudent = new Student()
            {
                FirstName = "Andrew",
                LastName = "Fuller",
                Age = 29,
                Grade = 4,
                SchoolId = schoolId
            };

            this.repository.Create(newStudent);

            var studentsFound = this.repository.Filter<Student>(s => s.Id == newStudent.Id, new[] { "School", "Marks" });

            Assert.AreEqual(1, studentsFound.Count());
            Assert.AreEqual(newStudent.FirstName, studentsFound.First().FirstName);
            Assert.AreEqual(newStudent.LastName, studentsFound.First().LastName);
            Assert.AreEqual(newStudent.Age, studentsFound.First().Age);
            Assert.AreEqual(newStudent.Grade, studentsFound.First().Grade);
            Assert.AreEqual(newStudent.SchoolId, studentsFound.First().SchoolId);
        }
        public HttpResponseMessage Post(StudentDto value)
        {
            var newStudent = new Student
            {
                SchoolId = value.SchoolId,
                FirstName = value.FirstName,
                LastName = value.LastName,
                Age = value.Age,
                Grade = value.Grade
            };

            HttpResponseMessage response;

            try
            {
                apiControllerHelper.Post<Student>(newStudent);

                if (value.Marks != null)
                {
                    foreach (var mark in value.Marks)
                    {
                        apiControllerHelper.Post<Mark>(new Mark
                        {
                            StudentId = newStudent.Id,
                            Subject = mark.Subject,
                            Value = mark.Value
                        });
                    }
                }

                var createdStudentDto = new StudentDto()
                {
                    Id = newStudent.Id,
                    FirstName = newStudent.FirstName,
                    LastName = newStudent.LastName,
                    Age = newStudent.Age,
                    Grade = newStudent.Grade
                };

                response = Request.CreateResponse<StudentDto>(HttpStatusCode.Created, createdStudentDto);
                var resourceLink = Url.Link("DefaultApi", new { id = createdStudentDto.Id });

                response.Headers.Location = new Uri(resourceLink);
            }
            catch (Exception ex)
            {
                response = Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex);
            }

            return response;
        }