// GET api/universities/{universityCode}/programs/{programCode} public EIProgram GetProgram(string universityCode, string programCode) { EIProgram program = db.Programs.SingleOrDefault(p => p.EducationalInstitute.Code == universityCode && p.Code == programCode); if (program == null) { throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound)); } return(program); }
// POST api/universities/{universityCode}/programs/ public HttpResponseMessage PostProgram(string universityCode, EIProgram program) { if (ModelState.IsValid) { program.EducationalInstitute = db.EducationalInstitutes.Single(u => u.Code == universityCode); db.Programs.Add(program); db.SaveChanges(); HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, program); response.Headers.Location = new Uri(Url.Link("ProgramsOfEducationalInstitute", new { universityCode = universityCode, programCode = program.Code })); return(response); } else { return(Request.CreateResponse(HttpStatusCode.BadRequest)); } }
// DELETE api/universities/{universityCode}/programs/{programCode} public HttpResponseMessage DeleteProgram(string universityCode, string programCode) { EIProgram program = GetProgram(universityCode, programCode); if (program == null) { return(Request.CreateResponse(HttpStatusCode.NotFound)); } db.Programs.Remove(program); try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { return(Request.CreateResponse(HttpStatusCode.NotFound)); } return(Request.CreateResponse(HttpStatusCode.OK, program)); }
// PUT api/universities/{universityCode}/programs/{programCode} public HttpResponseMessage PutProgram(string universityCode, EIProgram program) { if (ModelState.IsValid) { program.EducationalInstitute = db.EducationalInstitutes.Single(u => u.Code == universityCode); db.Entry(program).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { return(Request.CreateResponse(HttpStatusCode.NotFound)); } return(Request.CreateResponse(HttpStatusCode.OK)); } else { return(Request.CreateResponse(HttpStatusCode.BadRequest)); } }