[HttpDelete]// For deleting subject field public HttpResponseMessage DeleteSubjectFields(string id) { if (!ModelState.IsValid || id == null) { response = Request.CreateResponse(HttpStatusCode.BadRequest, new { Message = "Bad Request" }); } else { SubjectFields subjectFields = db.SubjectFields.Find(id); if (subjectFields == null) { response = Request.CreateResponse(HttpStatusCode.NotFound, new { Message = "Not Found" }); } else { db.SubjectFields.Remove(subjectFields); db.SaveChanges(); List <SubjectFields> fields = db.SubjectFields.Where(m => m.sub_year_id == subjectFields.sub_year_id).OrderBy(m => m.field_seq).ToList(); List <SubjectFieldModel> model_fields = new List <SubjectFieldModel>(); foreach (SubjectFields field in fields) { model_fields.Add(new SubjectFieldModel(field)); } response = Request.CreateResponse(HttpStatusCode.OK, new { Message = "Successfully deleted.", list = model_fields }); } } return(response); }
[HttpPut]// For updating subject fields public HttpResponseMessage PutSubjectFields(string id, SubjectFieldModel model) { if (!ModelState.IsValid || id == null || model == null || model.id == null || (id != model.id)) { response = Request.CreateResponse(HttpStatusCode.BadRequest, new { Message = "Bad Request" }); } else { SubjectFields subjectFields = model.getSubjectField(); db.Entry(subjectFields).State = EntityState.Modified; try { db.SaveChanges(); List <SubjectFields> fields = db.SubjectFields.Where(m => m.sub_year_id == subjectFields.sub_year_id).OrderBy(m => m.field_seq).ToList(); List <SubjectFieldModel> model_fields = new List <SubjectFieldModel>(); foreach (SubjectFields field in fields) { model_fields.Add(new SubjectFieldModel(field)); } response = Request.CreateResponse(HttpStatusCode.OK, new { Message = "Saved successfully", list = model_fields }); } catch (DbUpdateConcurrencyException) { if (!SubjectFieldsExists(id)) { response = Request.CreateResponse(HttpStatusCode.NotFound, new { Message = "Subject field not found." }); } else { response = Request.CreateResponse(HttpStatusCode.InternalServerError, new { Message = "An error occurs while updating." }); } } } return(response); }