public HttpResponseMessage AddStudent(Api_Models.MstStudent_ApiModel objStudent) { try { var course = from d in db.MstCourses where d.Id == objStudent.CourseId select d; if (course.Any()) { Data.MstStudent newStudent = new Data.MstStudent { StudentCode = objStudent.StudentCode, FullName = objStudent.FullName, CourseId = course.FirstOrDefault().Id, }; db.MstStudents.InsertOnSubmit(newStudent); db.SubmitChanges(); return(Request.CreateResponse(HttpStatusCode.OK, "Successfully added!")); } else { return(Request.CreateResponse(HttpStatusCode.NotFound, "Course not found!")); } } catch (Exception e) { return(Request.CreateResponse(HttpStatusCode.InternalServerError, e.Message)); } }
public HttpResponseMessage UpdateStudent(String studentId, Api_Models.MstStudent_ApiModel objStudent) { try { var student = from d in db.MstStudents where d.Id == Convert.ToInt32(studentId) select d; var course = from d in db.MstCourses where d.Id == objStudent.CourseId select d; if (!course.Any()) { return(Request.CreateResponse(HttpStatusCode.NotFound, "Course not found!")); } if (student.Any()) { var updateStudent = student.FirstOrDefault(); updateStudent.StudentCode = objStudent.StudentCode; updateStudent.FullName = objStudent.FullName; updateStudent.CourseId = course.FirstOrDefault().Id; db.SubmitChanges(); return(Request.CreateResponse(HttpStatusCode.OK, "Successfully added!")); } else { return(Request.CreateResponse(HttpStatusCode.NotFound, "Student not found!")); } } catch (Exception e) { return(Request.CreateResponse(HttpStatusCode.InternalServerError, e.Message)); } }