Exemple #1
0
        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);
        }
Exemple #2
0
        public HttpResponseMessage Post(SchoolDto value)
        {
            var newSchool = new School
            {
                Location = value.Location,
                Name     = value.Name
            };

            HttpResponseMessage response;

            try
            {
                apiControllerHelper.Post <School>(newSchool);

                var createdSchoolDto = new SchoolDto()
                {
                    Id       = newSchool.Id,
                    Name     = newSchool.Name,
                    Location = newSchool.Location
                };

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

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

            return(response);
        }