public HttpResponseMessage CreateAdvisor(Advisor advisor) { var checkEmail = dbcontext.Advisors.Where(a_name => a_name.Email == advisor.Email).Any(); if (!checkEmail) { dbcontext.Advisors.Add(advisor); try { dbcontext.SaveChanges(); } catch (Exception ex) { return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex); } HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, advisor); response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); return response; } HttpResponseMessage res = Request.CreateResponse(HttpStatusCode.NotFound, "Not Found"); return res; }
private void LoadAdvisors(HttpPostedFileBase file) { using (var package = new ExcelPackage(file.InputStream)) { var advisorworkSheet = package.Workbook.Worksheets["Advisors"]; var advisornoOfRow = advisorworkSheet.Dimension.End.Row; for (int rowIterator = 2; rowIterator <= advisornoOfRow; rowIterator++) { Advisor advisor = new Advisor(); if (advisorworkSheet.Cells[rowIterator, 1].Value != null) { advisor.Firstname = advisorworkSheet.Cells[rowIterator, 1].Value.ToString(); advisor.Lastname = advisorworkSheet.Cells[rowIterator, 2].Value.ToString(); advisor.Email = advisorworkSheet.Cells[rowIterator, 3].Value.ToString(); advisor.Phoneno = advisorworkSheet.Cells[rowIterator, 4].Value.ToString(); advisor.dept_Id = int.Parse(advisorworkSheet.Cells[rowIterator, 5].Value.ToString()); dbcontext.Advisors.Add(advisor); dbcontext.SaveChanges(); } } } }
public HttpResponseMessage UpdateAdvisor(Advisor advisor) { if (advisor != null) { dbcontext.Entry(advisor).State = EntityState.Modified; } try { dbcontext.SaveChanges(); } catch (Exception ex) { return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex); } HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, advisor); response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); return response; }