public void TransformSchoolFromEntity(School schoolEntity) { Name = schoolEntity.Name; SchoolId = schoolEntity.SchoolId; Location = schoolEntity.Location; if (schoolEntity.Students != null) { Students = (from student in schoolEntity.Students select new StudentModel() { FirstName = student.FirstName, LastName = student.LastName, Age = student.Age, Grade = student.Grade, StudentId = student.StudentId }).ToList(); } }
public HttpResponseMessage Post(SchoolFullModel school) { var entityToAdd = new School() { Name = school.Name, Location = school.Location }; if (school.Students != null) { entityToAdd.Students = (from student in school.Students select new Student() { FirstName = student.FirstName, LastName = student.LastName, Age = student.Age, Grade = student.Grade, }).ToList(); } var createdEntity = this.schoolsRepository.Add(entityToAdd); SchoolFullModel schoolModel = new SchoolFullModel(); schoolModel.TransformSchoolFromEntity(createdEntity); var response = Request.CreateResponse<SchoolFullModel>(HttpStatusCode.Created, schoolModel); var resourceLink = Url.Link("DefaultApi", new { id = schoolModel.SchoolId }); response.Headers.Location = new Uri(resourceLink); return response; }