예제 #1
0
        // POST api/Students
        public HttpResponseMessage PostStudent(StudentModel student)
        {
            if (student != null)
            {
                var entityToAdd = new Student()
                {
                    FirstName = student.FirstName,
                    LastName = student.LastName,
                    Age = student.Age,
                    Grade = student.Grade
                };

                var createdEntity = this.studentsRepository.Add(entityToAdd);

                var createdModel = new StudentModel()
                {
                    StudentId = createdEntity.StudentId,
                    FirstName = createdEntity.FirstName,
                    Age = createdEntity.Age
                };

                var response = Request.CreateResponse<StudentModel>(HttpStatusCode.Created, createdModel);
                var resourceLink = Url.Link("DefaultApi", new { id = createdModel.StudentId });

                response.Headers.Location = new Uri(resourceLink);

                return response;
            }

            return Request.CreateResponse<StudentModel>(HttpStatusCode.BadRequest, student);
        }
        public void GetAll_CheckForSingleStudentInRepository()
        {
            var repository = new FakeDbRepository<Student>();

            var stud = new Student()
            {
                FirstName = "First",
                LastName = "Last",
                Age = 100,
                Grade = 1
            };
            repository.entities.Add(stud);

            var studExpected = new StudentModel()
            {
                Name = "First Last"
            };
            var controller = new StudentsController(repository);

            var studentsModels = controller.GetStudents();
            Assert.IsTrue(studentsModels.Count() == 1);
            Assert.AreEqual(studExpected.Name, studentsModels.First().Name);
        }
        // GET api/students
        public IEnumerable<StudentModel> Get()
        {
            var sent = new StudentModel[]{
                new StudentModel(){
                    Id = students[0].Id,
                    Name = students[0].Name,
                    Grade = students[0].Grade
                },
                new StudentModel(){
                    Id = students[1].Id,
                    Name = students[1].Name,
                    Grade = students[1].Grade
                },
                new StudentModel(){
                    Id = students[2].Id,
                    Name = students[2].Name,
                    Grade = students[2].Grade
                },
                new StudentModel(){
                    Id = students[3].Id,
                    Name = students[3].Name,
                    Grade = students[3].Grade
                },
                new StudentModel(){
                    Id = students[4].Id,
                    Name = students[4].Name,
                    Grade = students[4].Grade
                },
            };

            return sent;
        }
        // GET api/students
        public HttpResponseMessage Get()
        {
            StudentModel[] students = new StudentModel[]
            {
                new StudentModel()
                {
                    FirstName = "Doncho",
                    LastName = "Minkov",
                    Marks =  new MarkModel[] {
                        new MarkModel()
                        {
                            Subject = "JS",
                            Score = 5.5
                        }, 
                        new MarkModel()
                        {
                            Subject = "Math",
                            Score = 3
                        },
                        new MarkModel()
                        {
                            Subject = "CSS",
                            Score = 6
                        },
                    }
                },
                new StudentModel()
                {
                    FirstName = "Svetlin",
                    LastName = "Nakov",
                    Marks =  new MarkModel[] {
                        new MarkModel()
                        {
                            Subject = "C#",
                            Score = 5.4
                        },
                        new MarkModel()
                        {
                            Subject = "JavaScript",
                            Score = 4
                        },
                        new MarkModel()
                        {
                            Subject = "Mind Map",
                            Score = 6
                        },
                    }
                },
                new StudentModel()
                {
                    FirstName = "Samuel",
                    LastName = "Jackson",
                    Marks =  new MarkModel[] {
                        new MarkModel()
                        {
                            Subject = "Jedy",
                            Score = 6
                        },
                        new MarkModel()
                        {
                            Subject = "Acting",
                            Score = 6
                        },
                        new MarkModel()
                        {
                            Subject = "Coolness",
                            Score = 6
                        },
                        new MarkModel()
                        {
                            Subject = "Immortality",
                            Score = 6
                        },
                    }
                },
                new StudentModel()
                {
                    FirstName = "John",
                    LastName = "Doe",
                    
                },
            };

            var responce = Request.CreateResponse(HttpStatusCode.OK, students, "application/json");

            return responce;
        }
        // GET api/students
        public HttpResponseMessage Get()
        {
            StudentModel[] students = new StudentModel[]
            {
                new StudentModel()
                {
                    FirstName = "Doncho",
                    LastName = "Minkov",
                    Marks =  new MarkModel[] {
                        new MarkModel()
                        {
                            Subject = "Math",
                            Score = 5.9
                        }, new MarkModel()
                        {
                            Subject = "BG",
                            Score = 3
                        },
                        new MarkModel()
                        {
                            Subject = "JavaScript",
                            Score = 6
                        },
                    }
                },
                new StudentModel()
                {
                    FirstName = "Svetlin",
                    LastName = "Nakov",
                    Marks =  new MarkModel[] {
                        new MarkModel()
                        {
                            Subject = "Math",
                            Score = 4.9
                        },
                        new MarkModel()
                        {
                            Subject = "JavaScript",
                            Score = 3
                        },
                    }
                },
                new StudentModel()
                {
                    FirstName = "Pesho",
                    LastName = "Ivanov",
                    Marks =  new MarkModel[] {
                        new MarkModel()
                        {
                            Subject = "BG",
                            Score = 3
                        },
                        new MarkModel()
                        {
                            Subject = "JavaScript",
                            Score = 6
                        },
                    }
                },
                new StudentModel()
                {
                    FirstName = "Marian",
                    LastName = "Marinov",

                },
                 new StudentModel()
                {
                    FirstName = "Minko",
                    LastName = "Donchov",
                    Marks =  new MarkModel[] {
                        new MarkModel()
                        {
                            Subject = "Math",
                            Score = 5.9
                        }, new MarkModel()
                        {
                            Subject = "BG",
                            Score = 3
                        },
                        new MarkModel()
                        {
                            Subject = "JavaScript",
                            Score = 6
                        },
                    }
                }
            };

            var responce = Request.CreateResponse(HttpStatusCode.OK, students, "application/json");

            return responce;
        }