// PUT api/EmployeeAPI/5 public IHttpActionResult PutEmployee(int id, Employee employee) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != employee.EmpNo) { return(BadRequest()); } db.Entry(employee).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!EmployeeExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public HttpResponseMessage Post(Employee emp) { try { if (ModelState.IsValid) { int max_id = db.employees.Max(e => e.id); emp.id = max_id + 1; db.employees.Add(emp); db.SaveChanges(); //return response status as successfully created with Employee entity var response = Request.CreateResponse <Employee>(HttpStatusCode.Created, emp); //Response message with request uri for checking purpose response.Headers.Location = new Uri(Request.RequestUri + emp.id.ToString()); return(response); } else { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Error in Employee Creation")); } } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex.Message)); } }
public IHttpActionResult empinsert(Newempreg empinsert) { em.Newempregs.Add(empinsert); em.SaveChanges(); return(Ok()); }
public ActionResult Create([Bind(Include = "ID,First,Last,email,position")] emp emp) { if (ModelState.IsValid) { db.emps.Add(emp); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(emp)); }
public HttpResponseMessage Put(int id, [FromBody] Employee emp) { using (EmpDBEntities entities = new EmpDBEntities()) { var entity = entities.Employees.FirstOrDefault(e => e.Id == id); if (entity == null) { return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Emp with Id: " + id.ToString() + " NOT FOUND!")); } try { entity.Name = emp.Name; entity.Email = emp.Email; entity.PhotoPath = emp.PhotoPath; entity.Department = emp.Department; entities.SaveChanges(); return(Request.CreateResponse(HttpStatusCode.OK, entity)); } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex)); } } }
public static string refreshTToken() { string _token = ""; string userName = "******"; string passWord = "******"; string BaseAddress = "http://localhost:56473/Token"; using (var client = new HttpClient { BaseAddress = new Uri(BaseAddress) }) { var token = client.PostAsync("Token", new FormUrlEncodedContent(new[] { new KeyValuePair <string, string>("grant_type", "password"), new KeyValuePair <string, string>("username", userName), new KeyValuePair <string, string>("password", passWord) })).Result.Content.ReadAsAsync <AuthenticationToken>().Result; Console.WriteLine("..."); if (token != null) { _token = token.access_token.ToString().Trim(); // insert accessToken to AccessToken table // create new AccessToken record Data_WebApi_Jquery.Models.AccessToken accessToken = new Data_WebApi_Jquery.Models.AccessToken(); accessToken.Created = DateTime.Now; accessToken.TokenString = token.access_token; try { using (EmpDBEntities dc = new EmpDBEntities()) { dc.AccessTokens.Add(accessToken); dc.SaveChanges(); } } catch (Exception e) { Console.WriteLine(e.ToString()); } return(_token); } } return(_token); }
//public void Post([FromBody] Employee emp) public HttpResponseMessage Post([FromBody] Employee emp) { try { using (EmpDBEntities entities = new EmpDBEntities()) { entities.Employees.Add(emp); entities.SaveChanges(); var message = Request.CreateResponse(HttpStatusCode.Created, emp); message.Headers.Location = new Uri(Request.RequestUri + emp.Id.ToString()); return(message); } } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex)); } }
public HttpResponseMessage Delete(int id) { try { using (EmpDBEntities entities = new EmpDBEntities()) { var emp = entities.Employees.FirstOrDefault(e => e.Id == id); if (emp != null) { entities.Employees.Remove(entities.Employees.FirstOrDefault(e => e.Id == id)); entities.SaveChanges(); return(Request.CreateResponse(HttpStatusCode.OK, emp)); } else { return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Emp with Id: " + id.ToString() + " NOT FOUND!")); } } }catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex)); } }
public ActionResult Index(Part7IndexView part7IndexView) { if (Session["SelectedProductName"] != null) { Test_String = Session["SelectedProductName"].ToString(); } //await GetTokenDetails("*****@*****.**", "123456"); string userName = part7IndexView.UserName.Trim(); //string passWord = "******"; string passWord = part7IndexView.Password.Trim(); string BaseAddress = "http://localhost:56473/Token"; using (var client = new HttpClient { BaseAddress = new Uri(BaseAddress) }) { var token = client.PostAsync("Token", new FormUrlEncodedContent(new[] { new KeyValuePair <string, string>("grant_type", "password"), new KeyValuePair <string, string>("username", userName), new KeyValuePair <string, string>("password", passWord) })).Result.Content.ReadAsAsync <AuthenticationToken>().Result; Console.WriteLine("..."); if (token != null) { part7IndexView.access_token = token.access_token.ToString().Trim(); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(token.token_type, token.access_token); // insert accessToken to AccessToken table // create new AccessToken record Data_WebApi_Jquery.Models.AccessToken accessToken = new Data_WebApi_Jquery.Models.AccessToken(); accessToken.Created = DateTime.Now; accessToken.TokenString = token.access_token; Session["SelectedProductName"] = token.access_token; try { using (EmpDBEntities dc = new EmpDBEntities()) { dc.AccessTokens.Add(accessToken); dc.SaveChanges(); } } catch (Exception e) { Console.WriteLine(e.ToString()); } } // actual requests from your api follow here . . . } return(View(part7IndexView)); }