示例#1
0
        // POST api/Students
        public HttpResponseMessage PostStudent(StudentModel studentModel)
        {
            if (studentModel == null)
            {
                var errResponse = Request.CreateErrorResponse(HttpStatusCode.BadRequest, "The supported student model cannot be null");
                throw new HttpResponseException(errResponse);
            }

            if (studentModel.SchoolId == 0)
            {
                var errResponse = Request.CreateErrorResponse(HttpStatusCode.BadRequest, "The school Id is not correct");
                throw new HttpResponseException(errResponse);
            }

            DbSchoolRepository schoolRepository = this.allRepositories.GetSchoolRepository();

            Student student = new Student()
            {
                FirstName = studentModel.FirstName,
                LastName  = studentModel.LastName,
                Age       = studentModel.Age,
                Grade     = studentModel.Grade,
                School    = schoolRepository.Get(studentModel.SchoolId)
            };

            DbStudentsRepository studentRepository = this.allRepositories.GetStudentsRepository();

            studentRepository.Add(student);

            StudentModel createdStudentModel = new StudentModel()
            {
                Id        = student.Id,
                FirstName = student.FirstName,
                LastName  = student.LastName,
                Age       = student.Age,
                Grade     = student.Grade,
                SchoolId  = student.School.Id
            };

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

            response.Headers.Location = new Uri(resourceLink);
            return(response);
        }
示例#2
0
        // GET api/Students
        public IEnumerable <StudentModel> GetStudents()
        {
            DbStudentsRepository studentRepository = this.allRepositories.GetStudentsRepository();
            var studentEntities = studentRepository.All();

            var studentModels =
                from studEntity in studentEntities
                select new StudentModel()
            {
                Id        = studEntity.Id,
                FirstName = studEntity.FirstName,
                LastName  = studEntity.LastName,
                Age       = studEntity.Age,
                Grade     = studEntity.Grade,
                SchoolId  = studEntity.School.Id
            };

            return(studentModels.ToList());
        }
示例#3
0
        // GET api/Students
        public IEnumerable <StudentModel> GetStudents(string subject, decimal value)
        {
            DbStudentsRepository studentRepository = this.allRepositories.GetStudentsRepository();

            var studentEntities = studentRepository.All().Where(s => s.Marks.Any(m => (m.Subject == subject && m.Value > value)));

            var studentModels =
                from studEntity in studentEntities
                select new StudentModel()
            {
                Id        = studEntity.Id,
                FirstName = studEntity.FirstName,
                LastName  = studEntity.LastName,
                Age       = studEntity.Age,
                Grade     = studEntity.Grade,
                SchoolId  = studEntity.School.Id
            };

            return(studentModels.ToList());
        }
 public object GetService(Type serviceType)
 {
     var context = new SchoolContext();
     if (serviceType == typeof(MarksController))
     {
         var repository = new DbMarksReposiotry(context);
         return new MarksController(repository);
     }
     else if (serviceType == typeof(StudentsController))
     {
         var repository = new DbStudentsRepository(context);
         return new StudentsController(repository);
     }
     else if (serviceType == typeof(TownSchoolsController))
     {
         var repository = new DbTownSchoolsRepository(context);
         return new TownSchoolsController(repository);
     }
     else
     {
         return null;
     }
 }
示例#5
0
        // POST api/Marks
        public HttpResponseMessage PostMarks(MarkModel marksModel)
        {
            if (marksModel == null)
            {
                var errResponse = Request.CreateErrorResponse(HttpStatusCode.BadRequest, "The supported mark model cannot be null");
                throw new HttpResponseException(errResponse);
            }

            DbStudentsRepository studentRepository = this.allRepositories.GetStudentsRepository();

            Student student = studentRepository.Get(marksModel.StudentId);

            DbMarksRepository marksRepository = this.allRepositories.GetMarksRepository();

            Mark mark = new Mark()
            {
                Subject = marksModel.Subject,
                Value   = marksModel.Value,
                Student = student
            };

            marksRepository.Add(mark);

            MarkModel createdMarkModel = new MarkModel()
            {
                Id        = mark.Id,
                Subject   = mark.Subject,
                Value     = mark.Value,
                StudentId = mark.Student.Id
            };

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

            response.Headers.Location = new Uri(resourceLink);
            return(response);
        }
 public void CreateRepository_WithNullContext_Test()
 {
     DbStudentsRepository studentsRepository = new DbStudentsRepository(null);
 }