public HttpResponseMessage CreateDepartment(Department department) { var checkName = dbcontext.Departments.Where(d_name => d_name.dept_name == department.dept_name).Any(); if (!checkName) { dbcontext.Departments.Add(department); try { dbcontext.SaveChanges(); } catch (Exception ex) { return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex); } HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, department); response.StatusCode = HttpStatusCode.Created; response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); return response; } HttpResponseMessage res = Request.CreateResponse(HttpStatusCode.OK, "Not Found"); return res; }
public HttpResponseMessage UpdateDepartment(Department department) { if (department != null) { db.Entry(department).State = EntityState.Modified; } try { db.SaveChanges(); } catch (Exception ex) { return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex); } HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, department); response.StatusCode = HttpStatusCode.Created; response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); return response; }
public HttpResponseMessage CreateDepartment(Department department) { db.Departments.Add(department); try { db.SaveChanges(); } catch (Exception ex) { return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex); } HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, department); response.StatusCode = HttpStatusCode.Created; response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); return response; }
private void LoadDept(HttpPostedFileBase file) { using (var package = new ExcelPackage(file.InputStream)) { var deptworkSheet = package.Workbook.Worksheets["Depts"]; var deptnoOfRow = deptworkSheet.Dimension.End.Row; for (int rowIterator = 2; rowIterator <= deptnoOfRow; rowIterator++) { Department dept = new Department(); if (deptworkSheet.Cells[rowIterator, 1].Value != null) { //dept.dept_Id = int.Parse(deptworkSheet.Cells[rowIterator, 1].Value.ToString()); dept.dept_name = deptworkSheet.Cells[rowIterator, 1].Value.ToString(); dept.room_no = deptworkSheet.Cells[rowIterator, 2].Value.ToString(); dept.campus_Id = int.Parse(deptworkSheet.Cells[rowIterator, 3].Value.ToString()); dbcontext.Departments.Add(dept); dbcontext.SaveChanges(); } } } }