public HttpResponseMessage PostStudent(Student studentEntity) { if (studentEntity == null || studentEntity.FirstName == null || studentEntity.LastName == null || studentEntity.School == null) { var errResponse = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "The student is invalid"); return(errResponse); } try { var entity = this.studentRepository.Add(studentEntity); var response = this.Request.CreateResponse( HttpStatusCode.Created, StudentModel.CreateFromStudentEntity(studentEntity)); response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = entity.StudentId })); return(response); } catch (DbUpdateConcurrencyException) { var errResponse = this.Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "The student could not be added because of a concurrency problem"); return(errResponse); } }
public HttpResponseMessage PutStudent(int id, Student studentEntity) { if (id <= 0) { var errResponse = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "The id of the student must be positive"); return(errResponse); } try { var entity = this.studentRepository.Update(id, studentEntity); var response = this.Request.CreateResponse( HttpStatusCode.OK, StudentModel.CreateFromStudentEntity(entity)); response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = entity.StudentId })); return(response); } catch (DbUpdateConcurrencyException) { var errResponse = this.Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "The student could not be updated because of a concurrency problem"); return(errResponse); } catch (NullReferenceException) { var errResponse = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, string.Format("A Student entity with id={0} does not exist in the Database", id)); return(errResponse); } }
public IEnumerable <StudentModel> GetAll() { var entities = this.studentRepository.GetAll(); List <StudentModel> models = new List <StudentModel>(); foreach (var entity in entities) { models.Add(StudentModel.CreateFromStudentEntity(entity)); } return(models); }
public IEnumerable <StudentModel> Find(string subject, double?value) { var entities = this.studentRepository .Find(s => s.Marks.Any(m => m.Subject == subject && m.Value >= value)); List <StudentModel> models = new List <StudentModel>(); foreach (var entity in entities) { models.Add(StudentModel.CreateFromStudentEntity(entity)); } return(models); }
public StudentModel Get(int id) { if (id <= 0) { var errResponse = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "The id of the student must be positive"); throw new HttpResponseException(errResponse); } var entity = this.studentRepository.GetById(id); if (entity != null) { StudentModel model = StudentModel.CreateFromStudentEntity(entity); return(model); } else { return(null); } }